bircastri
bircastri

Reputation: 2167

How to change language app at runtime with appcelerator

I'm builind an app to iOS and Android with AppCelerator.

So I have created Under i18n, two folder

i18n
--en
----string.xml
--it
----string.xml

In the string.xml I have insert some resources.

Now how can I change the language of application at runtime???

I'm try to do this but not works:

Ti.App.Properties.setString('SETTING_LANUAGE','it');

Upvotes: 3

Views: 371

Answers (1)

Prashant Saini
Prashant Saini

Reputation: 3539

There are many ways to change the app language at runtime but it will only change your app's language, not the entire device language. So I hope this is what you want to achieve.

The simple solution I know is that:

  • e.g. Use L("appTitle") method to change the language.

Here is a short code snippet to use above method:

en -> strings.xml

<string name="en_home">Home</string>
<string name="en_cl">Change Language</string>

it -> strings.xml

<string name="it_home">Casa</string>
<string name="it_cl">Cambia lingua</string>

alloy.js

// you can set this to any language at default or you can save the last applied language in the app
Alloy.Globals.Lang = "en_";

index.js

var win = Ti.UI.createWindow({
    backgroundColor : "#aaaaaa"
});
var lang = Alloy.Globals.Lang;

var label = Ti.UI.createLabel({
    text : L(lang + "home")
});

var btn = Ti.UI.createButton({
    title : L(lang + "cl"),
    bottom : 50,
    color : 'white',
    backgroundColor : "#0aa"
});
btn.addEventListener('click', changeLanguageItalian);

win.add(label);
win.add(btn);

win.open();

function changeLanguageItalian() {
  Alloy.Globals.Lang = "it_";
  lang =  Alloy.Globals.Lang;
  btn.title = L(lang+"cl");
  label.text = L(lang+"home");
}

Note that this is just a simple explanation of how you can change the language at run time. You can try anything you feel right according to your app. Good Luck!

Upvotes: 1

Related Questions