You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: libs/i18n/README.md
+34-12Lines changed: 34 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -33,10 +33,10 @@ In order for a modular translation system, we provide a `TranslationLoader` to e
33
33
Using an array of source paths, the translation loader loads in only the provided translations. If one of the assets has previously been loaded by a different module, the translation will be fetched from the cache.
@@ -46,17 +46,17 @@ If no custom `TranslationLoader` is provided, than the module will use a fall-ba
46
46
47
47
In order to provide a lazy loaded translation system, the translations only get loaded when routing to a specific route.
48
48
49
-
For this purpose we've provided a `TranslationLoaderGuard` which will automatically fetch all translations when the application routes to this route.
49
+
For this purpose we've provided a `NgxI18nTranslationLoaderGuard` which will automatically fetch all translations when the application routes to this route.
50
50
51
-
At any given time you can query the `I18nLoadingService` to see whether the translations have been loaded into the application. There are two Observables provided, being `translationsLoading$` and `translationsFailed$`;
51
+
At any given time you can query the `NgxI18nLoadingService` to see whether the translations have been loaded into the application. There are two Observables provided, being `translationsLoading$` and `translationsFailed$`;
52
52
53
53
## Implementation
54
54
55
-
### I18nGuard
55
+
### NgxI18nRootService and NgxI18nService
56
56
57
-
The`@studiohyperdrive/ngx-i18n`package also provides us with a `I18nGuard` which will automatically prefix the routes of your application with a language parameter.
57
+
As`@studiohyperdrive/ngx-i18n`works with a modular approach, each feature has its own instance of the `NgxI18nService` which contains its own set of translations. When working within a feature and requiring a translation from the translation service, always use this service.
58
58
59
-
The name of the route parameter is `language` by default, but can be overwritten in the config file. The same config file will also provide the opportunity to define a set of permitted languages and a default language for when no language is provided.
59
+
However, the package also has a root service, `NgxI18nRootService`. Because whilst each feature will handle and load its own translations when needed, the application needs one single source of truth to which language is being used. This root service serves as this source of truth, and will also save the current language to the localStorage.
60
60
61
61
### Setup
62
62
@@ -80,13 +80,13 @@ export class AppModule {}
80
80
Next up, we'll take a look at the lazy loaded feature module. In order to then lazy load our translations, we provide a routing module with the `TranslationResolverGuard` on our root path.
In many applications we want the language parameter to be part of the routing. To do so, `@studiohyperdrive/ngx-i18n` provides two guards, the `NgxI18nSetLanguageGuard` and the `NgxI18nGuard`.
136
+
137
+
On one hand, the `NgxI18nSetLanguageGuard` can be set on the base route of the application to automatically set the current language of the application to the route. If there was previously a current language selected, the language will be fetched from the localStorage and will be used. If not, the provided default language will be used.
138
+
139
+
The `NgxI18nGuard` will both ensure that, once the language is set, the correct translations are loaded and will prevent users from altering the url and setting a language that is not available. When linked to a language that is not provided as an available language, this guard will default the language back to the default language.
140
+
141
+
In some setups, the base route of the application does not have a component and currently redirects to a fixed language. In order to circumvent this issue, `@studiohyperdrive/ngx-i18n` also provides a dummy component `NgxI18nEmptyComponent` that can be used instead.
0 commit comments