jazza
Topic Author
Posts: 5
Joined: 26 Sep 2019, 08:32

NOESIS Samples - Localization and language specific fonts

09 Jan 2020, 01:28

Hi all,

I am trying to understand how the NOESIS "Localization" example chooses a font to render a localised string. I can see that 'Main.cpp' lists Japanese and English fonts but I cannot see anything in the code that specifies which font to use based on the current language.

Additionally, if my game has several fonts for a language, is there a way for me to specify which one to use?

Tags:
 
User avatar
sfernandez
Site Admin
Posts: 2062
Joined: 22 Dec 2011, 19:20

Re: NOESIS Samples - Localization and language specific fonts

09 Jan 2020, 10:21

Hello,

Our localization sample sets the fonts in the root of MainWindow.xaml:
FontFamily="./#Oxygen, ./#FontopoNIHONGO"
When specifying multiple fonts in a FontFamily, characters are rendered using the first font in the list that contains the corresponding glyph.
In this sample, when writing English/French, all latin characters can be fond inside Oxygen font. When writing Japanese, the characters are found inside FontopoNIHONGO font.

If your game wants to use different fonts for different parts of the UI you can set the FontFamily property as explained in our Text tutorial. Is that what you need?
 
jazza
Topic Author
Posts: 5
Joined: 26 Sep 2019, 08:32

Re: NOESIS Samples - Localization and language specific fonts

10 Jan 2020, 04:10

Thanks for the prompt reply! I noticed the font family on the root window of the XAML after I wrote my post but still wasn't sure how NOESIS decided which one to use. It's alot clearer now so thank you.

While we're on the topic of localisation, I am curious about whether my chosen approach is appropriate. I am currently integrating NOESIS with a proprietary engine that has a Localisation class with a built in dictionary of keywords and matching localised strings. A function on this class takes an input string and returns a localised value based on the user's preferred language.

I figure that the best way to leverage this class via NOESIS is to use a value converter like so -
bool LocalisationConverter::TryConvert(BaseComponent* value, const Type* targetType, BaseComponent* parameter,
	Ptr<BaseComponent>& result)
{
	bool converted = false;
	if (targetType == TypeOf<NsString>())
	{
		// If the bound value is null, convert the parameter instead
		BaseComponent* valueToConvert = value == NULL ? parameter : value;
		if (Boxing::CanUnbox<NsString>(valueToConvert))
		{
			NsString sourceString = Boxing::Unbox<NsString>(valueToConvert);
			result = Boxing::Box<NsString>(Localisation::GetLocalised(sourceString));
			converted = true;
		}
	}

	return converted;
}
I then reference the value converter via a binding in the XAML as follows.
<Label Content="{Binding <<DataBoundValue>>, Converter={StaticResource localise}"/>
Where my approach falls apart is when I have to localise strings that are not bindable objects. I've resorted to a hack to solve this issue where I pass the string to the value converter using the 'ConverterParameter' attribute.
<Label Content="{Binding Converter={StaticResource localise},ConverterParameter=UnlocalisedString}"/>
I'm not proud of this hack. Is there a simpler way to call my localise function from XAML?
 
User avatar
sfernandez
Site Admin
Posts: 2062
Joined: 22 Dec 2011, 19:20

Re: NOESIS Samples - Localization and language specific fonts

16 Jan 2020, 12:36

Using a converter like you did is a good option because lets you localize bound values and explicit strings (via the ConverterParameter).

There are other approaches you can read about in this thread: viewtopic.php?f=3&t=1574
 
asusralis
Posts: 111
Joined: 30 Jul 2018, 05:03

Re: NOESIS Samples - Localization and language specific fonts

18 Dec 2020, 02:39

Sorry to bump this thread, but I had the same idea with using a converter. Is there any way to update all bindings that use the loc converter? My loc service has a static event I can listen to, but I'm not sure if you could with just a converter. I think I could create a behavior that does this, but then I need to attach a behavior to almost every TextBlock. Is there a better way to do this without explicitly defining localizations in xaml?
 
User avatar
sfernandez
Site Admin
Posts: 2062
Joined: 22 Dec 2011, 19:20

Re: NOESIS Samples - Localization and language specific fonts

18 Dec 2020, 12:37

Bindings are reevaluated when the source property notifies of changes, even if property didn't really change.
Can you use that to trigger the update?
<TextBlock Text="{Binding Localization, Converter={StaticResource localize}, ConverterParameter=TEXTID1}"/>
In this example everytime you raise the PropertyChanged event in your view model for the "Localization" property it will evaluate the binding and call the converter.
 
asusralis
Posts: 111
Joined: 30 Jul 2018, 05:03

Re: NOESIS Samples - Localization and language specific fonts

18 Dec 2020, 18:08

Is there any way to bind this to something static that could be notified? I was trying to keep all of this out of my view models.
 
User avatar
sfernandez
Site Admin
Posts: 2062
Joined: 22 Dec 2011, 19:20

Re: NOESIS Samples - Localization and language specific fonts

18 Dec 2020, 18:28

Localization can be a property in some base class of your view models that just returns a static object:
public ViewModelBase : INotifyPropertyChanged
{
  public object Localization { get { return LocalizationManager.Localization; } }
}
That base view model can hook to the static event you mentioned before, and raise the PropertyChanged for the Localization property to trigger the update of all localized bindings.

Who is online

Users browsing this forum: No registered users and 0 guests