Skip to content

Instantly share code, notes, and snippets.

🦄
Jumping over Rainbows...

Theofanis Despoudis theodesp

🦄
Jumping over Rainbows...
  • Teckro
  • Ireland
Block or report user

Report or block theodesp

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View example.json
{ "name":"John", "age":30, "phone":null }
View console
Εσύ και 9 άλλοι το αγαπάτε
View loadTranslationsFor.js
function loadCldrData(languageTag = 'en', $ = jQuery) {
return $.when(
$.get( `js/vendor/cldr/${languageTag}/ca-gregorian.json` ),
$.get( `js/vendor/cldr/${languageTag}/currencies.json` ),
$.get( `js/vendor/cldr/${languageTag}/numbers.json`),
$.get( `js/vendor/cldr/${languageTag}/units.json` ),
).then(function() {
// Normalize $.get results, we only need the JSON, not the request statuses.
return [].slice.apply( arguments, [ 0 ] ).map(function( result ) {
return result[ 0 ];
View main.js
(function($) {
$.when(
$.get( 'js/vendor/cldr/en/ca-gregorian.json' ),
$.get( 'js/vendor/cldr/en/currencies.json' ),
$.get( 'js/vendor/cldr/en/numbers.json' ),
$.get( 'js/vendor/cldr/en/units.json' ),
$.get( 'js/vendor/cldr/supplemental/plurals.json' ),
$.get( 'js/vendor/cldr/supplemental/timeData.json' ),
$.get( 'js/vendor/cldr/supplemental/weekData.json' ),
$.get( 'js/vendor/cldr/supplemental/likelySubtags.json' )
View message.json
{
"el": {
"like": [
"{0, plural, offset:1",
" =0 {Αγάπησε το}",
" =1 {Το αγαπάς ήδη}",
" one {Εσύ και καποιος άλλος το αγαπάτε}",
" other {Εσύ και # άλλοι το αγαπάτε}",
"}"
]
View tree
➜ tree src/locale
src/locale
├── el
│   └── messages.json
└── en
└── messages.json
2 directories, 2 files
View output
Detected user locale is en
main.js:21 Locale Changed to el
main.js:36 Current user locale is el
View main.js
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());
View localeProvider.js
import EventEmitter from 'https://unpkg.com/EventEmitter@1.0.0/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);
}
View detectLocale
import {detectLocale} from './utils.mjs';
...
console.log('Detected user locale is ', detectLocale()); // 'en'
You can’t perform that action at this time.