feat(translation): Translate pipe

This commit is contained in:
Max Lynch
2015-09-25 12:18:51 -05:00
parent c3ea116046
commit 6fd4b5f323
5 changed files with 46 additions and 25 deletions

View File

@ -3,10 +3,11 @@ import {Control, ControlGroup} from 'angular2/forms';
import {IonicApp, App, Http} from 'ionic/ionic';
import {Translate} from 'ionic/ionic';
import {Translate, TranslatePipe} from 'ionic/ionic';
@App({
templateUrl: 'main.html'
templateUrl: 'main.html',
pipes: [TranslatePipe]
})
class MyApp {
constructor(app: IonicApp, trans: Translate) {
@ -18,8 +19,8 @@ class MyApp {
});
console.log(this.trans.translate('Location'));
console.log(this.trans.translate('de', 'Location'));
this.trans.setLanguage('de');
console.log(this.trans.translate('Location', 'de'));
//this.trans.setLanguage('de');
console.log(this.trans.translate('Location'));
}

View File

@ -1,12 +1,5 @@
<ion-menu #menu [content]="content">
<ion-toolbar><ion-title>Plugins</ion-title></ion-toolbar>
<ion-view>
<ion-content>
<ion-list>
<button ion-item *ng-for="#p of plugins" (click)="openPage(menu, p)">
{{p.title}}
</button>
</ion-list>
Hello! What is your {{ 'Location' | translate }}
</ion-content>
</ion-menu>
<ion-nav #content swipe-back-enabled="false" [root]="firstPage" id="myNav"></ion-nav>
</ion-view>

View File

@ -26,3 +26,4 @@ export * from './transitions/md-transition'
export * from './platform/plugins'
export * from './translation/translate'
export * from './translation/translate_pipe'

View File

@ -18,22 +18,18 @@ export class Translate {
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) {
translate(key, lang) {
// If the language isn't specified and we have no overridden one, return the string passed.
if(!lang && !this._language) {
return key;
}
let map = this.getTranslations(lang);
let setLanguage = lang || this._language;
let map = this.getTranslations(setLanguage);
if(!map) {
console.warn('I18N: No translation for key', key, 'using language', this._language);
console.warn('I18N: No translation for key', key, 'using language', setLanguage);
return '';
}
return this._getTranslation(map, key);

View File

@ -0,0 +1,30 @@
import {Injectable, Pipe, PipeTransform} from 'angular2/angular2';
import {Translate} from './translate';
/**
* The Translate pipe makes it easy to translate strings.
*
* @usage
* Translate using the current language or language set through Translate.setLanguage
* {{ 'Please enter your location' | translate }}
*
* Translate using a specific language
* {{ 'Please enter your location' | translate:"de" }}
*/
@Pipe({name: 'translate'})
@Injectable()
export class TranslatePipe implements PipeTransform {
constructor(translate: Translate) {
this.translate = translate;
}
transform(value, args) {
let lang;
if(args.length > 0) {
lang = args[0];
}
return this.translate.translate(value, lang);
}
supports(obj) { return true; }
}