-
-
Notifications
You must be signed in to change notification settings - Fork 586
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
In a JSON file, allow references to other keys #469
Comments
Don't see any valid reason to do that. Just repeat the |
Hi, what about some basic names? Ex: |
angular-translate, the popular library for translation in AngularJS, has this feature and calls it "links". Here is their explanation and justification for it taken from their Guide page:
This was a feature that we used in angular-translate, and would be in favour of adding to this library as well. |
@SamVerschueren I'll give you a real-life example:
In my language, both "hi" and "hello" mean the same. now imagine that there's a whole bunch of other words that mean "olá", I could just reference them with You can give the counter that 'then, why not just use "hello" as the translation key instead where you need "olá"' and I'll answer to you with another question "what if 'hello' and 'hi' have different meanings in other languages?" - using "hello" would break the translation. Now,
That's just plain not true. There are benefits to have from a linkable translation, I just presented you with one - and the Chinese would like to speak to you about their intricate word-meaning ;) .. Can I survive without this? Sure. Would it be a cool feature? F* yeah. a "real world" example found in the wild:
|
This can be easily implemented via a TranslateCompiler (see ngx-translate documentation on how to register it). We are using this simple implementation: export class TranslationCompiler extends TranslateCompiler {
public compile(value: string, lang: string): string {
return value;
}
public compileTranslations(translations: any, lang: string) {
for (const key in translations) {
if (translations.hasOwnProperty(key)) {
translations[key] = this.resolveReferences(translations[key], translations);
}
}
return translations;
}
private resolveReferences(value: string, translations: any) {
return value.replace(/@:(\S+)/, (matches, key) => this.resolveReferences(translations[key], translations));
}
} Please be aware of the following problems you might face with this very simplistic implementation:
|
Thanks for the pointers @pfeigl. Your Compiler helped point me in the right direction. However, it wasn't able to parse my complex en.json file. Based off of your direction, I was able to create a recursive filter that works for our needs. Here's what it looks like:
|
any implementation planned? |
???? |
Would be really useful. |
Would indeed be helpful. I find it easier to organize my translations key close to the way they appear in the components and templates hierarchy. This means i tend to repeat the same translations several times. I would like it to have a shared translations json file and point to it the other files. |
I'm submitting a ... (check one with "x")
Current behavior
Today, we can only set strings and variables like {{myVar}}. It would be great to be able to include other existing keys (like a partial translation).
Expected/desired behavior
What is the motivation / use case for changing the behavior?
Today, we have to concatenate the translations on our side:
The text was updated successfully, but these errors were encountered: