Skip to content

Instantly share code, notes, and snippets.

@jasonkneen jasonkneen/Alloy.js
Last active Nov 27, 2018

Embed
What would you like to do?
Dynamically change languages in a Titanium Alloy app (for testing)
// 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();
}
@jasonkneen

This comment has been minimized.

Copy link
Owner Author

jasonkneen commented Jan 27, 2015

Feel free to pretty it up, suggest fixes etc - This was a quick workaround I needed to be able to switch languages when using TiShadow to test layouts

@jasonkneen

This comment has been minimized.

Copy link
Owner Author

jasonkneen commented Jan 27, 2015

Doesn't work in Android. yet ;)

@Baharroth

This comment has been minimized.

Copy link

Baharroth commented Feb 3, 2015

Hi.
This is a code who working with IOS 7 and Android 4.4.
http://pulkitgoyal.in/managing-multiple-languages-with-appcelerator-titanium/

Warning : Since sdk 3.5.0, you can't override L() ! It's annoying for me.

Little trick : check the path of your ressource file. Not the same between iOS 7 and Android with sdk 3.2.2

@john-dalsgaard

This comment has been minimized.

Copy link

john-dalsgaard commented Sep 21, 2017

I think the reason it does not work on Android is that you need to use nodes.item(i).textContent instead of nodes.item(i).text (which is deprecated - and does not work on Android according to the documentation).

I had the same issue in my snippet - but it is now working on Android as well: https://github.com/john-dalsgaard/Alloy/blob/master/language.js

@ketikutateladze

This comment has been minimized.

Copy link

ketikutateladze commented Nov 27, 2018

I think the reason it does not work on Android is that you need to use nodes.item(i).textContent instead of nodes.item(i).text (which is deprecated - and does not work on Android according to the documentation).

I had the same issue in my snippet - but it is now working on Android as well: https://github.com/john-dalsgaard/Alloy/blob/master/language.js
@john-dalsgaard Thank You. This helped me in Titanium sdk 7.0.0 (android version)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.