Skip to content

Instantly share code, notes, and snippets.

@mar9000
Forked from Thanood/app.html
Last active February 23, 2017 11:36
Show Gist options
  • Save mar9000/3587db861408ab674d3055b64109783f to your computer and use it in GitHub Desktop.
Save mar9000/3587db861408ab674d3055b64109783f to your computer and use it in GitHub Desktop.
aurelia-materialize-bridge: i18n for toast
<template>
<!-- The following require is required as a workaround for last version of materializecss. -->
<require from="materialize/dist/css/materialize.css"></require>
<div>
<p><a md-button click.trigger="showDefaultToast()">default toast</a></p>
<p style="margin-top: 5px;"><a md-button click.trigger="showToastWithPromise()">toast with callback</a></p>
<p style="margin-top: 5px;"><a md-button click.trigger="showStyledToast()">toast with style</a></p>
</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>
<p><a md-button click.trigger="showDefaultToastTr()">default toast</a></p>
</div>
</template>
import { inject } from 'aurelia-framework';
import { MdToastService } from 'aurelia-materialize-bridge';
import {I18N} from 'aurelia-i18n';
@inject(MdToastService, I18N)
export class App {
language;
constructor(toast, i18n) {
this.toast = toast;
this.i18n = i18n;
}
showDefaultToast() {
this.toast.show('I am a toast!', 4000);
}
showStyledToast() {
this.toast.show("I've got style!", 4000, 'rounded blue');
}
showToastWithPromise() {
this.toast.show('When finished, I trigger another toast.', 2000).then(() => {
this.toast.show('I am a toast called by a callback of another toast!', 2000);
});
}
updateLanguage() {
this.setLanguage(this.language);
}
setLanguage(l) {
this.i18n.setLocale(l);
this.toast.show('Language set to ' + l, 4000);
}
// i18n support.
showDefaultToastTr() {
this.toast.show(this.i18n.tr('dialogs.iAmAToast'), 4000);
}
}
{
"dialogs" : {
"iAmAToast": "I am a toast! uk"
}
}
{
"dialogs" : {
"iAmAToast": "I am a toast! 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