npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ng2-i18next-fork

v0.2.0

Published

use i18next with Angular2

Downloads

11

Readme

This is merely a fork of ng2-i18next from actimeo. I take no credit for this. I merely removed a few lines of code that caused issues for me. If the original dev finally fixes this issue ill remove the package and use his/hers

npm version Downloads

ng2-i18next

Use i18next, the popular internationalization Javascript library (http://i18next.com/), with Angular2.

A demo is available on github: ng2-i18next-demo-rc3

A. Install

This procedure is based on a fresh angular-cli install and uses the i18next XHR backend to read locales from XHR and the browser language detector module to detect language from the browser language preferences.

1. install npm package

npm install ng2-i18next --save

This will also install three i18next packages (i18next, i18next-browser-languagedetector and i18next-xhr-backend), necessary for this module to work.

2. Edit your angular-cli-build.js file

 var app = new Angular2App(defaults, {
     vendorNpmFiles: [
       ...,
       'i18next/**/*.+(js|js.map)',
       'i18next-xhr-backend/**/*.+(js|js.map)',
       'i18next-browser-languagedetector/**/*.+(js|js.map)',
       'ng2-i18next/**/*.+(js|js.map)'
     ]});

###3. Edit your system-config.ts file

const map: any = {
  'ng2-i18next': 'vendor/ng2-i18next',
  'i18next': 'vendor/i18next/i18next.min.js',
  'i18nextXHRBackend': 'vendor/i18next-xhr-backend/i18nextXHRBackend.min.js',
  'i18nextBrowserLanguageDetector': 'vendor/i18next-browser-languagedetector/i18nextBrowserLanguageDetector.min.js'
};

/** User packages configuration. */
const packages: any = {
  'vendor/ng2-i18next': {
    format: 'cjs',
    defaultExtension: 'js'
  },
  'i18next': { format: 'global' },
  'i18nextXHRBackend': { format: 'global' },
  'i18nextBrowserLanguageDetector': { format: 'global' }
};

###4. Import the i18next modules from your main component

import 'i18next';
import 'i18nextXHRBackend';
import 'i18nextBrowserLanguageDetector';

##B. Usage

###1. Inject I18nService in your main component In this example, the I18nService will initialize the i18next object and load the locales via XHR backend and use browser language detector module.

import {I18nService, I18nServiceConfig} from 'ng2-i18next/ng2-i18next';

declare var i18nextBrowserLanguageDetector: any;
declare var i18nextXHRBackend: any;

const NG2_I18NEXT_PROVIDERS = [
  provide(I18nServiceConfig, {
    useValue: {
      use: [i18nextBrowserLanguageDetector, i18nextXHRBackend],
      config: {
        detection: { order: ['navigator'] },
        fallbackLng: 'en'
      }
    }
  }),
  I18nService
];

@Component({
  ...
  providers: [NG2_I18NEXT_PROVIDERS]
})

###2. Add your locales Your locales have to be placed, for each language <lang>, in src/locales/<lang>/translation.json .

For example: src/locales/en/translation.json :

{
  "hello-world": "Hello World! ng2-i18next works!",
  "what-is-your-name": "What is your name?"
}

src/locales/fr/translation.json :

{
  "hello-world": "Salut les terriens ! ng2-i18next fonctionne !",
  "what-is-your-name": "Comment tu t'appelles ?"
}

###3. Use the I18nDirective Two directives are available:

  • i18n to replace the contents of a tag with the localization of the given key,
  • i18n-placeholder to replace the contents of the placeholder attribute with the localization of the given key.

src/app/app.component.ts :

[...]
import {I18nDirective} from 'ng2-i18next/ng2-i18next';
[...]
@Component({
[...]
  templateUrl: 'app.component.html',
  directives: [I18nDirective]
})

src/app/app.component.html :

<p i18n="hello-world">i18n does not work!</p>
<input type="text" i18n-placeholder="what-is-your-name">

###4. Use the I18nService t() method ####In the code:

[...]
import {I18nService} from 'ng2-i18next/ng2-i18next';
[...]
constructor(private i18n: I18nService) { }
[...]
myFunction() {
  var text = this.i18n.t('hello-world');
  [...]
}

####In the template: {{i18n.t('hello-world')}}

###5. Pass translation options to i18next

It is possible to pass options to the i18next module with this notation:

<p i18n='{"not_found_key": {"defaultValue": "a default value via option"}}'></p>

<p i18n='{"counter": {"count": {{theCounter}}}}'></p>

###6. Race at init time

The i18next module takes some time to load its locales files via XHR and initialize.

For this reason, it is possible that an early call to I18nService.t() does not return a correct value because the i18next module is not yet initialized.

You can subscribe to an observable defined by the I18nService which will send a message when initialization is done.

// case when i18next is already initialized
this.text = this.i18n.t('hello-world');
// case when i18next is not yet initialized
var obs = this.i18n.whenReady$.subscribe(b => {
  this.text = this.i18n.t('hello-world');
  obs.unsubscribe();
});

Or, using a promise:

this.loadAndRender('hello-world', s => this.text = s);

loadAndRender(code: string, doRenderCallback) {
  if (!code) {
    return;
  }

  this.i18n.tPromise(code)

    .then((val: string) => {
      doRenderCallback(val);
    })

    .catch((val: string) => {
      doRenderCallback(' ');
      var obs = this.i18n.whenReady$.subscribe(b => {
        doRenderCallback(this.i18n.t(code));
        obs.unsubscribe();
      });
    });
}