3

Silverlight Toolkit has a resource file named Resources.resx, which contains "On" and "Off" string states for the switch. But when I have added a corresponding localized resource, Resources.ru-RU.resx, it wasn't picked up by the localization (though the similar approach works for my own resources).

One way to do it is to create my own binding for the ToggleSwitch Content, but I was hoping for a non-coding solution. Possible?

Sergey Aldoukhov
  • 22,316
  • 18
  • 72
  • 99

2 Answers2

6

Localize ToggleSwitch in Silverlight Toolkit can be achived via DataTemplate

    <toolkit:ToggleSwitch x:Name="MySwitch" Header="Localized Switch">  
        <toolkit:ToggleSwitch.ContentTemplate>  
            <DataTemplate>  
                <ContentControl HorizontalAlignment="Left"   
                    Content="{Binding Converter={StaticResource Switch}}"/>  
            </DataTemplate>  
        </toolkit:ToggleSwitch.ContentTemplate>  
    </toolkit:ToggleSwitch>

Declare an ValueConverter:

    public class BoolToSwitchConverter : IValueConverter  
    {  
        private string FalseValue = Resources.Off;  
        private string TrueValue  = Resources.On;  

        public object Convert(object value, Type targetType, object parameter,
              System.Globalization.CultureInfo culture)  
        {  
            if (value == null)  
                return FalseValue;  
            else  
                return ("On".Equals(value)) ? TrueValue : FalseValue;  
        }  

        public object ConvertBack(object value, Type targetType, 
               object parameter, System.Globalization.CultureInfo culture)  
        {  
            return value != null ? value.Equals(TrueValue) : false;  
        }  
    }

More details here.

Ernest Poletaev
  • 558
  • 5
  • 15
2

You need to name your resource file ControlResources.ru-RU.resx , see Andrej Torzen's article on the subject.

Claus Jørgensen
  • 25,882
  • 9
  • 87
  • 150
  • But it does not. Maybe I should do something else besides creating Resources.ru-RU.resx – Sergey Aldoukhov Sep 01 '11 at 07:30
  • Oh right, the name needs to be ControlResources.ru-RU.resx, not Resources, see http://tozon.info/blog/post/2011/08/18/Localizing-the-August-2011-Windows-Phone-Toolkit.aspx , if it works I'll update the answer with it. – Claus Jørgensen Sep 01 '11 at 07:50
  • This works for everything except for "On"/"Off" labels for ToggleSwitch - by some reason they are not in the ControlResources.resx file, but in Resources.resx, which is in "Properties" folder. Maybe Jeff thought "On"/"Off" shouldn't be localized? Anyway, good answer. – Sergey Aldoukhov Sep 02 '11 at 05:24
  • You could always recompile the SL Toolkit yourself, with that fix. Or create a bug-report on codeplex. – Claus Jørgensen Sep 02 '11 at 05:50
  • 1
    By the way this was filed as a bug on CodePlex (http://silverlight.codeplex.com/workitem/10164) and I've recently checked in a fix for the ToggleSwitch resources not being in the correct location. – Shawn Oster Apr 27 '12 at 21:37