coderifous / jquery-localize

a jQuery plugin that makes it easy to internationalize your web site.
465 stars 142 forks source link

fix android quirks (navigator.languages is empty on Android >= 5.0) #74

Closed shaolin405mi16 closed 7 years ago

shaolin405mi16 commented 8 years ago

Hi,

I encountered an issue on Cordova + jQuery + jquery-localize on Android 5.0 and up (confirmed on 5.0 / 5.1 / 6.0 AVD emulators). I guess it is the same issue as issue #72 .

This issue happens when navigator.languages returns an empty array [] although navigator.language itself contains a value. Seems like it's happening on Android 5.0 and up (see also: “navigator.languages is empty in webview”)

In line 20 of src/jquery.localize.coffee, the argument for normalizeLang() is (unexpectedly) set to undefined:

$.defaultLanguage = normaliseLang(if navigator.languages then navigator.languages[0] else navigator.language or navigator.userLanguage)

then in the line 14, it fails with the message "Uncaught TypeError: Cannot read property 'replace' of undefined".

So here's the fix for this, ensuring navigator.languages is not empty:

$.defaultLanguage = normaliseLang(if navigator.languages and navigator.languages.length > 0 then navigator.languages[0] else navigator.language or navigator.userLanguage)

I believe this trivial patch has no side effect (for older Android versions / iOS / other browsers).

ArkeoSupport commented 8 years ago

This fix also solved issue #72

oSoMoN commented 8 years ago

This also fixes a similar issue in oxide (a web engine based on the chromium content API). Although it clearly is a bug in the web engine / webview, it’s easy enough to work around in jquery-localize, so I would like to see this fix included.

coderifous commented 7 years ago

Thanks for contributing @shaolin405mi16 ! Your work merged here: https://github.com/coderifous/jquery-localize/commit/eaa980b71aec65e136db94b055e7513a78a1623e