Skip to content

Commit

Permalink
Feat(TranslationService): add support to objects as values #129
Browse files Browse the repository at this point in the history
  • Loading branch information
robisim74 committed Oct 5, 2017
1 parent 87a0743 commit 69c1808
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/services/translation-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ import { TRANSLATION_CONFIG, TranslationConfig } from '../models/l10n-config';
* @param lang The current language
* @return The parsed value
*/
public abstract parseValue(path: string, key: string, value: string | null, args: any, lang: string): string;
public abstract parseValue(path: string, key: string, value: string | null, args: any, lang: string): string | any;

}

@Injectable() export class DefaultTranslationHandler implements TranslationHandler {

constructor( @Inject(TRANSLATION_CONFIG) private configuration: TranslationConfig) { }

public parseValue(path: string, key: string, value: string | null, args: any, lang: string): string {
public parseValue(path: string, key: string, value: string | null, args: any, lang: string): string | any {
if (value == null) {
return this.handleMissingValue(path);
} else if (args) {
Expand Down
19 changes: 10 additions & 9 deletions src/services/translation.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export interface ITranslationService {
});
}

private translateKey(key: string, args: any, lang: string): string | null {
private translateKey(key: string, args: any, lang: string): string | any {
if (key == null || key == "") return null;
// I18n plural.
if (this.configuration.i18nPlural && /^\d+\b/.exec(key)) {
Expand All @@ -136,21 +136,22 @@ export interface ITranslationService {
return this.getValue(key, args, lang);
}

private getValue(key: string, args: any, lang: string): string {
private getValue(key: string, args: any, lang: string): string | any {
const path: string = key;
let value: string | null = null;
if (this.translationData[lang]) {
let translation: any = this.translationData[lang];

let translation: any = this.translationData[lang];

if (translation) {
// Composed key.
if (this.configuration.composedKeySeparator) {
const sequences: string[] = key.split(this.configuration.composedKeySeparator);
do {

key = sequences.shift()!;
while (sequences.length > 0 && translation[key]) {
translation = translation[key];
key = sequences.shift()!;
if (translation[key] && typeof translation[key] === "object") {
translation = translation[key];
}
} while (sequences.length > 0);
}
}

value = translation[key] || translation[this.configuration.missingKey || ""];
Expand Down

0 comments on commit 69c1808

Please sign in to comment.