@mmstack/translate 19.2.5 → 19.2.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +44 -22
- package/lib/translation.store.d.ts +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -61,33 +61,55 @@ import { provideIntlConfig } from '@mmstack/translate';
|
|
|
61
61
|
|
|
62
62
|
const appConfig: Providers = [
|
|
63
63
|
provideIntlConfig({
|
|
64
|
-
defaultLocale: '
|
|
64
|
+
defaultLocale: 'en-US', // defaults to 'en-US' if nothing is provided
|
|
65
65
|
}),
|
|
66
66
|
];
|
|
67
67
|
```
|
|
68
68
|
|
|
69
69
|
```typescript
|
|
70
|
-
|
|
71
|
-
import { ActivatedRoute } from '@angular/router';
|
|
70
|
+
// DEMO impl, how you actually provide LOCALE_ID & what it's based on is up to you, it just has to be available when the resolvers are called
|
|
72
71
|
|
|
73
|
-
|
|
72
|
+
import { Component, LOCALE_ID } from '@angular/core';
|
|
74
73
|
|
|
75
74
|
@Component({
|
|
76
75
|
selector: 'app-locale-shell',
|
|
77
|
-
|
|
78
|
-
{
|
|
79
|
-
provide: LOCALE_ID,
|
|
80
|
-
useFactory: (route: ActivatedRoute) => {
|
|
81
|
-
const locale = route.snapshot.paramMap.get('locale') || 'en-US';
|
|
82
|
-
if (KNOWN_LOCALES.includes(locale)) return locale;
|
|
83
|
-
return 'en-US';
|
|
84
|
-
},
|
|
85
|
-
deps: [ActivatedRoute],
|
|
86
|
-
},
|
|
87
|
-
],
|
|
88
|
-
template: `<ng-content />`,
|
|
76
|
+
template: `<router-outlet />`,
|
|
89
77
|
})
|
|
90
78
|
export class LocaleShellComponent {}
|
|
79
|
+
|
|
80
|
+
// app.routes.ts
|
|
81
|
+
import { Routes, ActivatedRouteSnapshot } from '@angular/router';
|
|
82
|
+
import { Injectable, LOCALE_ID } from '@angular/core';
|
|
83
|
+
import { QuoteComponent } from './quote.component';
|
|
84
|
+
|
|
85
|
+
@Injectable({
|
|
86
|
+
providedIn: 'root',
|
|
87
|
+
})
|
|
88
|
+
export class LocaleStore {
|
|
89
|
+
locale = 'en-US';
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export const routes: Routes = [
|
|
93
|
+
{
|
|
94
|
+
path: ':locale',
|
|
95
|
+
component: LocaleShellComponent,
|
|
96
|
+
resolve: {
|
|
97
|
+
localeId: (route: ActivatedRouteSnapshot) => {
|
|
98
|
+
return route.params['locale'] || 'en-US';
|
|
99
|
+
},
|
|
100
|
+
},
|
|
101
|
+
providers: [
|
|
102
|
+
{
|
|
103
|
+
provide: LOCALE_ID,
|
|
104
|
+
useFactory: (store: LocaleStore) => {
|
|
105
|
+
return store.locale;
|
|
106
|
+
},
|
|
107
|
+
deps: [LocaleStore],
|
|
108
|
+
},
|
|
109
|
+
],
|
|
110
|
+
loadChildren: () => import('./quote.routes').then((m) => m.QUOTE_ROUTES),
|
|
111
|
+
},
|
|
112
|
+
];
|
|
91
113
|
```
|
|
92
114
|
|
|
93
115
|
_Note:_ `@mmstack/translate` relies on Angular's `LOCALE_ID` provider. You must provide a value for it. How you determine this value (e.g., hardcoded, from server config, from URL segment via a factory provider) is up to your application's architecture. The library assumes this `LOCALE_ID` is static for the duration of the application session and requires a page refresh to change.
|
|
@@ -129,7 +151,7 @@ export const createQuoteTranslation = ns.createTranslation;
|
|
|
129
151
|
import { createQuoteTranslation } from './quote.namespace';
|
|
130
152
|
|
|
131
153
|
// shape is typesafe (errors if you have missing or additional keys)
|
|
132
|
-
export default createQuoteTranslation({
|
|
154
|
+
export default createQuoteTranslation('sl-SI', {
|
|
133
155
|
pageTitle: 'Znani Citati',
|
|
134
156
|
greeting: 'Zdravo {name}!',
|
|
135
157
|
detail: {
|
|
@@ -152,10 +174,10 @@ import q from './quote.namespace';
|
|
|
152
174
|
// Register the namespace
|
|
153
175
|
// Example: packages/quote/src/lib/quote.t.ts
|
|
154
176
|
const r = registerNamespace(
|
|
155
|
-
() => import('./quote.namespace
|
|
177
|
+
() => import('./quote.namespace').then((m) => m.default), // Default locale's compiled translation (functions as fallback if no locale of type provided)
|
|
156
178
|
{
|
|
157
179
|
// Map other locales to promise factories (dynamic imports)
|
|
158
|
-
'sl-SI': () => import('./quote-sl.translation
|
|
180
|
+
'sl-SI': () => import('./quote-sl.translation').then((m) => m.default),
|
|
159
181
|
},
|
|
160
182
|
);
|
|
161
183
|
|
|
@@ -167,10 +189,10 @@ export const resolveQuoteTranslations = r.resolveNamespaceTranslation;
|
|
|
167
189
|
import { type Routes } from '@angular/router';
|
|
168
190
|
import { resolveQuoteTranslations } from './quote.t';
|
|
169
191
|
|
|
170
|
-
|
|
171
|
-
|
|
192
|
+
// quote.routes.ts
|
|
193
|
+
export const QUOTE_ROUTES: Routes = [
|
|
172
194
|
{
|
|
173
|
-
// ...
|
|
195
|
+
// ... component at or above where the translations need to be available
|
|
174
196
|
resolve: {
|
|
175
197
|
resolveQuoteTranslations,
|
|
176
198
|
},
|
|
@@ -2,7 +2,7 @@ import { Provider } from '@angular/core';
|
|
|
2
2
|
import { IntlConfig } from '@formatjs/intl';
|
|
3
3
|
import * as i0 from "@angular/core";
|
|
4
4
|
export declare function provideIntlConfig(config: Omit<IntlConfig, 'locale' | 'messages'>): Provider;
|
|
5
|
-
export declare function injectIntlConfig(): Omit<IntlConfig, "
|
|
5
|
+
export declare function injectIntlConfig(): Omit<IntlConfig, "locale" | "messages"> | undefined;
|
|
6
6
|
export declare function injectDefaultLocale(): string;
|
|
7
7
|
export declare class TranslationStore {
|
|
8
8
|
private readonly cache;
|