Skip to content

Instantly share code, notes, and snippets.

@mar9000
Last active April 3, 2017 16:06
Show Gist options
  • Save mar9000/073d6e0609352efa260f4dd11da54304 to your computer and use it in GitHub Desktop.
Save mar9000/073d6e0609352efa260f4dd11da54304 to your computer and use it in GitHub Desktop.
aurelia-materialize-bridge: i18n for input
<template>
<!-- The following require is required as a workaround for last version of materializecss. -->
<require from="materialize/dist/css/materialize.css"></require>
<div>
<md-input md-label="put some text here" md-value.bind="textValue" md-disabled.bind="disabledValue"></md-input><br />
You entered: ${textValue}<br />
<button md-button="flat: true;" md-waves class="accent-text" click.delegate="setText()">set text to something</button>
<button md-button="flat: true;" md-waves class="accent-text" click.delegate="setDisabled()">Toggle Input Disabling</button>
</div>
<!-- i18n support. -->
<br/>
<div>
<h4>i18n support</h4>
<select md-select value.two-way="language" change.trigger="updateLanguage()">
<option value="" disabled selected t="selectLanguage">S L</option>
<option value="en_UK">English UK</option>
<option value="en_US">English US</option>
</select>
<md-input md-label="psth" t="[md-label]dialogs.putSomeTextHere" md-value.bind="textValue" md-disabled.bind="disabledValue"></md-input>
</div>
</template>
import { inject } from 'aurelia-framework';
import {I18N} from 'aurelia-i18n';
@inject(I18N)
export class App {
textValue = '';
disabledValue = false;
setText() {
this.textValue = 'something';
}
setDisabled() {
this.disabledValue = !this.disabledValue;
}
constructor(i18n) {
this.i18n = i18n;
}
language;
updateLanguage() {
this.setLanguage(this.language);
}
setLanguage(l) {
this.i18n.setLocale(l);
this.toast.show('Language set to ' + l, 4000);
}
}
{
"dialogs" : {
"putSomeTextHere": "put some text here uk"
}
}
{
"dialogs" : {
"putSomeTextHere": "put some text here us"
}
}
<!doctype html>
<html>
<head>
<title>Aurelia</title>
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body aurelia-app="main">
<h1>Loading....</h1>
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.6/system.js"></script>
<script src="https://rawgit.com/mar9000/aurelia-materialize-bundles/0.24.0-1_i18n-2/config2.js"></script>
<script>
System.import('aurelia-bootstrapper');
</script>
</body>
</html>
import {inject} from 'aurelia-framework';
import {I18N} from 'aurelia-i18n';
@inject(I18N)
export class Locale {
static LANGUAGES = new Map([
['en_UK', 'English UK'],
['en_US', 'English US']
]);
static INITIAL_LANGUAGE_KEY = 'en_UK';
constructor(i18n) {
this.i18n = i18n;
}
setLanguage(l) {
this.currentLanguageKey = l;
this.currentLanguageName = Locale.LANGUAGES.get(this.currentLanguageKey);
this.i18n.setLocale(l);
}
get languages() {
return Locale.LANGUAGES;
}
}
/*******************************************************************************
* The following two lines enable async/await without using babel's
* "runtime" transformer. Uncomment the lines if you intend to use async/await.
*
* More info here: https://github.com/jdanyow/aurelia-plunker/issues/2
*/
//import regeneratorRuntime from 'babel-runtime/regenerator';
//window.regeneratorRuntime = regeneratorRuntime;
/******************************************************************************/
import 'materialize';
import {I18N} from 'aurelia-i18n';
import Backend from 'i18next-xhr-backend';
import {Locale} from 'locale';
export function configure(aurelia) {
aurelia.use
.standardConfiguration()
.developmentLogging()
.plugin('aurelia-materialize-bridge', bridge => bridge.useAll() )
// Add the aurelia-i18n plugin.
.plugin('aurelia-i18n', (instance) => {
instance.i18next.use(Backend);
return instance.setup({
backend: {
loadPath: './{{lng}}_{{ns}}.json'
},
lng : Locale.INITIAL_LANGUAGE_KEY,
attributes : ['t'],
fallbackLng : Locale.INITIAL_LANGUAGE_KEY,
debug : true
});
});
aurelia.start().then(a => a.setRoot());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment