Change language of localization of RadGridView at runtime

19 posts, 1 answers
  1. Li
    Li avatar
    46 posts
    Member since:
    Feb 2011

    Posted 02 May 2012 Link to this post

    Hello,

    I need to change the language of the localization of a gridview at runtime without creating an instance of a gridview (as shown in the examples). I know, that I have to adapt the templates for the RadGridView but before I start I wanted to ask whether there are any exisiting templates doing dynamic localization which could be provided or whether it is planned to support dynamic localization in a further Telerik version?

    Kind regards,
    Li
  2. Pavel Pavlov
    Admin
    Pavel Pavlov avatar
    2039 posts

    Posted 02 May 2012 Link to this post

    Hi Li,

    Please share which parts of RadGridView you need localized.

    All the best,
    Pavel Pavlov
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  3. DevCraft banner
  4. Li
    Li avatar
    46 posts
    Member since:
    Feb 2011

    Posted 03 May 2012 Link to this post

    Hi,

    we need to localize dynamically the filters, the grouppanel and the text for GridViewAlwaysVisibleNewRow.

    Kind regards,
    Li
  5. Answer
    Pavel Pavlov
    Admin
    Pavel Pavlov avatar
    2039 posts

    Posted 03 May 2012 Link to this post

    Hello Li,

    There are no existing templates for runtime localization without reloading the control . However this can be achieved using Blend. The workflow would be - modify the template of the part you are targeting, remove any occurrences of telerik:LocalizationManager.ResourceKey="XXXX" and bind the relevant texts with paths pointing to a ViewModel of yours.

    *Please have in mind that placing RadGridView in a container that reloads it when switching between languages would be the cheaper and the robust way.

    On your last question - for the time being we are not developing features to support  runtime switching between languages.

    Regards,
    Pavel Pavlov
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  6. Koen
    Koen avatar
    27 posts
    Member since:
    Apr 2012

    Posted 03 Jul 2012 Link to this post

    Hi Pavel,

    Is there any way to "reload" the grid, without having to create a new instance?
    So that the localization gets updated.


    Thanks,
    Koen
  7. Koen
    Koen avatar
    27 posts
    Member since:
    Apr 2012

    Posted 05 Jul 2012 Link to this post

    For future reference, I implemented dynamic language switching on RadGridViews , as below.

    Notes:
    - you'll need some sort of event to notify the grid that the culture has changed
    - you'll need the "VisualChildren" method that iterates the visual tree starting from a given element. 


    private void OnApplicationCultureChanged()
    {
        var gridChildElements =
    this.VisualChildren<DependencyObject>();
        foreach (var gridChildElement in gridChildElements)
        {
            // Force a property-change of the ResourceKey dependency property              
            var resourceKey = LocalizationManager.GetResourceKey(gridChildElement);
            if (resourceKey != null)
            {
                LocalizationManager.SetResourceKey(gridChildElement, null);
                LocalizationManager.SetResourceKey(gridChildElement, resourceKey);
            }
        }
    }
  8. Pavel Pavlov
    Admin
    Pavel Pavlov avatar
    2039 posts

    Posted 09 Jul 2012 Link to this post

    Hello Koen ,

    Thanks for sharing this hack !  Although it does not use  an official API  it might be quite useful for the community. Here is a small addition from me : 
    The VisualChildren method can be actually replaced by the Telerik's ChildrenOfType<> extension method. 

    * You will just need to add an "using Telerik.Windows.Controls" directive.

    One more time thanks for your contribution! I am updating your telerik points.

    Greetings,
    Pavel Pavlov
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  9. Uwe
    Uwe avatar
    48 posts
    Member since:
    Sep 2012

    Posted 23 Oct 2012 Link to this post

    Hello can some one give me a hit how the "VisualChildren" method looks like?
    Greetings Uwe
  10. Koen
    Koen avatar
    27 posts
    Member since:
    Apr 2012

    Posted 23 Oct 2012 Link to this post

    According to Pavel, Telerik has it's own extension method to do this: 
    Telerik.Windows.Controls.ChildrenOfTypeExtensions.ChildrenOfType<>.

    So you can replace the call 
    this.VisualChildren<DependencyObject>();

    with:

    this.ChildrenOfType<DependencyObject>();


    Regards,
    Koen

  11. Uwe
    Uwe avatar
    48 posts
    Member since:
    Sep 2012

    Posted 23 Oct 2012 Link to this post

    Hi Koen, thanks for helping,

    ...ok, I do this:

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        // Just for testing
        LocalizationManager.DefaultResourceManager = new ResourceManager("FuelOPTBA20.Properties.Resources", Assembly.GetCallingAssembly());
        var GridChildElements = this.ChildrenOfType<DependencyObject>();
        foreach (var GridChildElement in GridChildElements)
        {
            // Have a look to the elemnts
            Debug.WriteLine(GridChildElement.ToString());
            // Force a property-change of the ResourceKey dependency property           
            var resourceKey = LocalizationManager.GetResourceKey(GridChildElement);
            if (resourceKey != null)
            {
                LocalizationManager.SetResourceKey(GridChildElement, null);
                LocalizationManager.SetResourceKey(GridChildElement, resourceKey);
            }
        }
    }



    but I get a System.Resources.MissingManifestResourceException

    Best Regards
  12. Koen
    Koen avatar
    27 posts
    Member since:
    Apr 2012

    Posted 24 Oct 2012 Link to this post

    Are you sure you are initializing the ResourceManager correctly?
    Maybe try using the  "new ResourceManager(Type resourceSource)" ctor.

    Regards,
    Koen
  13. Marc Roussel
    Marc Roussel avatar
    429 posts
    Member since:
    Dec 2009

    Posted 02 Jul 2013 Link to this post

    I tried the method above and it doesn't work.
    It doesn't change the language.

    The only place I can make it work is having Thread.CurrentThread.CurrentCulture = new CultureInfo("fr") in the Application_Startup but since the user can change language at runtime it's not viable.

    Anything new in Q2 2013 about this ?
  14. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 04 Jul 2013 Link to this post

    Hi,

    In order to apply localization for the GridView at runtime, you should recreate it. 
     

    Regards,
    Didie
    Telerik
    TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for SILVERLIGHT.
    Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
    Sign up for Free application insights >>
  15. Marc Roussel
    Marc Roussel avatar
    429 posts
    Member since:
    Dec 2009

    Posted 04 Jul 2013 Link to this post

    Well, this is a problem since we keep the language the user selected in his/her last session in the Isolated Storage then when the user starts the app, in Application_Startup, the information of the language isn't available at that point since we need the app to be loaded then we need to authenticate the user and when we know who is there we get his/her isolated storage information about the language and then we set the language of the application but at this point recreating the grid is not viable since it's already created and loaded.  Do you have any alternative for that ?  Any suggestion ? Anything easy that would put the grid in the localization according to the language of the user getting in ?

  16. Uwe
    Uwe avatar
    48 posts
    Member since:
    Sep 2012

    Posted 04 Jul 2013 Link to this post

    Hi, Marc,
    I’m using the language changing at runtime in a MVVM environment and it works fine. The user selection of the language is in one ViewModel  and View and the Grid is in another ViewModel and View, so the condition which Pavel requested is satisfied. Maybe this is a solution.
    Greetings Uwe
  17. Koen
    Koen avatar
    27 posts
    Member since:
    Apr 2012

    Posted 04 Jul 2013 Link to this post

    Hi Mark,

    I've tested the work-around I posted last year, with the latest 2013 Q2 libraries.
    It's still a valid approach as it still works.

    I've created a sample project on github to show you this behavior:
    https://github.com/koenj/Telerik.RadGridView.DynamicCultures

    A couple of things to notice, as done in this sample project:
    - you need some kind of EventAggregator to notify your grid that the application culture has changed
    - you need to set the "SupportedCultures" tag in the csproj file


    If you have any more questions, feel free to contact me.
    Regards,
    Koen
  18. Koen
    Koen avatar
    27 posts
    Member since:
    Apr 2012

    Posted 04 Jul 2013 Link to this post

    Hi Mark,

    I've tested the work-around I posted last year, with the latest 2013 Q2 libraries.
    It's still a valid approach as it still works.

    I've created a sample project on github to show you this behavior:
    https://github.com/koenj/Telerik.RadGridView.DynamicCultures

    A couple of things to notice, as done in this sample project:
    - you need some kind of EventAggregator to notify your grid that the application culture has changed
    - you need to set the "SupportedCultures" tag in the csproj file


    If you have any more questions, feel free to contact me.
    Regards,
    Koen
  19. Marc Roussel
    Marc Roussel avatar
    429 posts
    Member since:
    Dec 2009

    Posted 04 Jul 2013 Link to this post

    Awesome.  Huge thank you !
  20. Camille
    Camille avatar
    1 posts
    Member since:
    Sep 2015

    Posted 12 May in reply to Koen Link to this post

    Hi all,

    I had trouble to change Telerik controls' language at runtime too. Based on the proposed solution in this discussion and Telerik documentation: Localization Using Custom Localization Manager I created yet another solution to make the process a simple as I could imagine.

    Requirements

    •  LocalizationService class which raises the event LanguageChanged (it is the equivalent of EventAggregator I guess) andhas a Translate() method.
    • The LanguageChanged event arguments contains the new CultureInfo.

    Code

    The TelerikLocalizationManeger derives from Telerik.Windows.Controls.LocalizationManager. It is used to update its Culture (of type CultureInfo) and is responsible to refresh controls to localize:

    using System;
    using System.Collections.Generic;
    using System.Windows;
    using Telerik.Windows.Controls;
     
    namespace CanmetEnergy.Localization.UI
    {
        public class TelerikLocalizationManager : LocalizationManager, IDisposable
        {
            /// <summary>The list of Telerik controls to localize.</summary>
            private readonly IList<DependencyObject> _managedControls = new List<DependencyObject>();
     
            public TelerikLocalizationManager()
            {
                LocalizationService.LanguageChanged += LocalizationService_LanguageChanged;
            }
     
            /// <summary>
            /// Registers a control to localize at runtime.
            /// </summary>
            /// <param name="control">The control to localize.</param>
            public void Register(DependencyObject control)
            {
                _managedControls.Add(control);
            }
      
            /// <summary>
            /// Changes the culture of the localization manager and localize all registered controls.
            /// </summary>
            public void LocalizationService_LanguageChanged(object sender, CultureInfoEventArgs e)
            {
                Culture = e.CultureInfo;
     
                foreach (var control in _managedControls)
                {
                    Refresh(control);
                }
            }
     
            /// <summary>
            /// Refresh localization of <paramref name="control"/>.
            /// </summary>
            /// <param name="control">The control to (re)localize.</param>
            private void Refresh(DependencyObject control)
            {
                var gridChildren = control.ChildrenOfType<DependencyObject>();
                foreach (var gridChild in gridChildren)
                {
                    var resourceKey = GetResourceKey(gridChild);
                    if (resourceKey != null)
                    {
                        SetResourceKey(gridChild, null);
                        SetResourceKey(gridChild, resourceKey);
                    }
                }
            }
     
            public override string GetStringOverride(string key)
            {
                var translation = LocalizationService.Translate(key);
                return translation != null ? translation : base.GetStringOverride(key);
            }
     
            public void Dispose()
            {
                LocalizationService.LanguageChanged -= LocalizationService_LanguageChanged;
            }
        }
    }

    Initialization

    You must initialize Telerik localization system at the startup of your application:

    Telerik.Windows.Controls.LocalizationManager.Manager = new TelerikLocalizationManager();
    Telerik.Windows.Controls.LocalizationManager.UseDynamicLocalization = true;

    Declare control to refresh

    In your view code behind, you can register controls you want to localize at runtime:

    public partial class MyView : UserControl
        {
            public MyView()
            {
                InitializeComponent();
     
                // _mygridView is a RadGridView declared in the XAML file.
                // It could be any kind of Telerik control as soon as it is a DependencyObject
                (LocalizationManager.Manager as TelerikLocalizationManager)?.Register(_myGridView);
            }
        }

    I hope this helped. Thank you.

    Camille

Back to Top
DevCraft banner