Skip to content

Commit

Permalink
replace ngx-translate with transloco
Browse files Browse the repository at this point in the history
use transloco as better maintained tool;
  • Loading branch information
antonborisoff committed Feb 20, 2024
1 parent 9b2c5c5 commit bcc28c0
Show file tree
Hide file tree
Showing 14 changed files with 415 additions and 164 deletions.
426 changes: 301 additions & 125 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
"@angular/platform-browser": "^17.1.0",
"@angular/platform-browser-dynamic": "^17.1.0",
"@angular/router": "^17.1.0",
"@ngx-translate/core": "^15.0.0",
"@ngx-translate/http-loader": "^8.0.0",
"@ngneat/transloco": "^6.0.4",
"rxjs": "~7.8.0",
"tslib": "^2.3.0",
"zone.js": "~0.14.3"
Expand All @@ -47,7 +46,6 @@
"karma-coverage": "~2.2.0",
"karma-jasmine": "~5.1.0",
"karma-jasmine-html-reporter": "~2.1.0",
"ngx-translate-testing": "^7.0.0",
"typescript": "~5.3.2"
}
}
2 changes: 1 addition & 1 deletion src/app/app.component.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div class="componentBorder">
<h1>{{ 'HELLO' | translate:{value: title} }}</h1>
<h1>{{ 'app.HELLO' | transloco:{value: title} }}</h1>
<p>Congratulations! Your app is running. 🎉</p>

<router-outlet />
Expand Down
14 changes: 6 additions & 8 deletions src/app/app.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,16 @@ import {
AppComponent
} from './app.component'
import {
TranslateTestingModule
} from 'ngx-translate-testing'
import en from '../assets/i18n/en.json'
getTranslocoTestingModule
} from '../transloco/transloco-testing'
import en from './i18n/en.json'

describe('AppComponent', () => {
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [
AppComponent,
TranslateTestingModule
.withTranslations('en', en)
.withDefaultLanguage('en')
getTranslocoTestingModule(AppComponent, en)
],
providers: [{
provide: ComponentFixtureAutoDetect,
Expand All @@ -38,9 +36,9 @@ describe('AppComponent', () => {
expect(app.title).toEqual('contracts-angular')
})

it('should render title', () => {
it('should render translated title', () => {
const fixture = TestBed.createComponent(AppComponent)
const compiled = fixture.nativeElement as HTMLElement
expect(compiled.querySelector('h1')?.textContent).toContain('Hello, contracts-angular')
expect(compiled.querySelector('h1')?.textContent).toContain('Hello, contracts-angular component en')
})
})
21 changes: 17 additions & 4 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,32 @@ import {
RouterOutlet
} from '@angular/router'
import {
TranslateModule
} from '@ngx-translate/core'
TranslocoPipe,
provideTranslocoScope
} from '@ngneat/transloco'
import {
getTranslocoInlineLoader
} from '../transloco/transloco-loaders'

const COMPONENT_TRANSLOCO_SCOPE = 'app'
@Component({
selector: 'app-root',
standalone: true,
imports: [
RouterOutlet,
TranslateModule
TranslocoPipe
],
templateUrl: './app.component.html',
styleUrl: './app.component.css'
styleUrl: './app.component.css',
providers: [provideTranslocoScope({
scope: COMPONENT_TRANSLOCO_SCOPE,
loader: getTranslocoInlineLoader((lang: string) => () => import(`./i18n/${lang}.json`))
})]
})
export class AppComponent {
title = 'contracts-angular'

static getTranslocoScope() {
return COMPONENT_TRANSLOCO_SCOPE
}
}
39 changes: 19 additions & 20 deletions src/app/app.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
ApplicationConfig,
importProvidersFrom
isDevMode
} from '@angular/core'
import {
provideRouter
Expand All @@ -10,32 +10,31 @@ import {
routes
} from './app.routes'
import {
TranslateLoader,
TranslateModule
} from '@ngx-translate/core'
import {
HttpClient,
provideHttpClient
} from '@angular/common/http'
import {
TranslateHttpLoader
} from '@ngx-translate/http-loader'

export function HttpLoaderFactory(http: HttpClient) {
return new TranslateHttpLoader(http)
}
TranslocoHttpLoader
} from '../transloco/transloco-loaders'
import {
provideTransloco
} from '@ngneat/transloco'
import {
DEFAULT_LANGUAGE,
SUPPORTED_LANGUAGES
} from '../transloco/transloco-languages'

export const appConfig: ApplicationConfig = {
providers: [
provideRouter(routes),
provideHttpClient(),
importProvidersFrom(TranslateModule.forRoot({
defaultLanguage: 'en',
loader: {
provide: TranslateLoader,
useFactory: HttpLoaderFactory,
deps: [HttpClient]
}
}))
provideTransloco({
config: {
availableLangs: SUPPORTED_LANGUAGES,
defaultLang: DEFAULT_LANGUAGE,
reRenderOnLangChange: false,
prodMode: !isDevMode()
},
loader: TranslocoHttpLoader
})
]
}
3 changes: 3 additions & 0 deletions src/app/i18n/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"HELLO": "Hello, {{value}} component en"
}
3 changes: 3 additions & 0 deletions src/app/i18n/ru.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"HELLO": "Hello, {{value}} component ru"
}
4 changes: 1 addition & 3 deletions src/assets/i18n/en.json
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
{
"HELLO": "Hello, {{value}}"
}
{}
1 change: 1 addition & 0 deletions src/assets/i18n/ru.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
3 changes: 3 additions & 0 deletions src/transloco/transloco-interfaces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface TranslocoLocalScope {
getTranslocoScope: () => string
}
5 changes: 5 additions & 0 deletions src/transloco/transloco-languages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const SUPPORTED_LANGUAGES = [
'en',
'ru'
]
export const DEFAULT_LANGUAGE = SUPPORTED_LANGUAGES[0]
32 changes: 32 additions & 0 deletions src/transloco/transloco-loaders.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {
Injectable
} from '@angular/core'
import {
InlineLoader,
Translation,
TranslocoLoader
} from '@ngneat/transloco'
import {
HttpClient
} from '@angular/common/http'
import {
SUPPORTED_LANGUAGES
} from './transloco-languages'

@Injectable({
providedIn: 'root'
})
export class TranslocoHttpLoader implements TranslocoLoader {
constructor(private http: HttpClient) {}

getTranslation(lang: string) {
return this.http.get<Translation>(`/assets/i18n/${lang}.json`)
}
}

export function getTranslocoInlineLoader(getLanguageLoader: (lang: string) => (() => Promise<Translation>)) {
return SUPPORTED_LANGUAGES.reduce<InlineLoader>((acc: InlineLoader, lang: string) => {
acc[lang] = getLanguageLoader(lang)
return acc
}, {} as InlineLoader)
}
22 changes: 22 additions & 0 deletions src/transloco/transloco-testing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {
Translation,
TranslocoTestingModule
} from '@ngneat/transloco'
import {
TranslocoLocalScope
} from './transloco-interfaces'

export function getTranslocoTestingModule(componentClass: TranslocoLocalScope, translations: Translation) {
const testingLang = 'en'
const scope = componentClass.getTranslocoScope()
return TranslocoTestingModule.forRoot({
langs: {
[`${scope}/${testingLang}`]: translations
},
translocoConfig: {
defaultLang: testingLang,
availableLangs: [testingLang]
},
preloadLangs: true
})
}

0 comments on commit bcc28c0

Please sign in to comment.