-
Notifications
You must be signed in to change notification settings - Fork 30
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
Fail to i18n strings with parameters containing {} #35
Comments
Tried to escape curly braces. This didn't work for me. Escaping gives escaped symbol in the translation file. Removing escaping in the translation file gives an error. Just refused from using curly braces at all |
Can anyone tell me how can I use i18n outside the class like enums or const arrays something like .model.ts files? |
In order to use i18n outside of classes you need to initialize this statically before bootstrapping. This may look like including some translation manager with a static method which returns an instance of this translation service. Name it as i18n at the beginning of your .model.ts file |
@skirilo Do you have a example for me if you don't mind |
@NagaSainath I think this is a wrong thread for this discussion. But I'll give my answer here. Please move it to the correct thread if needed.
import {MissingTranslationStrategy} from "@angular/core";
import {I18n, I18nDef} from "@ngx-translate/i18n-polyfill";
import {InjectionToken} from '@angular/core';
export const USE_TRANSLATION_SERVICE = new InjectionToken<boolean>('useTranslationService');
let translationService: I18n;
export const i18nServiceFactory = (useService: boolean = true) => {
if(!translationService) {
if(useService) {
let baseLocation = "base location of your app";
let loadingTranslationFailed = false;
let translations = "";
try {
let request = new XMLHttpRequest();
request.open('GET', baseLocation + '/locale/translations.xlf', false); // `false` makes the request synchronous
request.send(null);
if (request.status === 200) {
translations = request.responseText;
} else {
console.log("Translation file could not be loaded.");
}
} catch (e) {
loadingTranslationFailed = true;
console.log("Translation file could not be loaded.");
}
if(!loadingTranslationFailed) {
translationService = new I18n("xlf", translations, 'en', MissingTranslationStrategy.Ignore);
if(translations.length > 0)
console.log('Translation service was initialized successfully');
else
console.log('Default translation values will be used');
return translationService;
}
}
console.error('Translation service was not initialized properly. Dummy translation service will be used');
translationService = (value: string | I18nDef, params: { [key: string]: any; }) => {
if(typeof value == 'string')
return value;
else if(typeof value == 'object') //instanceof I18nDef
return value.value;
else
'Unknown value type';
}
}
return translationService;
};
import { I18n } from '@ngx-translate/i18n-polyfill';
import { i18nServiceFactory, USE_TRANSLATION_SERVICE} from "@yourmodulewiththetranslationservice"; Add this code to the corresponding place below: @NgModule({
// .... some code
providers: [
// ...
{provide: USE_TRANSLATION_SERVICE, useValue:true},
{provide: I18n, useFactory: i18nServiceFactory, deps:[USE_TRANSLATION_SERVICE]},
// ...
],
import {I18n} from "@ngx-translate/i18n-polyfill";
import {i18nServiceFactory} from "@yourmodulewiththetranslationservice";
const i18n: I18n = i18nServiceFactory(); Then use it like this: export const SomeConstants = {
STRCONSTANTNAME: i18n({value: 'Some text to translate', id: 'Constants_ConstantUniqueID'}),
} And quite a lot of time passed since I used it. As far as I remember I altered the extraction tool as well to process nodes with 'i18n' name to have automatic extraction |
Hi,
The polyfill fails to render strings with parameters containing
{}
. Origin issue: Chocobozzz/PeerTube#756How to reproduce:
Add
to
i18n-polyfill.service.spec.ts
test file.Stacktrace:
Is it up to us to escape parameters values?
Thanks
The text was updated successfully, but these errors were encountered: