Chris
Chris

Reputation: 3221

Kendo MVC Grid localisation for neutral cultures

We're using the Kendo UI ASP.NET MVC wrappers to render our grids. I've got localisation working as described in this article but the problem I have is that if the browser culture gets resolved as language neutral (just language level, for example "fr" instead of "fr-FR") the localisation doesn't work and the grid is rendered as "en-US".

Is there a simple way of making the wrappers use the localisations in this scenario?

I guess I could force the culture somehow and in the worst case compile the Kendo dll with additional resources but thought I would ask first.

Thanks for any pointers!

Upvotes: 0

Views: 112

Answers (1)

Chris
Chris

Reputation: 3221

Ok, so I've got a solution for now - I've made a culture switcher that I wrap in a using statement around the grid building code.

namespace Kendo.Mvc.UI
{
    public class KendoCultureSwitcher : IDisposable
    {
        private readonly CultureInfo _culture;
        private readonly List<string> _cultures = new List<string>()
        {
            "bg-BG",
            "cs-CZ",
            "da-DK",
            "de-DE",
            "es-ES",
            "fr-FR",
            "it-IT",
            "nl-NL",
            "pl-PL",
            "pt-PT", // Note before pt-BR
            "pt-BR",
            "ro-RO",
            "ru-RU",
            "sv-SE",
            "tr-TR",
            "uk-UA",
            "zh-CN"
        };

        public KendoCultureSwitcher()
        {
            _culture = CultureInfo.CurrentUICulture;

            var currentCultureName = _culture.ToString();
            if (!_cultures.Contains(currentCultureName, StringComparer.OrdinalIgnoreCase))
            {
                var switchCulture = _cultures.FirstOrDefault(c => c.StartsWith(currentCultureName, StringComparison.OrdinalIgnoreCase));
                if (switchCulture != null)
                {
                    Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(switchCulture);
                }
            }
        }

        public void Dispose()
        {
            if (Thread.CurrentThread.CurrentUICulture != _culture)
            {
                Thread.CurrentThread.CurrentUICulture = _culture;
            }
        }
    }
}

Upvotes: 0

Related Questions