burst2flame
Topic Author
Posts: 31
Joined: 11 Mar 2024, 18:04

Localization Formats

26 Oct 2024, 00:03

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!
 
burst2flame
Topic Author
Posts: 31
Joined: 11 Mar 2024, 18:04

Re: Localization Formats

29 Oct 2024, 00:00

Any word on this?
 
User avatar
sfernandez
Site Admin
Posts: 3363
Joined: 22 Dec 2011, 19:20

Re: Localization Formats

29 Oct 2024, 18:15

The localization tutorial seems straightforward, but I was wondering if there's a way to have it accept other formats like xliff or json.
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.
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?
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:
<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>
Also is there a way to switch fonts as well.
Yes, our LocExtension can point to any resource defined in the Source dictionary, even a FontFamily resource:
<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>
<TextBlock Text="Title" FontFamily="{noesis:Loc TheFont}"/>
 
burst2flame
Topic Author
Posts: 31
Joined: 11 Mar 2024, 18:04

Re: Localization Formats

20 Nov 2024, 20:25

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"
 
User avatar
jsantos
Site Admin
Posts: 4822
Joined: 20 Jan 2012, 17:18
Contact:

Re: Localization Formats

25 Nov 2024, 17:17

Right now, this is possible but it not very clean, for example using two styles depending on the value:
<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>
We should build something cleaner, implemented on translator side, without changing the XAML. As Unreal does.
"You have {NumCats} {NumCats}|plural(one=cat,other=cats)"
Could you please report this?
 
burst2flame
Topic Author
Posts: 31
Joined: 11 Mar 2024, 18:04

Re: Localization Formats

27 Nov 2024, 20:41

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?
 
User avatar
sfernandez
Site Admin
Posts: 3363
Joined: 22 Dec 2011, 19:20

Re: Localization Formats

28 Nov 2024, 19:18

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?
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:
<Label Content="{Binding TitleKey"/>
For reporting an issue you can register an account in our bugtracker.
 
burst2flame
Topic Author
Posts: 31
Joined: 11 Mar 2024, 18:04

Re: Localization Formats

02 Dec 2024, 19:54

Can you show me how I would accomplish this if the translations are in a resource dictionary?
 
User avatar
sfernandez
Site Admin
Posts: 3363
Joined: 22 Dec 2011, 19:20

Re: Localization Formats

03 Dec 2024, 11:18

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:
public string Title => Localization.Resources[_titleKey];
 
burst2flame
Topic Author
Posts: 31
Joined: 11 Mar 2024, 18:04

Re: Localization Formats

03 Dec 2024, 20:10

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:
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();
    }
}
          <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