balazs-endresz / jquery-translate

Automatically exported from code.google.com/p/jquery-translate
5 stars 5 forks source link

Could translate.ui present the languages in the current browser's language? #30

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Use translate.ui with a browser that has 'es' as its language code
2. translate.ui offers 'Spanish' as a possible language to translate to.

I would hope to see 'español' as the language to translate to... and all
the other languages in the select translated to Spanish.

Is there any way to do this with Google's api?

What version of the product are you using? On what operating system? 1.4.5

Thanks for the marvelous library.

BTW: This is used in the Drupal translatableregions module:
http://drupal.org/project/translatableregions.

Please add that to your list of users. It's also used on the site
http://warmshowers.org

Original issue reported on code.google.com by ra...@randyfay.com on 23 Feb 2010 at 10:53

GoogleCodeExporter commented 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

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago

Original comment by balazs.endresz on 14 Mar 2010 at 9:17