Skip to content

Instantly share code, notes, and snippets.

View theodesp's full-sized avatar
🦄
Jumping over Rainbows...

Theofanis Despoudis theodesp

🦄
Jumping over Rainbows...
View GitHub Profile
import {LocaleProvider} from './localeProvider.mjs';
...
const locale = new LocaleProvider(detectLocale());
locale.onChangeLocale((tag)=> console.log('Locale Changed to', tag));
console.log('Detected user locale is', detectLocale());
locale.setCurrent('el');
console.log('Current user locale is', locale.getCurrent());
import EventEmitter from 'https://unpkg.com/[email protected]/src/index.js?module';
export class LocaleProvider {
constructor(currentLocale = 'en', availableLocales = ['en', 'el'], defaultLocale = 'en') {
this.availableLocales = availableLocales;
this.defaultLocale = defaultLocale;
this.emiter = new EventEmitter();
this.setCurrent(currentLocale);
}
import {detectLocale} from './utils.mjs';
...
console.log('Detected user locale is ', detectLocale()); // 'en'
export function detectLocale() {
const languageString = navigator.language || '';
const language = languageString.split(/[_-]/)[0].toLowerCase();
switch (language) {
case 'en':
return 'en';
case 'el':
return 'en';
default:
var formatter;
// Note you can define multiple lines message using an Array of Strings.
Globalize.loadMessages({
en: {
party: [
"{hostGender, select,",
" female {{from} joined {to}} at her excursion}",
" male {{from} joined {to} at his excursion}",
" other {{from} joined {to} to their excursion}",
// Note you can define multiple lines message using an Array of Strings.
Globalize.loadMessages({
en: {
email: [
"You have {count, plural,",
" one {one new email}",
" other {{formattedCount} new emails}",
]
}
});
Globalize.loadMessages({
en: {
hello: "Hello, {0} {1}",
hey: "Hey, {prefix} {last}"
},
'el': {
hello: "Γιά σας, {0} {1}",
hey: "Γιά σου, {prefix} {last}"
}
});
Globalize( "el" ).formatMessage( "intro" ); // Γιά σας
Globalize.loadMessages({
'en': {
'intro': "Hello"
},
'el': {
'intro': "Γιά σας"
}
});
(function($) {
$.when(
$.getJSON( 'js/vendor/cldr/en/ca-gregorian.json' ),
$.getJSON( 'js/vendor/cldr/en/currencies.json' ),
$.getJSON( 'js/vendor/cldr/en/numbers.json' ),
$.getJSON( 'js/vendor/cldr/en/units.json' ),
$.getJSON( 'js/vendor/cldr/supplemental/plurals.json' ),
$.getJSON( 'js/vendor/cldr/supplemental/timeData.json' ),
$.getJSON( 'js/vendor/cldr/supplemental/weekData.json' ),
$.getJSON( 'js/vendor/cldr/supplemental/likelySubtags.json' )