java - Detecting browser locales/languages and generating output -
we have java app renders localized text on user’s browser session based upon browser language settings. app reads browser language settings [set within header comes in request app] , prepares localized text. faced issues mozilla 5.0 version browser. note our code works fine in ie. taking example 'ja' language expecting browser send accepted language 'ja-jp' [which ie does] - unfortunately mozilla (ff) not - sends browser accepted language 'ja'. land in generating content using default language.
since providing fix same - map [language code language-country code] - 'ja' 'ja-jp' , create new locale [only if 2 digits language code present after in request] - question other browsers
chrome
safari
etc
language formats send within headers?
having array
ja-jp = ja-jp
ja = ja-jp
, mapping browser language language-country code resolve issue. there specific constraint need address – there languages spoken in multiple locations - if how deal it?
other thing need pay attention to?
in advance.
look @ java.util.resourcebundle class, getbundle() method. (you can use this, or implement similar mechanism yourself.)
in principle, have hierarchy of locales, , whenever locale not specially supported, fall parent locale. in case, "ja_jp" (in java notation) has parent locale "ja", in turn has parent locale "".
as japanese pages not specific japan, have japanese translation done ja, , when special valid japanese users in japan, additionally have ja_jp. have no problem if user sends jp_us since using japanese in usa.
if wanted use java resourcebundle mechanism indicating locales have data, create (for example) these (empty) files:
- mylocale.properties - corresponds "" locale
- mylocale_de.properties - corresponds "de" locale (german)
- mylocale_en.properties - corresponds "en" locale (english)
- mylocale_ja.properties - corresponds "ja" locale (japanese)
then in program write
locale rlocale = request.getlocale(); resourcebundle bundle = resourcebundle.getbundle("mylocale", rlocale); locale selectedlocale = bundle.getlocale(); now selectedlocale 1 of "", "de", "en", "jp", no matter locale rlocale was. example, "en", "en_gb", "en_us" in cases "en" selected, "ja" , "ja_jp" both result in "ja", while "de_de" , "de_at" both result in "de", , "it_it", "eo" , other locales result in "".
the "right java way" this: not ask bundle locale, use bundle resourcebundle, taking localized resources it. so, when need text, do
string text = bundle.gettext("greeting.hello"); and output text. use messageformat format text values inserted (or formatter). (then property files not empty, of course, contain lines these:
greeting.hello = hello world! (in english file)
greeting.hello = hallo welt! (in german file)
note browser sends not 1 locale code, list of preferred ones. in fact should "bundle-search" each of these codes , take first 1 return else "", , fall "" when no requested language matches. (for example, browser sends "eo", "de_de", "de", "en". since websites don't support esperanto, fall german (if available , selection implemented correctly), or default language (if @ first entry)).
Comments
Post a Comment