-
- burst2flame
- Posts: 31
- Joined:
Localization Formats
The localization tutorial seems straightforward, but I was wondering if there's a way to have it accept other formats like xliff or json. Also does the system work with a fallback language. Like if the key just isn't in the dictionary will it fall back to the default language?
Also is there a way to switch fonts as well. Thanks for the help!
Also is there a way to switch fonts as well. Thanks for the help!
-
-
sfernandez
Site Admin
- Posts: 3363
- Joined:
Re: Localization Formats
Our LocExtension relies on ResourceDictionaries to get the values of the specified keys. There is no way to support other formats in our extension, but you can implement your own MarkupExtension if you need a different approach.The localization tutorial seems straightforward, but I was wondering if there's a way to have it accept other formats like xliff or json.
It can be achieved by merging the default language in the specific language dictionary. For example, if you have a Language-en.xaml (the default language), then you can have a Language-fr.xaml that merges it:Also does the system work with a fallback language. Like if the key just isn't in the dictionary will it fall back to the default language?
Code: Select all
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Language-en.xaml"/> <!-- Default language is merged -->
</ResourceDictionary.MergedDictionaries>
<!-- Entries for the FR language -->
</ResourceDictionary>Yes, our LocExtension can point to any resource defined in the Source dictionary, even a FontFamily resource:Also is there a way to switch fonts as well.
Code: Select all
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<FontFamily x:Key="TheFont">Fonts/#Arial</FontFamily>
</ResourceDictionary>Code: Select all
<TextBlock Text="Title" FontFamily="{noesis:Loc TheFont}"/>-
- burst2flame
- Posts: 31
- Joined:
Re: Localization Formats
Thanks, this is very helpful!
One last question was how the project would handle plurals being translated differently. Like if you had a number of items. and the translations were different.
"You have 0 items"
"You have 1 item"
One last question was how the project would handle plurals being translated differently. Like if you had a number of items. and the translations were different.
"You have 0 items"
"You have 1 item"
Re: Localization Formats
Right now, this is possible but it not very clean, for example using two styles depending on the value:
We should build something cleaner, implemented on translator side, without changing the XAML. As Unreal does.
Could you please report this?
Code: Select all
<TextBlock>
<TextBlock.Style>
<Style TargetType="TextBlock">
<Setter Property="Text" Value="{noesis:Loc ItemCountPlural}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Items.Count}" Value="1">
<Setter Property="Text" Value="{noesis:Loc ItemCountSingular}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
Code: Select all
"You have {NumCats} {NumCats}|plural(one=cat,other=cats)"
-
- burst2flame
- Posts: 31
- Joined:
Re: Localization Formats
Yes that would be awesome! Where do I report it, as a bug?
Another question I had, was how to use the extension with dynamic resource keys. For example, we might have a place where we want to show the title of a skill. In that case the two possible resource keys might be something like:
fireball-title
slam-title
I'd like to do something like:
<Label Content={noesis:Loc {Binding TitleKey}} />
But it doesn't seem to work, is there a way to make this work?
Another question I had, was how to use the extension with dynamic resource keys. For example, we might have a place where we want to show the title of a skill. In that case the two possible resource keys might be something like:
fireball-title
slam-title
I'd like to do something like:
<Label Content={noesis:Loc {Binding TitleKey}} />
But it doesn't seem to work, is there a way to make this work?
-
-
sfernandez
Site Admin
- Posts: 3363
- Joined:
Re: Localization Formats
We decided not to support that scenario, as the ViewModel could return the localized string directly in the bound property. So instead of using the Loc extension, you would do a normal binding:I'd like to do something like:
<Label Content={noesis:Loc {Binding TitleKey}} />
But it doesn't seem to work, is there a way to make this work?
Code: Select all
<Label Content="{Binding TitleKey"/>-
- burst2flame
- Posts: 31
- Joined:
Re: Localization Formats
Can you show me how I would accomplish this if the translations are in a resource dictionary?
-
-
sfernandez
Site Admin
- Posts: 3363
- Joined:
Re: Localization Formats
A ResourceDictionary has methods to access any resource defined there. For example, assuming you have a way to globally access the localization dictionary, your view model can do the following:
Code: Select all
public string Title => Localization.Resources[_titleKey];-
- burst2flame
- Posts: 31
- Joined:
Re: Localization Formats
That makes sense. I'm assuming that this property would need to send an IPropertyNotification for when the language or the title key changed to render correctly?
I ended up writing a converter so I could do this in the markup. Perhaps some people might want to use it the same way:
I ended up writing a converter so I could do this in the markup. Perhaps some people might want to use it the same way:
Code: Select all
public class LanguageConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values[0] is ResourceDictionary resourceDictionary && values[1] != null)
{
var key = values[1];
if (resourceDictionary.Contains(key))
{
return resourceDictionary[key];
}
}
return DependencyProperty.UnsetValue;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
Code: Select all
<Label Foreground="White" FontSize="40" >
<Label.Content>
<MultiBinding Converter="{StaticResource LanguageConverter}">
<Binding Path="SelectedLanguage.ResourceDictionary" />
<Binding Path="SelectedLanguageId" />
</MultiBinding>
</Label.Content>
</Label>
Who is online
Users browsing this forum: No registered users and 3 guests