Reload App.js?
Hi, I'm making an app with a language setting.
I'm first setting the language to the iPhone locale in app.js
like this:
if (!Titanium.App.Properties.getString('locale')) {
var language = Titanium.Platform.locale;
Titanium.App.Properties.setString('locale',language);
}
On a settings page I then override this with a new setString.
This works good, however I need to restart the app for the change to take effect.
I'm calling the language files in a included file like this:
var language = Titanium.App.Properties.getString('locale');
if (language == 'sv') {
Ti.include('json/lang/swedish.js');
}
else if (language == 'en') {
Ti.include('json/lang/english.js');
}
else if (language == 'de') {
Ti.include('json/lang/deutsch.js');
}
This is included on every page. So basically I need to refresh app.js, is there any way to do that? Or a better way to incorporate a language setting?
Thanks
4 Answers
-
Accepted Answer
there's no direct way to reload the app.js or main application context.
to probably do what you're talking, you'd want to put everything in a new window which is a separate JS context (vs. app.js) and then reload that window.
your window could simply be a non-visual window that opens other windows, tabs, etc. but could then be used to reload (by closing and re-opening) on a language change.
-
maybe you can do this with a little trick by using Titanium.App which is available in the whole app:
var language = Titanium.Platform.locale; Titanium.App.Properties.setString('locale',language); Titanium.App.language = language;
when calling your language files simply use:
var language = Titanium.App.language;
so you have the setting saved for the next run of the app and also available for actual run.
hope that helps.
-
Exactly what do you mean with
"which is a separate JS context (vs. app.js) and then reload that window."
? -
In your app.js, create a new window to another file:
function reopen() { var window = Ti.UI.createWindow({url:"myapp.js"}); window.open(); } reopen(); Ti.App.addEventListener('reload',reopen);
Then in myapp.js when you want to reload the app, call
Ti.App.fireEvent('reload');