mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-08-18 19:21:34 +08:00
feat(i18n): translation service
This commit is contained in:
26
demos/i18n/index.ts
Normal file
26
demos/i18n/index.ts
Normal 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
12
demos/i18n/main.html
Normal 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>
|
@ -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)
|
||||
]);
|
||||
|
||||
|
@ -24,3 +24,5 @@ export * from './transitions/ios-transition'
|
||||
export * from './transitions/md-transition'
|
||||
|
||||
export * from './platform/plugins'
|
||||
|
||||
export * from './translation/translate'
|
||||
|
45
ionic/translation/translate.ts
Normal file
45
ionic/translation/translate.ts
Normal 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] || '';
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user