mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-08-18 19:21:34 +08:00
feat(translation): Translate pipe
This commit is contained in:
@ -3,10 +3,11 @@ import {Control, ControlGroup} from 'angular2/forms';
|
|||||||
|
|
||||||
import {IonicApp, App, Http} from 'ionic/ionic';
|
import {IonicApp, App, Http} from 'ionic/ionic';
|
||||||
|
|
||||||
import {Translate} from 'ionic/ionic';
|
import {Translate, TranslatePipe} from 'ionic/ionic';
|
||||||
|
|
||||||
@App({
|
@App({
|
||||||
templateUrl: 'main.html'
|
templateUrl: 'main.html',
|
||||||
|
pipes: [TranslatePipe]
|
||||||
})
|
})
|
||||||
class MyApp {
|
class MyApp {
|
||||||
constructor(app: IonicApp, trans: Translate) {
|
constructor(app: IonicApp, trans: Translate) {
|
||||||
@ -18,8 +19,8 @@ class MyApp {
|
|||||||
});
|
});
|
||||||
|
|
||||||
console.log(this.trans.translate('Location'));
|
console.log(this.trans.translate('Location'));
|
||||||
console.log(this.trans.translate('de', 'Location'));
|
console.log(this.trans.translate('Location', 'de'));
|
||||||
this.trans.setLanguage('de');
|
//this.trans.setLanguage('de');
|
||||||
console.log(this.trans.translate('Location'));
|
console.log(this.trans.translate('Location'));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,5 @@
|
|||||||
<ion-menu #menu [content]="content">
|
<ion-view>
|
||||||
<ion-toolbar><ion-title>Plugins</ion-title></ion-toolbar>
|
|
||||||
<ion-content>
|
<ion-content>
|
||||||
<ion-list>
|
Hello! What is your {{ 'Location' | translate }}
|
||||||
<button ion-item *ng-for="#p of plugins" (click)="openPage(menu, p)">
|
|
||||||
{{p.title}}
|
|
||||||
</button>
|
|
||||||
</ion-list>
|
|
||||||
</ion-content>
|
</ion-content>
|
||||||
</ion-menu>
|
</ion-view>
|
||||||
|
|
||||||
<ion-nav #content swipe-back-enabled="false" [root]="firstPage" id="myNav"></ion-nav>
|
|
||||||
|
@ -26,3 +26,4 @@ export * from './transitions/md-transition'
|
|||||||
export * from './platform/plugins'
|
export * from './platform/plugins'
|
||||||
|
|
||||||
export * from './translation/translate'
|
export * from './translation/translate'
|
||||||
|
export * from './translation/translate_pipe'
|
||||||
|
@ -18,22 +18,18 @@ export class Translate {
|
|||||||
return this._transMap[lang];
|
return this._transMap[lang];
|
||||||
}
|
}
|
||||||
|
|
||||||
translate(lang, key) {
|
translate(key, lang) {
|
||||||
// If called with just one param, first is the key
|
// If the language isn't specified and we have no overridden one, return the string passed.
|
||||||
if(typeof key === 'undefined') {
|
if(!lang && !this._language) {
|
||||||
key = lang;
|
|
||||||
lang = this._language;
|
|
||||||
}
|
|
||||||
|
|
||||||
// If the language isn't specified, return the string passed.
|
|
||||||
if(!lang) {
|
|
||||||
return key;
|
return key;
|
||||||
}
|
}
|
||||||
|
|
||||||
let map = this.getTranslations(lang);
|
let setLanguage = lang || this._language;
|
||||||
|
|
||||||
|
let map = this.getTranslations(setLanguage);
|
||||||
|
|
||||||
if(!map) {
|
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 '';
|
||||||
}
|
}
|
||||||
return this._getTranslation(map, key);
|
return this._getTranslation(map, key);
|
||||||
|
30
ionic/translation/translate_pipe.ts
Normal file
30
ionic/translation/translate_pipe.ts
Normal 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; }
|
||||||
|
}
|
Reference in New Issue
Block a user