-
-
Notifications
You must be signed in to change notification settings - Fork 202
Bug(inline-loader): Translations with the inline loader strategy only work at the template level #539
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
Comments
Hi, @kaname-png |
@noobyogi0010 please. |
Can you take a look at the problem? @shaharkazaz |
@noobyogi0010 do you still want this issue? :) |
Thanks for answering @shaharkazaz, I hope you can or someone can solve this problem, this prevents me from being able to continue with my project since I need it. And thank you very much in advance. |
Hey @shaharkazaz any update on this issue? I am facing the same issue. |
@mehrad-rafigh you are welcome to pick this up and open a PR :) |
@shaharkazaz I would like to do that. Can you please point me to the direction? I haven't contributed to transloco before |
@mehrad-rafigh I'm currently on vacation so I don't have my laptop and I didn't get the chance to investigate the issue myself. I suggest you start by creating a small dedicated repo that reproduces the issue and try to work your way from there by using a local version of the library so you can debug the issue easily. |
I encountered the same issue, but I found that if you pass a scope as the third argument to the To cater to your specific example, I think the following will work: I used an array accessor there since you have the scope provider marked as |
Also facing the same issue here :) |
The issue arises when scopes are provided via @gmiklich approach works fine and it does make sense that a asynchronous method is needed when retrieving a lazy module translation. I thought about a way by |
I wrote this provider factory, that might be useful for one or another person. import { Provider } from '@angular/core';
import { TRANSLOCO_SCOPE } from '@ngneat/transloco';
export function translateProvider(scope : string) : Provider
{
const provider : Provider =
{
provide: TRANSLOCO_SCOPE,
useValue:
{
scope,
loader:
{
en: () => import('libs/frontend/' + scope + '/src/assets/i18n/en.json'),
de: () => import('libs/frontend/' + scope + '/src/assets/i18n/de.json')
}
},
multi: true
};
return provider;
} It can either be used in modules (feature lib) or directly in components: providers:
[
translateProvider('shared')
] Unfortunately I face the same issue of unloaded scopes which are causing flickers and repaints on the app. From my observation using |
I don't know if it helps, but I found kind of a hackaround. First of all, my setup: @NgModule({
exports: [ TranslocoModule ],
providers: [
{
provide: TRANSLOCO_CONFIG,
useValue: translocoConfig({
availableLangs: ['en', 'de'],
defaultLang: 'en',
// Remove this option if your application
// doesn't support changing language in runtime.
reRenderOnLangChange: true,
prodMode: !isDevMode(),
})
},
{ provide: TRANSLOCO_LOADER, useClass: TranslocoHttpLoader }
]
})
export class TranslocoRootModule {} @Injectable({ providedIn: 'root' })
export class TranslocoHttpLoader implements TranslocoLoader {
private readonly http = inject(HttpClient)
getTranslation(lang: string) {
return this.http.get<Translation>(`/assets/i18n/${lang}.json`);
}
} The feature module with the scope is imported in the root app module and provides the scope like so: {
provide: TRANSLOCO_SCOPE,
multi: true,
useValue: {
scope: 'feature-scope-name',
loader: scopeLoader(
(lang: string, root: string) => import(`./${root}/${lang}.json`)
),
},
}, Using translations from the scope SolutionI now decided not to use a scopeLoader for this use case and instead, trying to utilize the httpLoader from the root module for this job. {
provide: TRANSLOCO_SCOPE,
multi: true,
useValue: {
scope: 'feature-scope-name',
},
}, Additional I wrote an APP_INITIALIZER factory {
provide: APP_INITIALIZER,
useFactory: initialize,
multi: true,
deps: [TranslocoService],
}, export function initialize( translateService: TranslocoService ): () => Promise<void> {
return () =>
new Promise<void>(async (resolve) => {
// Setting default language to enforce loading translation before app starts
translateService.setActiveLang(translateService.getDefaultLang());
// Scope hack...
const scopeLang = translateService._completeScopeWithLang('inspector');
await firstValueFrom(translateService.selectTranslation(scopeLang));
resolve();
});
} Additionally you have to output the library i18n files into the app assets in angular.json(or project.json in NX): "assets": [
{
"input": "libs/feature/src/lib/i18n",
"glob": "**/*",
"output": "assets/i18n/feature-scope-name"
}
], ConclusionI think, it's not a good solution because you need extra steps like defining the assets in the angular.json. Additionally the languages initialize on app start which also is not good from performance side. Lazy LoadingFor lazy loaded modules, this approach also should work. But yeah, it's a fail to load the child modules translations before the actual child module has been loaded. Maybe the scope hack can be executed right before the lazy module is being imported. Maybe in a guard? |
Are there any plans to fix this issue? It is more than 2 years old and I now ran into the same issues. |
So am I right, that the scoped tranlation keys are always prefixed with the scope name? Wouldn't it be possible to inject all scope providers in selectTranslate, use the first part of the translation key to find the scope and the corresponding provider and then to wait until the inlineloader is loaded? I don't think it is a great developer experience to always have to provide the ProviderScope from the injected array. |
I'm struggling with this problem too right now |
Uh oh!
There was an error while loading. Please reload this page.
Is there an existing issue for this?
Which Transloco package(s) are the source of the bug?
Transloco, Locale
Is this a regression?
Yes
Current behavior
I try to get translations through my component's service, but it always throws error that the translation has not been found, but in the component's template it works perfectly.
I have tried the .translate() and .selectTranslate() methods and nothing works.
Code
Results
Expected behavior
Correctly obtain the translations at the service and template level of the component.
Please provide a link to a minimal reproduction of the bug
N/A
Transloco Config
No response
Please provide the environment you discovered this bug in
Browser
Additional context
The translation keys are correct and I have also tried to use the same translation keys that are in the template and they work but it does not work at the service level either.
I would like to make a pull request for this bug
No
The text was updated successfully, but these errors were encountered: