NOESIS Samples - Localization and language specific fonts
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?
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?
-
-
sfernandez
Site Admin
- Posts: 2537
- Joined:
Re: NOESIS Samples - Localization and language specific fonts
Hello,
Our localization sample sets the fonts in the root of MainWindow.xaml:
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?
Our localization sample sets the fonts in the root of MainWindow.xaml:
Code: Select all
FontFamily="./#Oxygen, ./#FontopoNIHONGO"
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?
Re: NOESIS Samples - Localization and language specific fonts
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 -
I then reference the value converter via a binding in the XAML as follows.
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.
I'm not proud of this hack. Is there a simpler way to call my localise function from XAML?
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 -
Code: Select all
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;
}
Code: Select all
<Label Content="{Binding <<DataBoundValue>>, Converter={StaticResource localise}"/>
Code: Select all
<Label Content="{Binding Converter={StaticResource localise},ConverterParameter=UnlocalisedString}"/>
-
-
sfernandez
Site Admin
- Posts: 2537
- Joined:
Re: NOESIS Samples - Localization and language specific fonts
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
There are other approaches you can read about in this thread: viewtopic.php?f=3&t=1574
Re: NOESIS Samples - Localization and language specific fonts
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?
-
-
sfernandez
Site Admin
- Posts: 2537
- Joined:
Re: NOESIS Samples - Localization and language specific fonts
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?
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.
Can you use that to trigger the update?
Code: Select all
<TextBlock Text="{Binding Localization, Converter={StaticResource localize}, ConverterParameter=TEXTID1}"/>
Re: NOESIS Samples - Localization and language specific fonts
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.
-
-
sfernandez
Site Admin
- Posts: 2537
- Joined:
Re: NOESIS Samples - Localization and language specific fonts
Localization can be a property in some base class of your view models that just returns a static object:
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.
Code: Select all
public ViewModelBase : INotifyPropertyChanged
{
public object Localization { get { return LocalizationManager.Localization; } }
}
Who is online
Users browsing this forum: Google [Bot], sfernandez and 2 guests