Skip to content

Commit

Permalink
enable i18n
Browse files Browse the repository at this point in the history
  • Loading branch information
antonborisoff committed Feb 16, 2024
1 parent d91aeea commit 0b03b2f
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 9 deletions.
45 changes: 45 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
"@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",
"rxjs": "~7.8.0",
"tslib": "^2.3.0",
"zone.js": "~0.14.3"
Expand All @@ -43,6 +45,7 @@
"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"
}
}
5 changes: 3 additions & 2 deletions src/app/app.component.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<div>
<h1>Hello, {{ title }}</h1>
<h1>{{ 'HELLO' | translate:{value: title} }}</h1>
<p>Congratulations! Your app is running. 🎉</p>

<router-outlet />
</div>

<router-outlet />
13 changes: 10 additions & 3 deletions src/app/app.component.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import { TestBed } from '@angular/core/testing'
import { ComponentFixtureAutoDetect, TestBed } from '@angular/core/testing'
import { AppComponent } from './app.component'
import { TranslateTestingModule } from 'ngx-translate-testing';

Check failure on line 3 in src/app/app.component.spec.ts

View workflow job for this annotation

GitHub Actions / execute-pr-checks

Extra semicolon
import en from '../assets/i18n/en.json'

describe('AppComponent', () => {
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [AppComponent],
imports: [
AppComponent,

Check failure on line 10 in src/app/app.component.spec.ts

View workflow job for this annotation

GitHub Actions / execute-pr-checks

Trailing spaces not allowed
TranslateTestingModule
.withTranslations('en', en)
.withDefaultLanguage('en')

Check failure on line 13 in src/app/app.component.spec.ts

View workflow job for this annotation

GitHub Actions / execute-pr-checks

Missing trailing comma
],

Check failure on line 14 in src/app/app.component.spec.ts

View workflow job for this annotation

GitHub Actions / execute-pr-checks

Expected indentation of 6 spaces but found 8
providers: [{ provide: ComponentFixtureAutoDetect, useValue: true }]

Check failure on line 15 in src/app/app.component.spec.ts

View workflow job for this annotation

GitHub Actions / execute-pr-checks

Missing trailing comma
}).compileComponents()
})

Expand All @@ -22,7 +30,6 @@ describe('AppComponent', () => {

it('should render title', () => {
const fixture = TestBed.createComponent(AppComponent)
fixture.detectChanges()
const compiled = fixture.nativeElement as HTMLElement
expect(compiled.querySelector('h1')?.textContent).toContain('Hello, contracts-angular')
})
Expand Down
3 changes: 2 additions & 1 deletion src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Component } from '@angular/core'
import { RouterOutlet } from '@angular/router'
import { TranslateModule } from '@ngx-translate/core'

@Component({
selector: 'app-root',
standalone: true,
imports: [RouterOutlet],
imports: [RouterOutlet, TranslateModule],
templateUrl: './app.component.html',
styleUrl: './app.component.css',
})
Expand Down
22 changes: 20 additions & 2 deletions src/app/app.config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@
import { ApplicationConfig } from '@angular/core'
import { ApplicationConfig, importProvidersFrom } from '@angular/core'
import { provideRouter } from '@angular/router'

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)
}

export const appConfig: ApplicationConfig = {
providers: [provideRouter(routes)],
providers: [
provideRouter(routes),
provideHttpClient(),
importProvidersFrom(TranslateModule.forRoot({
defaultLanguage: 'en',
loader: {
provide: TranslateLoader,
useFactory: HttpLoaderFactory,
deps: [HttpClient],
},
})),
],
}
3 changes: 3 additions & 0 deletions src/assets/i18n/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"HELLO": "Hello, {{value}}"
}
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"lib": [
"ES2022",
"dom"
]
],
"resolveJsonModule": true
},
"angularCompilerOptions": {
"enableI18nLegacyMessageIdFormat": false,
Expand Down

0 comments on commit 0b03b2f

Please sign in to comment.