mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2026-03-13 10:22:08 +08:00
feat(i18n): translation service
This commit is contained in:
@@ -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