Last active
November 27, 2018 07:12
-
-
Save jasonkneen/5283642701575ec3fcbd to your computer and use it in GitHub Desktop.
Dynamically change languages in a Titanium Alloy app (for testing)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// add this to the Alloy.js file | |
// Set to run in ENV_DEV mode so it's used for testing withou | |
// having to change languages on device etc | |
// NO checks in place so assumes you know what you're doing, have | |
// the relevant strings files and specify the correct one! | |
// should work on Android - not tested yet! | |
if (ENV_DEV) { | |
Alloy.Globals.setLanguage = function(lang) { | |
lang = lang || "en"; | |
var file = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory, lang + '/strings.xml'), | |
doc = Ti.XML.parseString(file.read().toString()), | |
nodes = doc.getElementsByTagName('string'), | |
strings = {}; | |
for (var i = 0; i < nodes.length; i++) { | |
strings[nodes.item(i).getAttribute('name')] = nodes.item(i).text; | |
}; | |
W = L; | |
// redefine L - NOT ideal but necessary to test | |
// languages without affecting existing code | |
L = function(key) { | |
return strings[key]; | |
}; | |
}; | |
Alloy.Globals.setLanguage(); | |
} |
ketikutateladze
commented
Nov 27, 2018
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment