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

@wellwind/ngx-easy-translate

v1.0.3

Published

An easy to use, strong typed i18n Angular library.

Downloads

4

Readme

NgxEasyTranslate

An easy to use, strong typed i18n Angular library.

Feature

  • Internationalization.
  • STRONG TYPED supported.
  • Lazy load languages supported.
  • Lazy load languages by seperated feature modules suppported.

Getting Started

Basic Usage

  1. Install package
npm install @wellwind/ngx-easy-translate
  1. Prepare your language loader, and register to NgxEasyTranslateModule.
import { NgxEasyTranslateModule } from '@wellwind/ngx-easy-translate';
import { of } from 'rxjs';

@NgModule({
  ...,
  imports: [
    ...,
    NgxEasyTranslateModule.forRoot({
      defaultLang: 'en',
      loader: (lang: string) => {
        if(lang === 'en') {
          return of({
            title: 'Home',
            profile: {
              name: 'Your Name'
            },
            sayHi: (name) => `Greeting, ${name}.`
          });
        } else if(lang === 'zh'){
          return of({
            title: '首頁',
            profile: {
              name: '你的名字',
            },
            sayHi: (name) => `哈囉,${name}。`
          });
        }
      }
    })
  ]
})
  1. Inject ROOT_TRANSLATE to get language, it's an Observable.

  2. If need change language, inject NgxEasyTranslateService and call changeCurrentLanguage().

import { Component, OnInit, Inject } from '@angular/core';
import { NgxEasyTranslateService, ROOT_TRANSLATE } from '@wellwind/ngx-easy-translate';
import { Observable } from 'rxjs';

@Component({
  selector: 'app-root',
  template: `
    <h1>{{ (translate | async)?.title }}</h1>
    <button (click)="changeLang('en')">EN</button>
    <button (click)="changeLang('zh')">ZH</button>
  `,
})
export class AppComponent {
  get translate() { return this.rootTranslate; }

  constructor(
    private translateService: NgxEasyTranslateService,
    @Inject(ROOT_TRANSLATE) private rootTranslate: Observable<any>) { }

  changeLang(lang: string) {
    this.translateService.changeCurrentLanguage(lang);
  }
}
  1. There are also a translate pipe, then we don't have to inject anything.
{{ 'profile.name' | translate }}
  1. Because languages are writing in TypeScript, not just a json, so we can programing anything.
const greeting = this.rootTranslate.sayHi('Mike');

Support Strong Type

Because all languages are in TypeScript file, is't easy to make it strong typed.

Here are simple steps to make translation strong typed.

  1. Writing first translation in TypeScript.
  • in i18n/en.ts
export const lang = {
  title: 'Home'
}
  1. Define type.
  • in i18n/type.ts
import { en } from './en';
export type Translate = typeof en;
  1. In another translation file, use the type defined above.
  • in i18n/zh.ts
import { Translate } from './type';
export const lang: Translate = {
  title: '首頁'
}
  1. Refactor the module.
import { en } from './i18n/en';
import { zh } from './i18n/zh';
import { of } from 'rxjs';

...

NgxEasyTranslateModule.forRoot(
  defaultLang: 'en',
  loader: (lang: string) => {
    if(lang === 'en') {
      return of(en);
    } else if(lang === 'zh'){
      return of(zh);
    }
})
  1. Now we can use the type we defined
import { Translate } from './type';

export class AppComponent {
 constructor(
    @Inject(ROOT_TRANSLATE) private rootTranslate: Observable<Translate>) { }
}

Support Lazy Loading

The key point is the loader function, we can write any logic to load translation content, and thanks for dynamic import, it's very easy to seperate language file by using import().

NgxEasyTranslateModule.forRoot(
  defaultLang: 'en',
  loader: (lang: string) => {
    if(lang === 'en') {
      return from(import('./i18n/en').then(result => result.lang));
    } else if(lang === 'zh'){
      return from(import('./i18n/zh').then(result => result.lang));
    }
});

Or just like this:

loader: (lang: string) => from(import(`./i18n/${lang}`)).pipe(map(result => result.lang))

The important thing is now en.ts, zh.ts was never used explicitly, we will got errors when compile the project.

We have to include these files in tsconfig.app.json.

"include": [
  ...
  "src/app/**/i18n/**/*.ts"
]

Support Lazy Loading By Feature Module

We may not want to write all translation content in one file (imagine 1000+ pages), sometimes we need lazily load languages by every feature module.

To acheieve that, put your loader to NgxEasyTranslateModule.forFeature()

NgxEasyTranslateModule.forFeature((lang) => from(import(`./i18n/${lang}`).then(result => result.lang)))

Then we can write own own translate file for seperated feature module.

In component, we can inject FEATURE_TRANSLATE to get the current language translaction for the feature.

import { ROOT_TRANSLATE, FEATURE_TRANSLATE } from '@wellwind/ngx-easy-translate';

export class FeatureComponent {
  
  constructor(
    @Inject(ROOT_TRANSLATE) private rootTranslate: Observable<RootTranslate>,
    @Inject(FEATURE_TRANSLATE) private featureTranslate: Observable<FeatureTranslate>) { }
}

Release Notes

v1.0.0

  • Just publish package.

Help Wanted

Because English is not my major language. This document should have many incorrect things. If you think this package is useful, please proofread and send a PR to me. Thank you :)

LICENSE

MIT