HTML5 Mobile app localization using javascript and PhoneGap

前端 未结 3 506
粉色の甜心
粉色の甜心 2021-01-01 03:21

I\'m creating a HTML5 mobile app that runs on all 3 mobile platforms (Android, iOS a,d Windows Mobile 8). I\'m using javascript for localization(https://github.com/eligrey/l

3条回答
  •  萌比男神i
    2021-01-01 03:56

    You can write your own JavaScript to grab the localisation language. This is the exact same reply I wrote elsewhere on here.

    In general, the JavaScript window.navigator.language usually works for iOS and newer Androids, but earlier versions of Android have it hardcoded to en.

    For older Androids I suggest pulling the language parameter out of the UserAgent string (yes sniffing!), e.g.

    if (navigator && navigator.userAgent && (androidLang = navigator.userAgent.match(/android.*\W(\w\w)-(\w\w)\W/i))) {
       lang = androidLang[1];
    }
    

    Here, lang might also contain the country code (e.g. 'en-IE') so you might have to remove that also:

    if (lang.indexOf('-') != -1) lang = lang.substring(0, lang.indexOf('-'));
    

    This is what I've used in a recent app, using HTML5 and PhoneGap, and it works fine.

提交回复
热议问题