Closed GoogleCodeExporter closed 9 years ago
AFAIK it's safer to inspect the http Accept-Language header but this might work
too:
var lang = navigator.language || navigator.browserLanguage ||
navigator.systemLanguage || navigator.userLanguage;
lang = $.translate.toLanguageCode(lang) || lang.substr(0,2);
//$.translate.languageCodeMap can be used as well if the language code is not
recognized by the Language API
//e.g. $.translate.languageCodeMap["en-GB"] = "en"; lang =
$.translate.toLanguageCode(lang);
then use the ui extension with a modified `label` option so the languages will
be displayed in English:
$.translate.ui({
tags: ["select", "option"],
filter: $.translate.isTranslatable,
label: function(langCode, language){ return $.translate.capitalize(language); },
//label: $.translate.toNativeLanguage, //default
includeUnknown: false
})
.val(lang)
.prependTo('body');
then translate it:
$(".jq-translate-ui").translate("en", lang);
Original comment by balazs.endresz
on 24 Feb 2010 at 9:04
Wow, your help is much appreciated.
For starters, the core problem was that I hadn't included the native extension
when I
built it - didn't know I needed it.
I actually wanted what I think you say is the default behavior (show labels in
browser language) but it doesn't to behave that way with just $.translate.ui()
You showing me how to get the lang helps too, as I was doing some
less-than-elegant
things to work around the pt-BR issue and various things like that.
Here's what worked great for me:
var lang = navigator.language || navigator.browserLanguage ||
navigator.systemLanguage || navigator.userLanguage;
lang = $.translate.toLanguageCode(lang) || lang.substr(0,2);
// lang="es";
//$.translate.languageCodeMap can be used as well if the language code is
not recognized by the Language API
//e.g. $.translate.languageCodeMap["en-GB"] = "en"; lang =
$.translate.toLanguageCode(lang);
//then use the ui extension with a modified `label` option so the languages
will be displayed in English:
$.translate.ui({
tags: ["select", "option"],
filter: $.translate.isTranslatable,
label: $.translate.toNativeLanguage, //default
includeUnknown: false
})
.val(lang)
.change(function(){ //when selecting another language
translate_element(element, $(this).val());
})
.prependTo($(this))
Original comment by ra...@randyfay.com
on 24 Feb 2010 at 5:07
Sorry, what I wrote was incorrect regarding the ui thing, it IS displayed in
English by
default, only if you include the `native` extension does it show each language
localized. So you don't have to change the ui function to translate the
dropdown to
another language.
Original comment by balazs.endresz
on 24 Feb 2010 at 5:50
That wasn't my experience... I had to make the changes I show. But I could have
easily been confused.
Thanks so much for your help.
Original comment by ra...@randyfay.com
on 24 Feb 2010 at 5:57
Original comment by balazs.endresz
on 14 Mar 2010 at 9:17
Original issue reported on code.google.com by
ra...@randyfay.com
on 23 Feb 2010 at 10:53