diff --git a/.npmrc b/.npmrc index e69de29..14a8346 100644 --- a/.npmrc +++ b/.npmrc @@ -0,0 +1 @@ +//registry.npmjs.org/:_authToken=${NPM_TOKEN?} diff --git a/README.md b/README.md index 36c27b3..7e88d94 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ Monorepo for [simply-translate](https://www.npmjs.com/package/simply-translate) ### [simply-translate-angular](projects/translate/README.md) -The core translation library — `TranslateModule`, directive, pipes, `TranslateService`, and dictionary loading (root/lazy). See [projects/translate/README.md](projects/translate/README.md) for setup and usage, and its [changelog](projects/translate/CHANGELOG.md). +The core translation library — `provideTranslate()`, standalone directive and pipes, `TranslateService`, and dictionary loading (root/lazy). See [projects/translate/README.md](projects/translate/README.md) for setup and usage, and its [changelog](projects/translate/CHANGELOG.md). ### [simply-translate-angular-proxy](projects/proxy/README.md) @@ -16,12 +16,12 @@ Optional add-on built on top of `simply-translate-angular` — `TranslateProxy` ```bash npm ci -npm test # tests both libraries once in headless Chrome +npm test # both libraries and the demo app, once in headless Chrome npm run build:prod # builds dist/translate and dist/proxy npm run start:demo # demo app on http://localhost:4800 (run build:prod first) ``` -The proxy library and the demo app import `simply-translate-angular` from `dist/translate`, so build the translate library before working on them. +The proxy library imports `simply-translate-angular` from `dist/translate`, and the demo app imports `simply-translate-angular-proxy` from `dist/proxy`, so build those before working on them — `npm test` does it in order. ## Releasing diff --git a/angular.json b/angular.json index d915929..b190608 100644 --- a/angular.json +++ b/angular.json @@ -4,8 +4,8 @@ "newProjectRoot": "projects", "projects": { "simply-translate": { - "root": "", - "sourceRoot": "src", + "root": "projects/demo", + "sourceRoot": "projects/demo/src", "projectType": "application", "prefix": "app", "schematics": { @@ -18,16 +18,16 @@ "builder": "@angular-devkit/build-angular:browser", "options": { "outputPath": "dist/simply-translate", - "index": "src/index.html", - "main": "src/main.ts", - "polyfills": "src/polyfills.ts", - "tsConfig": "src/tsconfig.app.json", + "index": "projects/demo/src/index.html", + "main": "projects/demo/src/main.ts", + "polyfills": "projects/demo/src/polyfills.ts", + "tsConfig": "projects/demo/tsconfig.app.json", "assets": [ - "src/favicon.ico", - "src/assets" + "projects/demo/src/favicon.ico", + "projects/demo/src/assets" ], "styles": [ - "src/styles.scss" + "projects/demo/src/styles.scss" ], "scripts": [], "vendorChunk": true, @@ -41,8 +41,8 @@ "production": { "fileReplacements": [ { - "replace": "src/environments/environment.ts", - "with": "src/environments/environment.prod.ts" + "replace": "projects/demo/src/environments/environment.ts", + "with": "projects/demo/src/environments/environment.prod.ts" } ], "optimization": true, @@ -87,17 +87,17 @@ "test": { "builder": "@angular-devkit/build-angular:karma", "options": { - "main": "src/test.ts", - "polyfills": "src/polyfills.ts", - "tsConfig": "src/tsconfig.spec.json", - "karmaConfig": "src/karma.conf.cjs", + "main": "projects/demo/src/test.ts", + "polyfills": "projects/demo/src/polyfills.ts", + "tsConfig": "projects/demo/tsconfig.spec.json", + "karmaConfig": "projects/demo/karma.conf.cjs", "styles": [ - "src/styles.scss" + "projects/demo/src/styles.scss" ], "scripts": [], "assets": [ - "src/favicon.ico", - "src/assets" + "projects/demo/src/favicon.ico", + "projects/demo/src/assets" ] } }, @@ -105,8 +105,8 @@ "builder": "@angular-devkit/build-angular:tslint", "options": { "tsConfig": [ - "src/tsconfig.app.json", - "src/tsconfig.spec.json" + "projects/demo/tsconfig.app.json", + "projects/demo/tsconfig.spec.json" ], "exclude": [ "**/node_modules/**" @@ -116,14 +116,14 @@ } }, "simply-translate-e2e": { - "root": "e2e/", + "root": "projects/demo/e2e", "projectType": "application", "prefix": "", "architect": { "e2e": { "builder": "@angular-devkit/build-angular:protractor", "options": { - "protractorConfig": "e2e/protractor.conf.js", + "protractorConfig": "projects/demo/e2e/protractor.conf.js", "devServerTarget": "simply-translate:serve" }, "configurations": { @@ -135,7 +135,7 @@ "lint": { "builder": "@angular-devkit/build-angular:tslint", "options": { - "tsConfig": "e2e/tsconfig.e2e.json", + "tsConfig": "projects/demo/e2e/tsconfig.e2e.json", "exclude": [ "**/node_modules/**" ] diff --git a/package.json b/package.json index 3ca1b42..234d2b1 100644 --- a/package.json +++ b/package.json @@ -10,15 +10,16 @@ "build:dev": "ng build translate && ng build proxy --configuration development", "publish:translate": "npm run build:translate && npm publish ./dist/translate", "publish:proxy": "npm run build:prod && npm publish ./dist/proxy", - "build:demo:dev": "ng build --configuration development", - "build:demo:prod": "node --max-old-space-size=8192 ./node_modules/@angular/cli/bin/ng build --configuration production --aot", - "start:demo": "ng serve --port 4800 --live-reload false", + "build:demo:dev": "ng build simply-translate --configuration development", + "build:demo:prod": "node --max-old-space-size=8192 ./node_modules/@angular/cli/bin/ng build simply-translate --configuration production --aot", + "start:demo": "ng serve simply-translate --port 4800 --live-reload false", "restart:all": "npm run build:prod && npm run start:demo", - "test": "npm run test:translate && npm run build:translate && npm run test:proxy", + "test": "npm run test:translate && npm run build:translate && npm run test:proxy && npm run build:proxy && npm run test:demo", "test:translate": "ng test translate --watch=false --browsers=ChromeHeadless", "test:proxy": "ng test proxy --watch=false --browsers=ChromeHeadless", + "test:demo": "ng test simply-translate --watch=false --browsers=ChromeHeadless", "test:watch": "ng test translate", - "e2e": "ng e2e" + "e2e": "ng e2e simply-translate-e2e" }, "dependencies": { "@angular/animations": "^17.3.12", diff --git a/e2e/protractor.conf.js b/projects/demo/e2e/protractor.conf.js similarity index 100% rename from e2e/protractor.conf.js rename to projects/demo/e2e/protractor.conf.js diff --git a/e2e/src/app.e2e-spec.ts b/projects/demo/e2e/src/app.e2e-spec.ts similarity index 100% rename from e2e/src/app.e2e-spec.ts rename to projects/demo/e2e/src/app.e2e-spec.ts diff --git a/e2e/src/app.po.ts b/projects/demo/e2e/src/app.po.ts similarity index 100% rename from e2e/src/app.po.ts rename to projects/demo/e2e/src/app.po.ts diff --git a/e2e/tsconfig.e2e.json b/projects/demo/e2e/tsconfig.e2e.json similarity index 65% rename from e2e/tsconfig.e2e.json rename to projects/demo/e2e/tsconfig.e2e.json index a6dd622..64c2548 100644 --- a/e2e/tsconfig.e2e.json +++ b/projects/demo/e2e/tsconfig.e2e.json @@ -1,7 +1,7 @@ { - "extends": "../tsconfig.json", + "extends": "../../../tsconfig.json", "compilerOptions": { - "outDir": "../out-tsc/app", + "outDir": "../../../out-tsc/app", "module": "commonjs", "target": "es5", "types": [ diff --git a/src/karma.conf.cjs b/projects/demo/karma.conf.cjs similarity index 93% rename from src/karma.conf.cjs rename to projects/demo/karma.conf.cjs index ee9caa1..c57e806 100644 --- a/src/karma.conf.cjs +++ b/projects/demo/karma.conf.cjs @@ -16,7 +16,7 @@ module.exports = function (config) { clearContext: false // leave Jasmine Spec Runner output visible in browser }, coverageIstanbulReporter: { - dir: require('path').join(__dirname, '../coverage'), + dir: require('path').join(__dirname, '../../coverage'), reports: ['html', 'lcovonly', 'text-summary'], fixWebpackSourcePaths: true }, diff --git a/projects/demo/src/_translate.imports.ts b/projects/demo/src/_translate.imports.ts new file mode 100644 index 0000000..9852e12 --- /dev/null +++ b/projects/demo/src/_translate.imports.ts @@ -0,0 +1,6 @@ +// In a real application this is simply: +// export * from 'simply-translate-angular'; +// It must resolve to the same copy simply-translate-angular-proxy imports (dist/translate, see tsconfig paths): +// pointing at projects/translate/src instead creates a second TranslateService class, and the proxies +// fail with "No provider for TranslateService". Run `npm run build:translate` after changing the library. +export * from 'simply-translate-angular'; diff --git a/src/app/another-dynamic/dynamic.component.ts b/projects/demo/src/app/another-dynamic/dynamic.component.ts similarity index 74% rename from src/app/another-dynamic/dynamic.component.ts rename to projects/demo/src/app/another-dynamic/dynamic.component.ts index 9757c6f..ae5e436 100644 --- a/src/app/another-dynamic/dynamic.component.ts +++ b/projects/demo/src/app/another-dynamic/dynamic.component.ts @@ -1,5 +1,9 @@ import { Component } from '@angular/core'; +import { RouterLink } from '@angular/router'; +import { TRANSLATE_IMPORTS } from 'src/_translate.imports'; @Component({ + standalone: true, + imports: [TRANSLATE_IMPORTS, RouterLink], styles: [ ` :host { diff --git a/projects/demo/src/app/another-dynamic/dynamic.routes.ts b/projects/demo/src/app/another-dynamic/dynamic.routes.ts new file mode 100644 index 0000000..ead925c --- /dev/null +++ b/projects/demo/src/app/another-dynamic/dynamic.routes.ts @@ -0,0 +1,30 @@ +import { HttpClient } from '@angular/common/http'; +import { inject } from '@angular/core'; +import { Routes } from '@angular/router'; +import { forkJoin, map } from 'rxjs'; +import { Dictionary, withTranslateChild } from 'src/_translate.imports'; +import { DynamicComponent } from './dynamic.component'; +import { OtherDynamicComponent } from './other-dynamic.component'; + +function getDictionary(lang: string, client: HttpClient) { + return client.get(`/assets/translations/${lang}.other-dynamic.json`); +} + +export const routes: Routes = [ + { + path: '', + ...withTranslateChild({ + id: 'another_dynamic', + loadDictionaries: ({ lang }) => { + const client = inject(HttpClient); + return forkJoin([getDictionary(lang!, client), getDictionary('ru-RU', client)]).pipe( + map(([current, fallback]) => ({ [lang!]: { ...current, same_key: 'Same in another Dynamic' }, 'ru-RU': fallback })), + ); + }, + }), + children: [ + { path: '', component: DynamicComponent }, + { path: 'other', component: OtherDynamicComponent }, + ], + }, +]; diff --git a/src/app/dynamic/other-dynamic.component.ts b/projects/demo/src/app/another-dynamic/other-dynamic.component.ts similarity index 70% rename from src/app/dynamic/other-dynamic.component.ts rename to projects/demo/src/app/another-dynamic/other-dynamic.component.ts index 7be1288..7acf0d0 100644 --- a/src/app/dynamic/other-dynamic.component.ts +++ b/projects/demo/src/app/another-dynamic/other-dynamic.component.ts @@ -1,5 +1,8 @@ import { Component } from '@angular/core'; +import { TRANSLATE_IMPORTS } from 'src/_translate.imports'; @Component({ + standalone: true, + imports: [TRANSLATE_IMPORTS], selector: 'app-dynamic', template: `

{{ 'hello_user_dynamic' | translateTo: 'ru-RU': { user: 'Basil' } }}

diff --git a/src/app/app.component.html b/projects/demo/src/app/app.component.html similarity index 100% rename from src/app/app.component.html rename to projects/demo/src/app/app.component.html diff --git a/src/app/app.component.scss b/projects/demo/src/app/app.component.scss similarity index 100% rename from src/app/app.component.scss rename to projects/demo/src/app/app.component.scss diff --git a/projects/demo/src/app/app.component.ts b/projects/demo/src/app/app.component.ts new file mode 100644 index 0000000..d81c349 --- /dev/null +++ b/projects/demo/src/app/app.component.ts @@ -0,0 +1,18 @@ +import { Component, ViewEncapsulation } from '@angular/core'; +import { FormControl, ReactiveFormsModule } from '@angular/forms'; +import { RouterLink, RouterLinkActive, RouterOutlet } from '@angular/router'; +import { TRANSLATE_IMPORTS } from 'src/_translate.imports'; +@Component({ + standalone: true, + imports: [TRANSLATE_IMPORTS, ReactiveFormsModule, RouterOutlet, RouterLink, RouterLinkActive], + selector: 'app-root', + templateUrl: './app.component.html', + styleUrls: ['./app.component.scss'], + encapsulation: ViewEncapsulation.None +}) +export class AppComponent { + nameInput = new FormControl(''); + constructor() { + } + +} diff --git a/projects/demo/src/app/app.config.ts b/projects/demo/src/app/app.config.ts new file mode 100644 index 0000000..777426b --- /dev/null +++ b/projects/demo/src/app/app.config.ts @@ -0,0 +1,44 @@ +import { HttpClient, provideHttpClient } from '@angular/common/http'; +import { ApplicationConfig, inject } from '@angular/core'; +import { provideRouter } from '@angular/router'; +import { forkJoin, map } from 'rxjs'; +import { Dictionary, provideTranslate, withLoader, withMiddleware } from 'src/_translate.imports'; +import { routes } from './app.routes'; +import { TranslateProxyMore, TranslateProxyOneMore } from './try-proxy/translate.proxy'; + +function getDictionary(lang: string, client: HttpClient) { + return client.get(`/assets/translations/${lang}.json`); +} + +export const appConfig: ApplicationConfig = { + providers: [ + provideHttpClient(), + provideRouter(routes), + + provideTranslate( + { + lang: 'en-US', + fallbackLang: 'en-US', + }, + + withMiddleware(() => [ + ({ params, result }) => { + if (result.fallingBack) { + console.warn('falling back:', `${params.key.toString()}, [${params.lang}]`); + } + }, + ]), + + // dependencies come from inject(), there is no deps array any more + withLoader(({ lang }) => { + const client = inject(HttpClient); + return forkJoin([getDictionary(lang!, client), getDictionary('ru-RU', client)]).pipe( + map(([current, fallback]) => ({ [lang!]: current, 'ru-RU': fallback })), + ); + }), + ), + + TranslateProxyMore, + TranslateProxyOneMore, + ], +}; diff --git a/projects/demo/src/app/app.routes.ts b/projects/demo/src/app/app.routes.ts new file mode 100644 index 0000000..68eb394 --- /dev/null +++ b/projects/demo/src/app/app.routes.ts @@ -0,0 +1,18 @@ +import { Routes } from '@angular/router'; +import { TranslateToComponent } from './components/translate-to.component'; +import { TryDateComponent } from './components/try-date.component'; +import { SwitchLangComponent } from './switch-lang/switch-lang.component'; +import { AppTranslateComponent } from './translate.component'; +import { TryDirectiveComponent } from './try-directive/try-directive.component'; +import { TryProxyComponent } from './try-proxy/try-proxy.component'; + +export const routes: Routes = [ + { path: 'translate', component: AppTranslateComponent }, + { path: 'translate-to', component: TranslateToComponent }, + { path: 'try-date', component: TryDateComponent }, + { path: 'switch-lang', component: SwitchLangComponent }, + { path: 'try-directive', component: TryDirectiveComponent }, + { path: 'try-proxy', component: TryProxyComponent }, + { path: 'dynamic', loadChildren: () => import('./dynamic/dynamic.routes').then((m) => m.routes) }, + { path: 'another-dynamic', loadChildren: () => import('./another-dynamic/dynamic.routes').then((m) => m.routes) }, +]; diff --git a/projects/demo/src/app/app.spec.ts b/projects/demo/src/app/app.spec.ts new file mode 100644 index 0000000..f7a884b --- /dev/null +++ b/projects/demo/src/app/app.spec.ts @@ -0,0 +1,102 @@ +import { provideHttpClientTesting, HttpTestingController } from '@angular/common/http/testing'; +import { ApplicationInitStatus } from '@angular/core'; +import { TestBed } from '@angular/core/testing'; +import { RouterTestingHarness } from '@angular/router/testing'; +import { TranslateRootService } from 'src/_translate.imports'; +import { appConfig } from './app.config'; +import { DynamicComponent } from './dynamic/dynamic.component'; +import { DynamicComponent as AnotherDynamicComponent } from './another-dynamic/dynamic.component'; +import { TranslateProxyMore } from './try-proxy/translate.proxy'; + +import enUS from '../assets/translations/en-US.json'; +import ruRU from '../assets/translations/ru-RU.json'; +import enUSDynamic from '../assets/translations/en-US.dynamic.json'; +import ruRUDynamic from '../assets/translations/ru-RU.dynamic.json'; +import enUSOther from '../assets/translations/en-US.other-dynamic.json'; +import ruRUOther from '../assets/translations/ru-RU.other-dynamic.json'; + +const ROOT_DICTIONARIES: Record = { + '/assets/translations/en-US.json': enUS, + '/assets/translations/ru-RU.json': ruRU, + '/assets/translations/en-US.dynamic.json': enUSDynamic, + '/assets/translations/ru-RU.dynamic.json': ruRUDynamic, + '/assets/translations/en-US.other-dynamic.json': enUSOther, + '/assets/translations/ru-RU.other-dynamic.json': ruRUOther, +}; + +describe('demo application wiring', () => { + let http: HttpTestingController; + let harness: RouterTestingHarness; + + /** Answers every dictionary request the application has made so far. */ + function flushDictionaries() { + http.match(() => true).forEach((request) => { + const body = ROOT_DICTIONARIES[request.request.url]; + expect(body).withContext(`unexpected request ${request.request.url}`).toBeDefined(); + request.flush(body); + }); + } + + /** + * Navigates and keeps answering dictionary requests until the navigation settles — + * the lazy chunk, the child providers and the resolver each take a turn of their own. + */ + async function navigate(url: string, component: new (...args: any[]) => T): Promise { + let settled = false; + const navigation = harness.navigateByUrl(url, component).then((instance) => ((settled = true), instance)); + + for (let i = 0; i < 50 && !settled; i++) { + await new Promise((resolve) => setTimeout(resolve, 0)); + flushDictionaries(); + } + + return navigation; + } + + beforeEach(async () => { + TestBed.configureTestingModule({ + providers: [...appConfig.providers, provideHttpClientTesting()], + }); + + // injecting creates the test module, which runs the APP_INITIALIZER + http = TestBed.inject(HttpTestingController); + flushDictionaries(); + await TestBed.inject(ApplicationInitStatus).donePromise; + + harness = await RouterTestingHarness.create(); + }); + + afterEach(() => http.verify()); + + it('loads the root dictionaries during initialization', () => { + expect(TestBed.inject(TranslateRootService).translate('welcome_to_app')).toBe('Welcome to Translation Testing Application'); + expect(TestBed.inject(TranslateRootService).translate('same_key')).toBe('Same in the Root'); + }); + + it('gives the proxies the TranslateService that provideTranslate() registered', () => { + // fails with NullInjectorError when the demo and the proxy package load different copies of the library + expect(() => TestBed.inject(TranslateProxyMore)).not.toThrow(); + http.match((request) => request.url.startsWith('/assets/translations/proxy/')).forEach((request) => request.flush({})); + }); + + it('scopes the lazy "dynamic" route to its own dictionary', async () => { + await navigate('/dynamic', DynamicComponent); + harness.detectChanges(); + + const text = harness.routeNativeElement!.textContent!; + expect(text).toContain('Same in the Dynamic'); + expect(text).toContain('Same Dic'); + // keys the scope does not define still come from the root + expect(text).toContain('Only in the Root'); + }); + + it('scopes the lazy "another-dynamic" route to its own dictionary', async () => { + await navigate('/another-dynamic', AnotherDynamicComponent); + harness.detectChanges(); + + const text = harness.routeNativeElement!.textContent!; + expect(text).toContain('Same in another Dynamic'); + expect(text).toContain('Same other Dic'); + expect(text).toContain('Only in the Root'); + }); +}); diff --git a/src/app/components/translate-to.component.html b/projects/demo/src/app/components/translate-to.component.html similarity index 100% rename from src/app/components/translate-to.component.html rename to projects/demo/src/app/components/translate-to.component.html diff --git a/src/app/components/translate-to.component.ts b/projects/demo/src/app/components/translate-to.component.ts similarity index 71% rename from src/app/components/translate-to.component.ts rename to projects/demo/src/app/components/translate-to.component.ts index 991c6f4..edf1645 100644 --- a/src/app/components/translate-to.component.ts +++ b/projects/demo/src/app/components/translate-to.component.ts @@ -1,8 +1,12 @@ import { ChangeDetectorRef, Component } from '@angular/core'; -import { ActivatedRoute, Router } from '@angular/router'; +import { ActivatedRoute } from '@angular/router'; import { Subscription } from 'rxjs'; +import { TRANSLATE_IMPORTS } from 'src/_translate.imports'; +import { VisitsComponent } from './visits/visits.component'; @Component({ + standalone: true, + imports: [TRANSLATE_IMPORTS, VisitsComponent], selector: 'translate-to', templateUrl: './translate-to.component.html', }) diff --git a/src/app/components/try-date.component.ts b/projects/demo/src/app/components/try-date.component.ts similarity index 78% rename from src/app/components/try-date.component.ts rename to projects/demo/src/app/components/try-date.component.ts index 6578fe6..263f917 100644 --- a/src/app/components/try-date.component.ts +++ b/projects/demo/src/app/components/try-date.component.ts @@ -1,8 +1,10 @@ import { Component, OnInit } from '@angular/core'; -import { ActivatedRoute, Router } from '@angular/router'; -import { Subscription } from 'rxjs'; +import { FormsModule } from '@angular/forms'; +import { TRANSLATE_IMPORTS } from 'src/_translate.imports'; @Component({ + standalone: true, + imports: [TRANSLATE_IMPORTS, FormsModule], selector: 'app-more', template: `
diff --git a/src/app/components/visits/visits.component.ts b/projects/demo/src/app/components/visits/visits.component.ts similarity index 93% rename from src/app/components/visits/visits.component.ts rename to projects/demo/src/app/components/visits/visits.component.ts index 1bd2e6d..f72a0d6 100644 --- a/src/app/components/visits/visits.component.ts +++ b/projects/demo/src/app/components/visits/visits.component.ts @@ -1,9 +1,13 @@ +import { NgIf } from '@angular/common'; import { ChangeDetectionStrategy, ChangeDetectorRef, Component, Input, OnInit } from '@angular/core'; import { ActivatedRoute, Router } from '@angular/router'; import { Subscription } from 'rxjs'; +import { TRANSLATE_IMPORTS } from 'src/_translate.imports'; import { StorageService } from '../../storage.service'; @Component({ + standalone: true, + imports: [TRANSLATE_IMPORTS, NgIf], selector: 'app-visits', template: `
diff --git a/src/app/dynamic/dynamic.component.ts b/projects/demo/src/app/dynamic/dynamic.component.ts similarity index 72% rename from src/app/dynamic/dynamic.component.ts rename to projects/demo/src/app/dynamic/dynamic.component.ts index fdd0020..6724e80 100644 --- a/src/app/dynamic/dynamic.component.ts +++ b/projects/demo/src/app/dynamic/dynamic.component.ts @@ -1,5 +1,9 @@ import { Component } from '@angular/core'; +import { RouterLink } from '@angular/router'; +import { TRANSLATE_IMPORTS } from 'src/_translate.imports'; @Component({ + standalone: true, + imports: [TRANSLATE_IMPORTS, RouterLink], styles: [ ` :host { diff --git a/projects/demo/src/app/dynamic/dynamic.routes.ts b/projects/demo/src/app/dynamic/dynamic.routes.ts new file mode 100644 index 0000000..ceed6dc --- /dev/null +++ b/projects/demo/src/app/dynamic/dynamic.routes.ts @@ -0,0 +1,36 @@ +import { HttpClient } from '@angular/common/http'; +import { inject } from '@angular/core'; +import { Routes } from '@angular/router'; +import { forkJoin, map } from 'rxjs'; +import { Dictionary, withTranslateChild } from 'src/_translate.imports'; +import { DynamicComponent } from './dynamic.component'; +import { OtherDynamicComponent } from './other-dynamic.component'; + +export function getDictionary(lang: string, client: HttpClient) { + return client.get(`/assets/translations/${lang}.dynamic.json`); +} + +export const routes: Routes = [ + { + path: '', + // scope providers + the resolver that waits for them, for this route and its children + ...withTranslateChild({ + id: 'dynamic', + dictionaries: { + 'en-US': { + same_key: 'Same in the Dynamic', + }, + }, + loadDictionaries: ({ lang }) => { + const client = inject(HttpClient); + return forkJoin([getDictionary(lang!, client), getDictionary('ru-RU', client)]).pipe( + map(([current, fallback]) => ({ [lang!]: current, 'ru-RU': fallback })), + ); + }, + }), + children: [ + { path: '', component: DynamicComponent }, + { path: 'other', component: OtherDynamicComponent }, + ], + }, +]; diff --git a/src/app/another-dynamic/other-dynamic.component.ts b/projects/demo/src/app/dynamic/other-dynamic.component.ts similarity index 70% rename from src/app/another-dynamic/other-dynamic.component.ts rename to projects/demo/src/app/dynamic/other-dynamic.component.ts index 7be1288..7acf0d0 100644 --- a/src/app/another-dynamic/other-dynamic.component.ts +++ b/projects/demo/src/app/dynamic/other-dynamic.component.ts @@ -1,5 +1,8 @@ import { Component } from '@angular/core'; +import { TRANSLATE_IMPORTS } from 'src/_translate.imports'; @Component({ + standalone: true, + imports: [TRANSLATE_IMPORTS], selector: 'app-dynamic', template: `

{{ 'hello_user_dynamic' | translateTo: 'ru-RU': { user: 'Basil' } }}

diff --git a/src/app/more/title.directive.ts b/projects/demo/src/app/more/title.directive.ts similarity index 95% rename from src/app/more/title.directive.ts rename to projects/demo/src/app/more/title.directive.ts index a9e9df4..b2db622 100644 --- a/src/app/more/title.directive.ts +++ b/projects/demo/src/app/more/title.directive.ts @@ -1,6 +1,7 @@ import { Directive, ElementRef, Input, OnChanges, SimpleChanges } from '@angular/core'; @Directive({ + standalone: true, selector: '[appTitle]', }) export class TitleDirective implements OnChanges { diff --git a/src/app/storage.service.ts b/projects/demo/src/app/storage.service.ts similarity index 100% rename from src/app/storage.service.ts rename to projects/demo/src/app/storage.service.ts diff --git a/src/app/switch-lang/switch-lang.component.ts b/projects/demo/src/app/switch-lang/switch-lang.component.ts similarity index 81% rename from src/app/switch-lang/switch-lang.component.ts rename to projects/demo/src/app/switch-lang/switch-lang.component.ts index ed82f8a..e3f8f9c 100644 --- a/src/app/switch-lang/switch-lang.component.ts +++ b/projects/demo/src/app/switch-lang/switch-lang.component.ts @@ -1,8 +1,12 @@ +import { AsyncPipe, NgFor } from '@angular/common'; import { Component } from '@angular/core'; +import { FormsModule } from '@angular/forms'; import { Observable } from 'rxjs'; -import { TranslateRootService, TranslateService } from 'src/_translate.imports'; +import { TRANSLATE_IMPORTS, TranslateRootService, TranslateService } from 'src/_translate.imports'; @Component({ + standalone: true, + imports: [TRANSLATE_IMPORTS, FormsModule, NgFor, AsyncPipe], selector: 'app-switch-lang', template: ` diff --git a/src/app/try-proxy/schema.ts b/projects/demo/src/app/try-proxy/schema.ts similarity index 100% rename from src/app/try-proxy/schema.ts rename to projects/demo/src/app/try-proxy/schema.ts diff --git a/src/app/try-proxy/translate.proxy.ts b/projects/demo/src/app/try-proxy/translate.proxy.ts similarity index 100% rename from src/app/try-proxy/translate.proxy.ts rename to projects/demo/src/app/try-proxy/translate.proxy.ts diff --git a/src/app/try-proxy/try-proxy.component.ts b/projects/demo/src/app/try-proxy/try-proxy.component.ts similarity index 85% rename from src/app/try-proxy/try-proxy.component.ts rename to projects/demo/src/app/try-proxy/try-proxy.component.ts index 945d4a1..21c1857 100644 --- a/src/app/try-proxy/try-proxy.component.ts +++ b/projects/demo/src/app/try-proxy/try-proxy.component.ts @@ -1,8 +1,12 @@ +import { AsyncPipe, NgFor } from '@angular/common'; import { Component, computed, signal } from '@angular/core'; -import { TranslateRootService } from 'src/_translate.imports'; -import { TranslateProxyCommon, TranslateProxyMore, TranslateProxyOneMore } from './translate.proxy'; +import { FormsModule } from '@angular/forms'; +import { TRANSLATE_IMPORTS, TranslateRootService } from 'src/_translate.imports'; +import { TranslateProxyMore, TranslateProxyOneMore } from './translate.proxy'; @Component({ + standalone: true, + imports: [TRANSLATE_IMPORTS, FormsModule, NgFor, AsyncPipe], selector: 'app-try-proxy', template: `