feat(i18n): translation service

This commit is contained in:
Max Lynch
2015-09-25 11:56:54 -05:00
parent 60f4d65e63
commit c3ea116046
5 changed files with 89 additions and 0 deletions

26
demos/i18n/index.ts Normal file
View File

@ -0,0 +1,26 @@
import {Component} from 'angular2/angular2';
import {Control, ControlGroup} from 'angular2/forms';
import {IonicApp, App, Http} from 'ionic/ionic';
import {Translate} from 'ionic/ionic';
@App({
templateUrl: 'main.html'
})
class MyApp {
constructor(app: IonicApp, trans: Translate) {
this.app = app;
this.trans = trans;
this.trans.translations('de', {
'Location': 'lage'
});
console.log(this.trans.translate('Location'));
console.log(this.trans.translate('de', 'Location'));
this.trans.setLanguage('de');
console.log(this.trans.translate('Location'));
}
}

12
demos/i18n/main.html Normal file
View File

@ -0,0 +1,12 @@
<ion-menu #menu [content]="content">
<ion-toolbar><ion-title>Plugins</ion-title></ion-toolbar>
<ion-content>
<ion-list>
<button ion-item *ng-for="#p of plugins" (click)="openPage(menu, p)">
{{p.title}}
</button>
</ion-list>
</ion-content>
</ion-menu>
<ion-nav #content swipe-back-enabled="false" [root]="firstPage" id="myNav"></ion-nav>

View File

@ -14,6 +14,7 @@ import {Popup} from '../popup/popup';
import {FocusHolder} from '../form/focus-holder';
import {Events} from '../../util/events';
import {NavRegistry} from '../nav/nav-registry';
import {Translate} from '../../translation/translate';
/**
* @name IonicApp
@ -298,6 +299,8 @@ export function ionicBootstrap(rootComponentType, views, config) {
let modal = new Modal(app, config);
let popup = new Popup(app, config);
let events = new Events();
let translate = new Translate();
console.log('Translate', translate);
let navRegistry = new NavRegistry(views);
// add injectables that will be available to all child components
@ -312,6 +315,7 @@ export function ionicBootstrap(rootComponentType, views, config) {
bind(Events).toValue(events),
ROUTER_BINDINGS,
bind(LocationStrategy).toClass(HashLocationStrategy),
bind(Translate).toValue(translate),
bind(NavRegistry).toValue(navRegistry)
]);

View File

@ -24,3 +24,5 @@ export * from './transitions/ios-transition'
export * from './transitions/md-transition'
export * from './platform/plugins'
export * from './translation/translate'

View File

@ -0,0 +1,45 @@
import {Injectable} from 'angular2/angular2';
@Injectable()
export class Translate {
constructor() {
this._transMap = {};
}
translations(lang, map) {
this._transMap[lang] = map;
}
setLanguage(lang) {
this._language = lang;
}
getTranslations(lang) {
return this._transMap[lang];
}
translate(lang, key) {
// If called with just one param, first is the key
if(typeof key === 'undefined') {
key = lang;
lang = this._language;
}
// If the language isn't specified, return the string passed.
if(!lang) {
return key;
}
let map = this.getTranslations(lang);
if(!map) {
console.warn('I18N: No translation for key', key, 'using language', this._language);
return '';
}
return this._getTranslation(map, key);
}
_getTranslation(map, key) {
return map && map[key] || '';
}
}