Last active
February 11, 2016 10:11
-
-
Save falkolab/68cd7f80c9c52e813462 to your computer and use it in GitHub Desktop.
Set Moment locale to the Titanium SDK Ti.Locale.currentLocale
This file contains 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
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; |
This file contains 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
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