Skip to content

Latest commit

 

History

History

README.md

Tolgee for Angular
The Tolgee i18n SDK for Angular

Logo

Tolgee is an open-source alternative to Crowdin, Phrase, or Lokalise

test workflow @tolgee/ngx version typescript Types Typescript Licence GitHub Stars

GitHub Stars GitHub Discussions Docker Read the Docs Slack Dev.to YouTube LinkedIn X

What's Tolgee for Angular?

Angular integration library of Tolgee. With this package. It's super simple to add i18n to your Angular app! For more information about using Tolgee with Angular, visit the docs 📖.

Quick links

Installation

npm install @tolgee/ngx

Then register Tolgee in your application config using provideTolgee.

import { ApplicationConfig } from '@angular/core';
import { provideTolgee, Tolgee, DevTools, TolgeeOptions } from '@tolgee/ngx';

const tolgeeConfig: TolgeeOptions = {
  availableLanguages: ['en', 'cs'],
  defaultLanguage: 'en',
  fallbackLanguage: 'en',

  // for development
  apiUrl: environment.tolgeeApiUrl,
  apiKey: environment.tolgeeApiKey,

  // for production
  staticData: {
    en: () => import('../i18n/en.json').then((m) => m.default),
    cs: () => import('../i18n/cs.json').then((m) => m.default),
  },
};

export const appConfig: ApplicationConfig = {
  providers: [
    provideTolgee(() => Tolgee().use(DevTools()).init(tolgeeConfig)),
  ],
};

Usage

In standalone components, import TranslatePipe and TDirective where you use them.

import { Component } from '@angular/core';
import { TDirective, TranslatePipe } from '@tolgee/ngx';

@Component({
  template: `
    <h1>{{ 'hello_world' | translate }}</h1>
    <h2 t key="providing_default_values"></h2>
  `,
  imports: [TranslatePipe, TDirective],
})
export class AppComponent {}

Imperative translation and language switching are available via TranslateService.

import { Component, inject } from '@angular/core';
import { TranslateService } from '@tolgee/ngx';

@Component({
  template: `{{ currentLanguage }}`,
})
export class LanguageSwitcherComponent {
  private readonly translateService = inject(TranslateService);

  currentLanguage = this.translateService.language;

  async switchToCzech() {
    await this.translateService.changeLanguage('cs');
    this.currentLanguage = this.translateService.language;
  }
}

For lazy routes with namespaced translations, use namespaceResolver to load the namespace before the route renders.

import { Routes } from '@angular/router';
import { namespaceResolver } from '@tolgee/ngx';

export const routes: Routes = [
  {
    path: 'lazy',
    loadComponent: () => import('./lazy/lazy.component'),
    data: { tolgeeNamespace: 'namespaced' },
    resolve: {
      _namespace: namespaceResolver,
    },
  },
];

Translating using pipe:

<h1>{{&#39;hello_world&#39; | translate}}</h1>

Or using t attribute

<h1 t key="providing_default_values"></h1>

Prerequisites

  1. You have some Angular-based project
  2. You have generated an API key from Tolgee Cloud or a self-hosted Tolgee instance.

Why to use Tolgee?

Tolgee saves a lot of time you would spend on localization tasks otherwise. It enables you to provide perfectly translated software.

Features

  • All-in-one localization solution for your JS application 🙌
  • Out-of-box in-context localization 🎉
  • Automated screenshot generation 📷
  • Translation management platform 🎈
  • Open-source 🔥

Frame 47 Read more on the Tolgee website

Development

We welcome your PRs.

To develop the package locally:

  1. Clone the repository
  2. Install the packages in the repository root
pnpm install
  1. Run the development script
pnpm develop:ngx

This runs the development suite of this monorepo for the ngx integration. The changes in each dependency package are automatically built and propagated to the test application, which you can open and play within the browser.

Testing

To run Jest tests of this package, execute

npm run test

In the /packages/ngx directory.

End-to-end (e2e) testing

To run the e2e tests, simply execute

pnpm run e2e run ngx

To open and play with e2e tests, run:

pnpm run e2e open ngx

Contributors

contributors