Page 1 of 2

translation resource directionary

Posted: 09 Nov 2020, 11:37
by Roest
How would you suggest handling translations. It seems the recommended way is a resource dictionary and something like
<TextBlock Text="{StaticResource prefix-key}"></TextBlock>
Now this works well enough for all static text but a lot of text in a game is dynamically supplied via binding. It seems to be impossible to trigger resource lookups via binding. So this leads to having two sperate translation tables in two places. Not an optimal solution. Is there anything that would support a better solution.

Kind of in the same category is my other problem, giving a button a custom color via binding. Something I haven't been able to figure out. Of course it is possible to work with triggers. On a table that can have 4 colors I can work with something like this
<Style.Triggers>
			<DataTrigger Binding="{Binding Activity}" Value="N">
				<Setter Property="Background" Value="{StaticResource ActivityNoneBrush}"/>
			</DataTrigger>
			<DataTrigger Binding="{Binding Activity}" Value="E">
				<Setter Property="Background" Value="{StaticResource ActivityEatBrush}"/>
			</DataTrigger>
			<DataTrigger Binding="{Binding Activity}" Value="S">
				<Setter Property="Background" Value="{StaticResource ActivitySleepBrush}"/>
			</DataTrigger>
			<DataTrigger Binding="{Binding Activity}" Value="T">
				<Setter Property="Background" Value="{StaticResource ActivityTrainBrush}"/>
			</DataTrigger>
		</Style.Triggers>
But imagine this table


Every column is supposed to have their own color and the buttons are toggleable. That would create about 50-60 triggers per button for a few hundred buttons. I don't believe that is feasible. Since those buttons already have their own classes and bindings in the model the easiest way would be to provide a color binding but that doesn't seem to exist in XAML.

Re: translation resource directionary

Posted: 09 Nov 2020, 13:17
by sfernandez
For translation, if the text comes from a Binding, won't be possible to provide the translated text directly from the view model? Or maybe the view model is just returning a text ID you want to translate? In that case you can use a Converter that gets that ID and translates to the appropriate text:
<TextBlock Text="{Binding Category, Converter={StaticResource TranslateConverter}}"/>
And for the brushes the best option here is also using a Converter:
<Button Background="{Binding Activity, Converter={StaticResource ActivityBrushConverter}}"/>
Have you tried something like that?

Re: translation resource directionary

Posted: 09 Nov 2020, 13:28
by Roest
For translation, if the text comes from a Binding, won't be possible to provide the translated text directly from the view model?
Yes that is possible and is the current way but that means you have two separate lookup sources.
... converters ...
No I haven't, guess I really need to look into converters then. Are there any examples for these use cases?

Re: translation resource directionary

Posted: 09 Nov 2020, 13:36
by sfernandez
Our Localization sample uses a converter to change the foreground color of some sliders depending on their value:
MainWindow.xaml
LevelToColorConverter.cs

You can have something similar but returning a Brush object instead of a Color, based on the Activity value.

Re: translation resource directionary

Posted: 09 Nov 2020, 14:33
by Roest
Our Localization sample uses a converter to change the foreground color of some sliders depending on their value:
MainWindow.xaml
LevelToColorConverter.cs
Sadly that doesn't help much as I'm in the C++ side of things and not in Unity.

Re: translation resource directionary

Posted: 09 Nov 2020, 14:49
by jsantos
The Localization sample is also available in C++.

Re: translation resource directionary

Posted: 09 Nov 2020, 14:59
by Roest
Thanks a lot.

Re: translation resource directionary

Posted: 09 Nov 2020, 15:51
by Roest
Ok I think I'm close to the solution.

So this would be the converter
    bool TryConvert( BaseComponent* value, const Noesis::Type* targetType, BaseComponent*, Noesis::Ptr<BaseComponent>& result )
    {
        if ( targetType == Noesis::TypeOf<Noesis::Brush>() )
        {
            auto col = Noesis::Color( 255, 0, 255 );
	    result = Noesis::Boxing::Box( Noesis::SolidColorBrush( col ) );
            return true;
        }

        return false;
    }
    
Just some purple test but it fails to compile because of the deleted copy constructor in Noesis::SolidColorBrush. Since you said it would be possible to return a Brush instead of a color what am I missing here?

Re: translation resource directionary

Posted: 09 Nov 2020, 16:42
by sfernandez
SolidColorBrush is a BaseComponent so you have to return it without boxing:
bool TryConvert( BaseComponent* value, const Noesis::Type* targetType, BaseComponent*, Noesis::Ptr<BaseComponent>& result )
{
     if (targetType == Noesis::TypeOf<Noesis::Brush>())
     {
         result = MakePtr<Noesis::SolidColorBrush>(Noesis::Color( 255, 0, 255 ));
         return true;
     }
     
     return false;
}

Re: translation resource directionary

Posted: 09 Nov 2020, 18:03
by Roest


Was a difficult birth as usual and more than I like but in the end it works.