Im building a small Angular Application with Angular's i18n setup. Everything ist working fine, except for the translations of the url paths and slugs. I tried a possible solution with providing a routing module per language (as described here), but this did not work.
I thought that I could do something like the following, but I can't figure out where to inject LOCALE_ID
:
app-routing.module.ts
import { LOCALE_ID, NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { MainComponent } from './main/main.component';
const i18nRoutes = {
de: {
main: 'inhalte',
// ...
},
fr: {
main: 'contenu',
// ...
}
}
const currentLanguage = i18nRoutes[ LOCALE_ID ]; // <-- Apparently not working, since I have to inject LOCALE_ID. But where?
const routes: Routes = [
{
path: '',
redirectTo: currentLanguage.main,
pathMatch: 'full'
},
{
path: currentLanguage.main + '/:key',
component: MainComponent
}
// ...
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
Update for clarification
in angular.json
, I set configurations for the building process in each language (taken from here, with a view modifications for 2018)
angular.json
{
// ...
"projects": {
"my-app": {
// ...
"architect": {
"build": {
// ...
"configurations": {
// ...
"de": {
"i18nFile": "src/i18n/de.xliff",
"outputPath": "dist/de",
"i18nFormat": "xlf",
"i18nLocale": "de",
"baseHref": "/de/"
// ...
},
"fr": {
"i18nFile": "src/i18n/fr.xliff",
"outputPath": "dist/fr",
"i18nFormat": "xlf",
"i18nLocale": "fr",
"baseHref": "/fr/",
// ...
}
}
}
}
}
}
For building all apps at once, I then enter npm run buildall
, which executes the following in package.json
:
package.json
{
"name": "my-app",
// ...
"scripts": {
// ...
"buildall": "for lang in de fr;do ng build --configuration=$lang; done"
}
}
which generates all apps in a subdirectory in the dist
folder just fine.
So, to come back to my original question: The provided answer by Exterminator does not fit my needs, since
- I cannot set a fixed locale while bootstrapping
- Injecting
LOCALE_ID
in the constructor is too late since I need the value inapp-routing.module.ts
I hope I explained enough. But maybe I misunderstood something completely. In any case, thanks in advance for any help. I am still learning and I must admit that a few concepts are still blurry to me.