Convert Figma logo to code with AI

ngx-translate logocore

The internationalization (i18n) library for Angular

4,650
591
4,650
95

Top Related Projects

8,358

i18next: learn once - translate everywhere

:globe_with_meridians: Internationalization plugin for Vue.js

14,610

The monorepo home to all of the FormatJS related libraries, most notably react-intl.

Give your JavaScript the ability to speak many languages.

🌍 📖 A readable, automated, and optimized (2 kb) internationalization for JavaScript

Quick Overview

ngx-translate/core is an internationalization (i18n) library for Angular applications. It provides a simple way to manage translations in your Angular projects, allowing for easy language switching and dynamic content localization.

Pros

  • Easy integration with Angular projects
  • Supports multiple file formats for translations (JSON, YAML, etc.)
  • Allows for dynamic language switching at runtime
  • Provides a pipe for easy translation in templates

Cons

  • Limited built-in features compared to some other i18n solutions
  • Requires manual management of translation files
  • May have performance issues with large translation sets
  • Lacks advanced pluralization support out of the box

Code Examples

  1. Basic translation usage in a component:
import { TranslateService } from '@ngx-translate/core';

export class MyComponent {
  constructor(private translate: TranslateService) {
    translate.setDefaultLang('en');
    translate.use('en');
  }

  switchLanguage(lang: string) {
    this.translate.use(lang);
  }
}
  1. Using the translate pipe in a template:
<h1>{{ 'HELLO_WORLD' | translate }}</h1>
<p>{{ 'WELCOME_MESSAGE' | translate:{ name: username } }}</p>
  1. Programmatically getting translations:
this.translate.get('SOME_KEY').subscribe((res: string) => {
  console.log(res);
});

this.translate.get(['KEY1', 'KEY2']).subscribe((res: any) => {
  console.log(res.KEY1, res.KEY2);
});

Getting Started

  1. Install the package:

    npm install @ngx-translate/core @ngx-translate/http-loader
    
  2. Import the TranslateModule in your app.module.ts:

    import { TranslateModule, TranslateLoader } from '@ngx-translate/core';
    import { TranslateHttpLoader } from '@ngx-translate/http-loader';
    import { HttpClient, HttpClientModule } from '@angular/common/http';
    
    export function HttpLoaderFactory(http: HttpClient) {
      return new TranslateHttpLoader(http);
    }
    
    @NgModule({
      imports: [
        HttpClientModule,
        TranslateModule.forRoot({
          loader: {
            provide: TranslateLoader,
            useFactory: HttpLoaderFactory,
            deps: [HttpClient]
          }
        })
      ],
      // ...
    })
    export class AppModule { }
    
  3. Create translation files (e.g., en.json, fr.json) in your assets folder.

  4. Use the TranslateService in your components and the translate pipe in your templates.

Competitor Comparisons

8,358

i18next: learn once - translate everywhere

Pros of i18next

  • Framework-agnostic, can be used with various JavaScript frameworks
  • Extensive plugin ecosystem for additional functionality
  • Supports pluralization and context-based translations

Cons of i18next

  • Steeper learning curve due to more complex API
  • Requires additional setup for framework-specific integrations

Code Comparison

i18next:

import i18next from 'i18next';

i18next.init({
  lng: 'en',
  resources: {
    en: {
      translation: {
        key: 'Hello, {{name}}!'
      }
    }
  }
});

i18next.t('key', { name: 'John' });

ngx-translate/core:

import { TranslateService } from '@ngx-translate/core';

constructor(private translate: TranslateService) {
  translate.setDefaultLang('en');
  translate.use('en');
}

this.translate.get('key', { name: 'John' }).subscribe((res: string) => {
  console.log(res);
});

Both libraries offer robust internationalization solutions, but i18next provides more flexibility across different frameworks, while ngx-translate/core is specifically tailored for Angular applications. i18next's extensive plugin system allows for more customization, but it may require additional setup. ngx-translate/core integrates more seamlessly with Angular's ecosystem, offering a simpler API for basic use cases.

:globe_with_meridians: Internationalization plugin for Vue.js

Pros of vue-i18n

  • Designed specifically for Vue.js, offering seamless integration and better performance
  • Supports pluralization and date/time localization out of the box
  • Provides a more flexible interpolation syntax, allowing for complex translations

Cons of vue-i18n

  • Limited to Vue.js applications, whereas ngx-translate/core is framework-agnostic
  • Slightly steeper learning curve for developers new to Vue.js ecosystem
  • Less extensive third-party plugin ecosystem compared to ngx-translate/core

Code Comparison

vue-i18n:

const i18n = new VueI18n({
  locale: 'en',
  messages: {
    en: { hello: 'Hello {name}!' },
    fr: { hello: 'Bonjour {name}!' }
  }
})

ngx-translate/core:

this.translate.setTranslation('en', {
  hello: 'Hello {{name}}!'
});
this.translate.setTranslation('fr', {
  hello: 'Bonjour {{name}}!'
});

Both libraries offer similar functionality for basic translations, but vue-i18n's syntax is more concise and Vue-specific. ngx-translate/core uses a service-based approach, which is more aligned with Angular's dependency injection system.

14,610

The monorepo home to all of the FormatJS related libraries, most notably react-intl.

Pros of formatjs

  • Broader ecosystem support, including React, Vue, and vanilla JavaScript
  • More comprehensive internationalization features, including pluralization and gender-aware formatting
  • Active development with frequent updates and improvements

Cons of formatjs

  • Steeper learning curve due to more complex API and features
  • Larger bundle size, which may impact performance in smaller applications
  • Requires more setup and configuration compared to ngx-translate/core

Code Comparison

ngx-translate/core:

import { TranslateService } from '@ngx-translate/core';

constructor(private translate: TranslateService) {
  translate.setDefaultLang('en');
}

this.translate.get('HELLO').subscribe((res: string) => {
  console.log(res);
});

formatjs:

import { IntlProvider, FormattedMessage } from 'react-intl';

<IntlProvider messages={messages} locale={locale}>
  <FormattedMessage
    id="greeting"
    defaultMessage="Hello, {name}!"
    values={{ name: 'World' }}
  />
</IntlProvider>

Both libraries offer internationalization solutions, but formatjs provides a more comprehensive set of features at the cost of increased complexity. ngx-translate/core is simpler and more lightweight, making it a good choice for Angular-specific projects with basic translation needs. formatjs is better suited for larger applications or those requiring advanced internationalization features across multiple frameworks.

Give your JavaScript the ability to speak many languages.

Pros of Polyglot.js

  • Lightweight and simple to use, with no external dependencies
  • Supports pluralization and interpolation out of the box
  • Can be used in both browser and Node.js environments

Cons of Polyglot.js

  • Limited features compared to more comprehensive solutions
  • Lacks built-in support for lazy loading or dynamic translation updates
  • No specific Angular integration, requiring additional setup for Angular projects

Code Comparison

Polyglot.js:

import Polyglot from 'node-polyglot';

const polyglot = new Polyglot();
polyglot.extend({"hello": "Hello, %{name}!"});
console.log(polyglot.t("hello", {name: "world"}));

ngx-translate/core:

import { TranslateService } from '@ngx-translate/core';

constructor(private translate: TranslateService) {
  translate.setTranslation('en', {"hello": "Hello, {{name}}!"});
  console.log(translate.instant("hello", {name: "world"}));
}

Both libraries provide similar functionality for basic translation needs. Polyglot.js offers a more straightforward setup for non-Angular projects, while ngx-translate/core is specifically designed for Angular applications, providing seamless integration with Angular's ecosystem and additional features like lazy loading and dynamic language switching.

🌍 📖 A readable, automated, and optimized (2 kb) internationalization for JavaScript

Pros of js-lingui

  • Supports multiple frameworks (React, Vue, Angular) while ngx-translate is Angular-specific
  • Offers advanced pluralization and formatting features
  • Provides a command-line interface for managing translations

Cons of js-lingui

  • Steeper learning curve compared to ngx-translate's simpler API
  • Requires additional setup and configuration
  • May have a larger bundle size due to its comprehensive feature set

Code Comparison

ngx-translate/core:

import { TranslateService } from '@ngx-translate/core';

constructor(private translate: TranslateService) {
  translate.setDefaultLang('en');
}

this.translate.get('HELLO').subscribe((res: string) => {
  console.log(res);
});

js-lingui:

import { Trans } from '@lingui/macro';

function MyComponent() {
  return <Trans>Hello, World!</Trans>;
}

// In a separate messages.po file:
msgid "Hello, World!"
msgstr "Bonjour, le monde !"

Both libraries offer straightforward ways to handle translations, but js-lingui's approach is more declarative and integrates well with build processes. ngx-translate uses a service-based approach, which may be more familiar to Angular developers.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

@ngx-translate/core Buildstatus npm version

The internationalization (i18n) library for Angular.

Angular 16 - 21

The new documentation now covers installation on Angular 16+ and is divided into smaller, more readable sections, making it easier to digest than this big README. It also documents the additional interfaces and explains how to develop custom plugins.

In addition to that, a getting started tutorial is available here: How to Translate Your Angular App with NGX-Translate

Support for older versions

We only support the current version with new updates and fixes.

When using an EOL (End Of Lifetime) version, you have a few options:

  • Upgrade to the current version of Ngx-translate. We have worked hard to make upgrading simple. You can view our migration guide to assist you in your upgrade.

  • Remain Out of Date: Continue using the outdated version, but be aware that you will be subject to security risks and bugs.

  • Commercial Support for EOL versions Our partner HeroDevs offers commercial security support for EOL versions of Ngx-translate. If you cannot migrate but must remain secure you can contact them here.

Recommended tools

  • BabelEdit — translation editor for ngx-translate files

NPM DownloadsLast 30 Days