Skip to content

Instantly share code, notes, and snippets.

@falkolab
Last active Feb 11, 2016
Embed
What would you like to do?
Set Moment locale to the Titanium SDK Ti.Locale.currentLocale
var moment = require('alloy/moment');
// First add languages that support your application
var languages = ['ru', 'nl', 'fr', 'fr-ca'];
// Full language list: https://github.com/appcelerator/alloy/tree/master/Alloy/builtins/moment/lang
// You must explicit way to require language files in order to compiler saw their.
require('alloy/moment/lang/ru');
require('alloy/moment/lang/nl');
require('alloy/moment/lang/fr');
require('alloy/moment/lang/fr-ca');
function selectLanguage(lang) {
// Not all locales with country code has corresponding file in the alloy/moment.
// So we will use first 2-letter language code as insurance.
[lang.split('-')[0], lang.toLowerCase()].forEach(function(lang) {
if(languages.indexOf(lang) !== -1) {
moment.lang(lang);
}
});
}
exports.selectLanguage = selectLanguage;
var moment = require('alloy/moment');
var mod = require('momentLang');
Ti.API.info('current locale:', Ti.Locale.currentLocale);
Ti.API.info('current language:', moment.lang(), 'date:', moment().format('L'));
mod.selectLanguage(Ti.Locale.currentLocale);
Ti.API.info('current language:', moment.lang(), 'date:', moment().format('L'));
// Ask user for language or select as you need (Ti.Locale.currentLocale).
// Letter case does not matter.
mod.selectLanguage('fr-ca');
Ti.API.info('current language:', moment.lang(), 'date:', moment().format('L'));
mod.selectLanguage('fr');
Ti.API.info('current language:', moment.lang(), 'date:', moment().format('L'));
mod.selectLanguage('nl-BE');
Ti.API.info('current language:', moment.lang(), 'date:', moment().format('L'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment