This is a migrated thread and some comments may be shown as answers.

Change language of localization of RadGridView at runtime

19 Answers 907 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Li
Top achievements
Rank 1
Li asked on 02 May 2012, 10:06 AM
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

19 Answers, 1 is accepted

Sort by
0
Pavel Pavlov
Telerik team
answered on 02 May 2012, 10:25 AM
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 >>

0
Li
Top achievements
Rank 1
answered on 03 May 2012, 08:34 AM
Hi,

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

Kind regards,
Li
0
Accepted
Pavel Pavlov
Telerik team
answered on 03 May 2012, 01:35 PM
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 >>

0
Koen
Top achievements
Rank 1
answered on 03 Jul 2012, 06:26 PM
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
0
Koen
Top achievements
Rank 1
answered on 05 Jul 2012, 10:09 AM
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);
        }
    }
}
0
Pavel Pavlov
Telerik team
answered on 09 Jul 2012, 07:37 AM
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 >>

0
Uwe
Top achievements
Rank 1
answered on 23 Oct 2012, 11:34 AM
Hello can some one give me a hit how the "VisualChildren" method looks like?
Greetings Uwe
0
Koen
Top achievements
Rank 1
answered on 23 Oct 2012, 11:47 AM
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

0
Uwe
Top achievements
Rank 1
answered on 23 Oct 2012, 12:40 PM
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
0
Koen
Top achievements
Rank 1
answered on 24 Oct 2012, 06:38 AM
Are you sure you are initializing the ResourceManager correctly?
Maybe try using the  "new ResourceManager(Type resourceSource)" ctor.

Regards,
Koen
0
Marc Roussel
Top achievements
Rank 2
answered on 02 Jul 2013, 03:28 PM
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 ?
0
Dimitrina
Telerik team
answered on 04 Jul 2013, 12:29 PM
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 >>
0
Marc Roussel
Top achievements
Rank 2
answered on 04 Jul 2013, 12:44 PM
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 ?

0
Uwe
Top achievements
Rank 1
answered on 04 Jul 2013, 01:27 PM
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
0
Koen
Top achievements
Rank 1
answered on 04 Jul 2013, 02:24 PM
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
0
Koen
Top achievements
Rank 1
answered on 04 Jul 2013, 02:27 PM
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
0
Marc Roussel
Top achievements
Rank 2
answered on 04 Jul 2013, 02:46 PM
Awesome.  Huge thank you !
0
Camille
Top achievements
Rank 1
answered on 12 May 2016, 05:38 PM

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

0
Fernando
Top achievements
Rank 1
answered on 20 Sep 2020, 11:26 PM

Fantastic! Still works!

 

For those in VB.Net, the method transfer as (plus I added a try catch and use 'Using')

       Private Sub OnApplicationCultureChanged()
        Try
            Dim gridChildElements = Me.DataGridServiceCallStatus.ChildrenOfType(Of DependencyObject)()
            For Each gridChildElement As Object In gridChildElements
                'Force a property-change of the ResourceKey dependency property              
                Using ResourceKey = LocalizationManager.GetResourceKey(gridChildElement)
                    If ResourceKey IsNot Nothing Then
                        LocalizationManager.SetResourceKey(gridChildElement, Nothing)
                        LocalizationManager.SetResourceKey(gridChildElement, ResourceKey)
                    End If
                End Using
            Next
        Catch ex As Exception
            'add your action here
        End Try
    End Sub

Tags
GridView
Asked by
Li
Top achievements
Rank 1
Answers by
Pavel Pavlov
Telerik team
Li
Top achievements
Rank 1
Koen
Top achievements
Rank 1
Uwe
Top achievements
Rank 1
Marc Roussel
Top achievements
Rank 2
Dimitrina
Telerik team
Camille
Top achievements
Rank 1
Fernando
Top achievements
Rank 1
Share this question
or