FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

feat(i18n): translation service · Java1024/ionic@c3ea116 · GitHub

Repository navigation

Commit c3ea116

Browse files
committed
feat(i18n): translation service
1 parent 60f4d65 commit c3ea116

5 files changed

Lines changed: 89 additions & 0 deletions

File tree

‎demos/i18n/index.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import {Component} from 'angular2/angular2';
2+
import {Control, ControlGroup} from 'angular2/forms';
3+
4+
import {IonicApp, App, Http} from 'ionic/ionic';
5+
6+
import {Translate} from 'ionic/ionic';
7+
8+
@App({
9+
templateUrl: 'main.html'
10+
})
11+
class MyApp {
12+
constructor(app: IonicApp, trans: Translate) {
13+
this.app = app;
14+
this.trans = trans;
15+
16+
this.trans.translations('de', {
17+
'Location': 'lage'
18+
});
19+
20+
console.log(this.trans.translate('Location'));
21+
console.log(this.trans.translate('de', 'Location'));
22+
this.trans.setLanguage('de');
23+
console.log(this.trans.translate('Location'));
24+
25+
}
26+
}

‎demos/i18n/main.html‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<ion-menu #menu [content]="content">
2+
<ion-toolbar><ion-title>Plugins</ion-title></ion-toolbar>
3+
<ion-content>
4+
<ion-list>
5+
<button ion-item *ng-for="#p of plugins" (click)="openPage(menu, p)">
6+
{{p.title}}
7+
</button>
8+
</ion-list>
9+
</ion-content>
10+
</ion-menu>
11+
12+
<ion-nav #content swipe-back-enabled="false" [root]="firstPage" id="myNav"></ion-nav>

‎ionic/components/app/app.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {Popup} from '../popup/popup';
1414
import {FocusHolder} from '../form/focus-holder';
1515
import {Events} from '../../util/events';
1616
import {NavRegistry} from '../nav/nav-registry';
17+
import {Translate} from '../../translation/translate';
1718

1819
/**
1920
* @name IonicApp
@@ -298,6 +299,8 @@ export function ionicBootstrap(rootComponentType, views, config) {
298299
let modal = new Modal(app, config);
299300
let popup = new Popup(app, config);
300301
let events = new Events();
302+
let translate = new Translate();
303+
console.log('Translate', translate);
301304
let navRegistry = new NavRegistry(views);
302305

303306
// add injectables that will be available to all child components
@@ -312,6 +315,7 @@ export function ionicBootstrap(rootComponentType, views, config) {
312315
bind(Events).toValue(events),
313316
ROUTER_BINDINGS,
314317
bind(LocationStrategy).toClass(HashLocationStrategy),
318+
bind(Translate).toValue(translate),
315319
bind(NavRegistry).toValue(navRegistry)
316320
]);
317321

‎ionic/ionic.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,5 @@ export * from './transitions/ios-transition'
2424
export * from './transitions/md-transition'
2525

2626
export * from './platform/plugins'
27+
28+
export * from './translation/translate'

‎ionic/translation/translate.ts‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import {Injectable} from 'angular2/angular2';
2+
3+
@Injectable()
4+
export class Translate {
5+
constructor() {
6+
this._transMap = {};
7+
}
8+
9+
translations(lang, map) {
10+
this._transMap[lang] = map;
11+
}
12+
13+
setLanguage(lang) {
14+
this._language = lang;
15+
}
16+
17+
getTranslations(lang) {
18+
return this._transMap[lang];
19+
}
20+
21+
translate(lang, key) {
22+
// If called with just one param, first is the key
23+
if(typeof key === 'undefined') {
24+
key = lang;
25+
lang = this._language;
26+
}
27+
28+
// If the language isn't specified, return the string passed.
29+
if(!lang) {
30+
return key;
31+
}
32+
33+
let map = this.getTranslations(lang);
34+
35+
if(!map) {
36+
console.warn('I18N: No translation for key', key, 'using language', this._language);
37+
return '';
38+
}
39+
return this._getTranslation(map, key);
40+
}
41+
42+
_getTranslation(map, key) {
43+
return map && map[key] || '';
44+
}
45+
}

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL