From c3ea116046ea4badd11f464cea75e5a4314a22cd Mon Sep 17 00:00:00 2001 From: Max Lynch Date: Fri, 25 Sep 2015 11:56:54 -0500 Subject: [PATCH] feat(i18n): translation service --- demos/i18n/index.ts | 26 ++++++++++++++++++++ demos/i18n/main.html | 12 +++++++++ ionic/components/app/app.ts | 4 +++ ionic/ionic.ts | 2 ++ ionic/translation/translate.ts | 45 ++++++++++++++++++++++++++++++++++ 5 files changed, 89 insertions(+) create mode 100644 demos/i18n/index.ts create mode 100644 demos/i18n/main.html create mode 100644 ionic/translation/translate.ts diff --git a/demos/i18n/index.ts b/demos/i18n/index.ts new file mode 100644 index 0000000000..997da2be39 --- /dev/null +++ b/demos/i18n/index.ts @@ -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')); + + } +} diff --git a/demos/i18n/main.html b/demos/i18n/main.html new file mode 100644 index 0000000000..1882213767 --- /dev/null +++ b/demos/i18n/main.html @@ -0,0 +1,12 @@ + + Plugins + + + + + + + + diff --git a/ionic/components/app/app.ts b/ionic/components/app/app.ts index def11107a2..56d46155e3 100644 --- a/ionic/components/app/app.ts +++ b/ionic/components/app/app.ts @@ -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) ]); diff --git a/ionic/ionic.ts b/ionic/ionic.ts index 6ccc5c4203..359ed77413 100644 --- a/ionic/ionic.ts +++ b/ionic/ionic.ts @@ -24,3 +24,5 @@ export * from './transitions/ios-transition' export * from './transitions/md-transition' export * from './platform/plugins' + +export * from './translation/translate' diff --git a/ionic/translation/translate.ts b/ionic/translation/translate.ts new file mode 100644 index 0000000000..61659a6f47 --- /dev/null +++ b/ionic/translation/translate.ts @@ -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] || ''; + } +}