3

I noticed jQuery has one locale for Spanish which is es. When you change your browser to a spanish locale which can be es, es-US, es-MX, etc, since there is only one jQuery spanish file, it only loads the file if the locale is es. Is there a way to convert between the locales that .NET returns to you using the CultureInfo object to the ones that jQuery expects?

Here is scenario to better explain my situation. If I change my browsers language to es, CultureInfo.CurrentCulture returns es-ES, however, jQuery does not contain the locale es-ES, it just has es, but in certain cases it does have something like en-US and en-GB, so what is the best way to go about this. Should I just rename locale files or do some sort of conversion?

user2864740
  • 60,010
  • 15
  • 145
  • 220
xaisoft
  • 3,343
  • 8
  • 44
  • 72
  • Doesn't jQuery automatically load the `es` locale if the `es-US` is requested but not present? – Dirk Feb 12 '15 at 20:27
  • @Dirk - I tried this and it does not appear to be doing it. I set my browser language to `es` and `CurrentCulture` returned `es-ES`, so the only way I got it to work was to have an `es-ES` jquery script. – xaisoft Feb 12 '15 at 22:02

1 Answers1

0

Living in Belgium we have similar problems for french, dutch, english and combinations of these cultures.

For my datepicker setup I came up with the idea of using the html tag as culture placeholder, feeded from CultureInfo.CurrentCulture or something server side.

<html lang="nl-BE" />

Then in JavaScript I first detect the current culture and see if it's valid according to a list and a default culture. I therefor split the language, and see if the language is available in my available cultures. If not I use the default one.

// var cfg.options -> $.extend to this.settings
options: {
    ...
    defaultCulture: 'en-GB',
    validCultures: ['en-GB', 'fr', 'fr-BE', 'nl-BE', 'nl'],
    ...
},

getCulture: function () {
    var settings = this.settings,
        cache = settings.cache,
        dynamicProp = settings.dynamicProp,
        options = settings.options,
        culture = $(cache.currentCulture).prop(dynamicProp.culture), // $('html').prop('lang')
        language;

    if ($.inArray(culture, options.validCultures) === -1) {
        language = culture.split('-')[0];
        culture = $.inArray(language, options.validCultures) === -1 ? options.defaultCulture : language;
    }

    return culture;
},

After my wicked implementation skills the customer asked to change the dates for fr-BE and I ended up creating a custom culture file in jQuery UI :s That was the easiest solution.

I believe it all depends on how custom your cultures have to be setup. If the data/translations provided by jQuery UI is enough to meet requirements, I wouldn't create custom cultures.

Tim Vermaelen
  • 6,869
  • 1
  • 25
  • 39