none
Suggestion how to localize templates XAML

    Question

  • Hi,

    my application is composed by a main window which loads in its client area different XAML for different customers.
    The directory structure of the program is:

    MyProgram
         |- Templates
                  |- Customer A
                  |- Customer B

    in the Customer folders there is the XAML file to load dynamically.
    Now, which is the best way to localize the XAML without to touch the main application?

    I don't want to store the strings into the application resource file because I don't want to rebuild it every time I have to made a change to a customer GUI.

    Thanks in advance for the suggestions!

    Daniele.

    Tuesday, August 27, 2013 7:57 AM

Answers

  • well a quick example to hopefully get you started. situation is need converter to read security permissions to determine whether or not to show elements.

    c# method to check security returning bool

        public class Security
        {
            public static bool HasPermission(string setting)
            {
                return CheckPermission(setting, null);
            }
    .....
    
    

    converter to use this with parameter

        public class PermissionConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                Visibility result = Visibility.Collapsed;
                if (parameter == null) return Visibility.Collapsed;
                if (parameter is string)
                {
                    var ps = ((string)parameter).Split(';');
                    if (ps.Length > 1)
                    {
                        if (Security.HasPermission(ps[0], ps[1]))
                            result = Visibility.Visible;
                    }
                    else if (Security.HasPermission(ps[0]))
                        return Visibility.Visible;
                }
                return result;
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }

    and useage in xaml

      ...  
    <Window.Resources>
            <local:PermissionConverter x:Key="PermissionConverter" />
        </Window.Resources>
    ...
    <TextBoxName="txt1" Visibility="{Binding Converter={StaticResource PermissionConverter}, ConverterParameter=Viewtxt1}"
    ...

    obviously implement own converter and methods but the one above allows you to pass 2 parameters seperated by ; character.

    hope that helps you get started.

    cheers

    andy

    • Marked as answer by DBarzo Wednesday, August 28, 2013 7:42 AM
    Tuesday, August 27, 2013 3:41 PM
  • create a separate dll and place all your resources in here. then if you rebuild for each different culture you support you can deploy these with your application and select the appropriate one at runtime.  If you then want to change these resource files they can be deployed separately in the same way you are deploying the customer GUI's.

    andy

    • Marked as answer by DBarzo Tuesday, August 27, 2013 12:00 PM
    Tuesday, August 27, 2013 8:24 AM
  • Tuesday, August 27, 2013 11:30 AM

All replies

  • create a separate dll and place all your resources in here. then if you rebuild for each different culture you support you can deploy these with your application and select the appropriate one at runtime.  If you then want to change these resource files they can be deployed separately in the same way you are deploying the customer GUI's.

    andy

    • Marked as answer by DBarzo Tuesday, August 27, 2013 12:00 PM
    Tuesday, August 27, 2013 8:24 AM
  • Thanks for the reply.
    I thought quite same thing...but..

    select the appropriate one at runtime.

    How can I do it?
    Do you have some starting point I can read about?

    Regards,
    Daniele.

    Tuesday, August 27, 2013 9:06 AM
  • Tuesday, August 27, 2013 11:30 AM
  • Thanks a lot.

    Cheers,
    Daniele.

    Tuesday, August 27, 2013 12:00 PM
  • Just only one thing, if I can..

    Searching around I found a question on StackOverflow which is exactly what I'm thinking to do...but the user does not have received an answer:

    http://stackoverflow.com/questions/15520579/xaml-bind-to-static-method-with-parameters

    Is possible to do such thing without to create and ObjectDataProvider for every controls I have to translate?

    Cheers,
    Daniele.

    Tuesday, August 27, 2013 12:30 PM
  • Hi Daniele,

    if you are just wanting to access a resource from your resources file bases on the localization setting then you can access that direct in the xaml and just make sure in each of the different languages/cultures they are called the same thing and it will pick up the correct string based on what dll loaded.

    if you are wanting to call methods in xaml I would say that the best way might be a converter and converter parameter, then within the c# code of the converter you can call a method return the appropriate object based on that.  I am not sure of any clean way to answer that question.

    andy

    Tuesday, August 27, 2013 1:28 PM
  • Hi Andy,
    thanks for your reply!
    I start to learn WPF too late in my career! :-P

    if you are just wanting to access a resource from your resources file bases on the localization setting then you can access that direct in the xaml and just make sure in each of the different languages/cultures they are called the same thing and it will pick up the correct string based on what dll loaded.

    I know, but, while in the main window (compiled inside the application) I use them directly:

    ..
    xmlns:props="clr-namespace:MyApplication.Properties"
    ..
    <TextBlock Text="{x:Static props:Resources.BTN_OPEN}" />

    If I made the same things into my dynamic loaded XAML I get a parse error.
    In such file I declared the namespace like:

    xmlns:props="clr-namespace:MyApplication.Properties;assembly=MyApplication"

    Where I wrong?

    if you are wanting to call methods in xaml I would say that the best way might be a converter and converter parameter, then within the c# code of the converter you can call a method return the appropriate object based on that.  I am not sure of any clean way to answer that question.

    Ok, I'll take a look! :-)

    Thanks a again,
    Daniele.

    Tuesday, August 27, 2013 2:06 PM
  • its never too late lol

    sorry but I have never tried to load controls like you are doing, but I assume that its something to do with the dynamically loaded dll trying to access properties in your main assembly. you should have your resources in a separate dll that is accessed by the dll with the controls.

    sorry not much help

    andy

    Tuesday, August 27, 2013 2:18 PM
  • its never too late lol

    :-)

    sorry but I have never tried to load controls like you are doing, but I assume that its something to do with the dynamically loaded dll trying to access properties in your main assembly. you should have your resources in a separate dll that is accessed by the dll with the controls.

    sorry not much help

    Don't worry Andy, a forum is a place to ask, not to pretend help! ;-)
    Instead thanks for your support!

    Maybe if I use converters and converter parameter will do the trick (also if, for now, I have no idea how to implement them :-P )

    Cheers,
    Daniele.

    Tuesday, August 27, 2013 3:27 PM
  • well a quick example to hopefully get you started. situation is need converter to read security permissions to determine whether or not to show elements.

    c# method to check security returning bool

        public class Security
        {
            public static bool HasPermission(string setting)
            {
                return CheckPermission(setting, null);
            }
    .....
    
    

    converter to use this with parameter

        public class PermissionConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                Visibility result = Visibility.Collapsed;
                if (parameter == null) return Visibility.Collapsed;
                if (parameter is string)
                {
                    var ps = ((string)parameter).Split(';');
                    if (ps.Length > 1)
                    {
                        if (Security.HasPermission(ps[0], ps[1]))
                            result = Visibility.Visible;
                    }
                    else if (Security.HasPermission(ps[0]))
                        return Visibility.Visible;
                }
                return result;
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }

    and useage in xaml

      ...  
    <Window.Resources>
            <local:PermissionConverter x:Key="PermissionConverter" />
        </Window.Resources>
    ...
    <TextBoxName="txt1" Visibility="{Binding Converter={StaticResource PermissionConverter}, ConverterParameter=Viewtxt1}"
    ...

    obviously implement own converter and methods but the one above allows you to pass 2 parameters seperated by ; character.

    hope that helps you get started.

    cheers

    andy

    • Marked as answer by DBarzo Wednesday, August 28, 2013 7:42 AM
    Tuesday, August 27, 2013 3:41 PM
  • Thanks, thanks and thanks again! :-)

    Now I try....and, in case, I will ask again!

    Cheers,
    Daniele.


    • Edited by DBarzo Tuesday, August 27, 2013 3:46 PM
    Tuesday, August 27, 2013 3:45 PM