Roest
Topic Author
Posts: 35
Joined: 15 Jan 2020, 15:30

translation resource directionary

09 Nov 2020, 11:37

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.

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

Re: translation resource directionary

09 Nov 2020, 13:17

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?
 
Roest
Topic Author
Posts: 35
Joined: 15 Jan 2020, 15:30

Re: translation resource directionary

09 Nov 2020, 13:28

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

Re: translation resource directionary

09 Nov 2020, 13:36

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.
 
Roest
Topic Author
Posts: 35
Joined: 15 Jan 2020, 15:30

Re: translation resource directionary

09 Nov 2020, 14:33

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

Re: translation resource directionary

09 Nov 2020, 14:49

The Localization sample is also available in C++.
 
Roest
Topic Author
Posts: 35
Joined: 15 Jan 2020, 15:30

Re: translation resource directionary

09 Nov 2020, 14:59

Thanks a lot.
 
Roest
Topic Author
Posts: 35
Joined: 15 Jan 2020, 15:30

Re: translation resource directionary

09 Nov 2020, 15:51

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

Re: translation resource directionary

09 Nov 2020, 16:42

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;
}
 
Roest
Topic Author
Posts: 35
Joined: 15 Jan 2020, 15:30

Re: translation resource directionary

09 Nov 2020, 18:03



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

Who is online

Users browsing this forum: Google [Bot] and 1 guest