1.1.2 • Published 7 years ago

angular2-i18next v1.1.2

Weekly downloads
5
License
MIT
Repository
github
Last release
7 years ago

angular2-i18next

An implementation of i18next wrapper for Angular2 4.0.1 compatible.

Installation

First you need to install the npm modules:

npm install ts-smart-logger --save

npm install angular2-i18next --save

Demo

1 git clone --progress -v "git@github.com:apoterenko/angular2-i18next.git" "D:\sources"
2 cd D:\sources\angular2-i18next\demo
3 npm install
4 npm start

Use

main.ts

import {TranslateI18NextModule} from 'angular2-i18next/index';

@NgModule({
    bootstrap: [ApplicationComponent],
    imports: [
        TranslateI18NextModule,
        ...
    ],
    ...
})
export class ApplicationModule {
}

app.ts

Call translateI18Next.init({ ... }) with the options you want to initialize i18next with.

You can also pass the special options use and defaultUse which are each Arrays that may contain "plugins" such as a cache or backend you wish to use. See .use method of i18next.

The default plugins for defaultUse are i18nextXHRBackend, browserLanguageDetectorCtor

import {TranslateI18Next} from 'angular2-i18next/index';

@Component({
  selector: 'app'
  ...
});
export class App {

    private appleCount: number;
    ...
    
    viewReady:boolean = false;
    
    constructor(private translateI18Next:TranslateI18Next) {
        translateI18Next.init({
            debug: true,                                                        // optional
            returnEmptyString: false,                                           // optional	- but.. it's important, please see http://i18next.com/docs/options/!
            mapping: {"specific_backend_message": "message_for_translate"},     // optional
            browserLanguageDetector: injectableCustomLanguageDetectorService,   // optional - the specific application language detector (allows you to return the language of the user.
                                                                                //            If it is absent, the service uses default "angular2 locale detector" behaviour using LOCALE_ID.  
            // supportedLanguages: ['en', 'pt'],                                //            Therefore you can pass the optional supportedLanguages parameter which indicates your supported languages.    
                                                                                //            For example, LOCALE_ID = 'en-AU' or 'en-US' or 'en', you can pass only ['en'] -> locales/en/translation.json
                                                                                //                         LOCALE_ID = 'pt-BR' or 'pt', you can pass only ['pt'] -> locales/pt/translation.json
            backend: injectableBackendConfigFactory                             // optional - allows to change "loadPath" i18next parameter
        }).then(() => {
            this.viewReady = true;
        });
    }
}

app.html

<div *ngIf="viewReady" [innerHTML]='"<span style=\"color:red;\">You have {count} apple</span>" | translate:{count: appleCount}'></div>

src/locales/en/translation.json

{
  "<span style=\"color:red;\">You have {count} apple</span>": "<span style=\"color:red;\">You have {count} apple</span>",
  "<span style=\"color:red;\">You have {count} apple</span>_plural": "<span style=\"color:red;\">You have {count} apples</span>"
}

src/locales/ru/translation.json

{
  "<span style=\"color:red;\">You have {count} apple</span>": "<span style=\"color:green;\">У вас есть одно яблоко</span>",
  "<span style=\"color:red;\">You have {count} apple</span>_2": "<span style=\"color:blue;\">У вас есть {count} яблока</span>",
  "<span style=\"color:red;\">You have {count} apple</span>_5": "<span style=\"color:yellow;\">У вас есть {count} яблок</span>"
}

I18nModule.ts (the localized constants at the typescript files)

class Products {

    static NAME:string = "Name";
    ...
}

export class I18nModule {
    static PRODUCTS = Products;
}

Use i18next-parser (gulp task)

var gulp = require('gulp'),
	i18next = require('i18next-parser');

gulp.task('i18next', function () {
	gulp.src(['src/**/*.html', 'src/*.html', 'src/locales/I18nModule.ts'])
		.pipe(i18next({
			locales: ['en'],
			output: '../src/locales',

			// The workaround (i18next-parser does not support "namespaceSeparator === false" flag):
			//      options.namespaceSeparator || ':';
			//      options.keySeparator || '.';
			keySeparator: "eb6c8400-b5f8-4177-a417-5e5e902a6c83",
			namespaceSeparator: "eb6c8400-b5f8-4177-a417-5e5e902a6c83",

			// Supported patterns:
			//      1) The gettext format like << 'Hello {{count}} world!' | translate:{count: 100} >>
			//      2) The I18nModule format like << static YOUR_CONSTANT:string = "Your value..."; >>
			//      3) The inner html format << <div [innerHTML]='"<span style=\"color:red;\">You have {count} apple</span>" | translate:{count: appleCount}'></div> >>
			parser: '\'(.+)\'.+translate|static [A-Z_0-9 ]+\: {0,}string \= ["|\'](.+)["|\']|\\[innerHTML\\]\=\'\"(.+)\" +\\|'
		}))
		.pipe(gulp.dest('src/locales'));
});

Publish

npm run deploy

License

Licensed under MIT.

1.1.2

7 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago

0.0.59

8 years ago

0.0.58

8 years ago

0.0.57

8 years ago

0.0.54

8 years ago

0.0.53

8 years ago

0.0.52

8 years ago

0.0.51

8 years ago

0.0.49

8 years ago

0.0.48

8 years ago

0.0.47

8 years ago

0.0.46

8 years ago

0.0.45

8 years ago

0.0.44

8 years ago

0.0.41

8 years ago

0.0.40

8 years ago

0.0.39

8 years ago

0.0.38

8 years ago

0.0.37

8 years ago

0.0.36

8 years ago

0.0.35

8 years ago

0.0.34

8 years ago

0.0.33

8 years ago

0.0.32

8 years ago

0.0.31

8 years ago

0.0.30

8 years ago

0.0.29

8 years ago

0.0.28

8 years ago

0.0.27

8 years ago

0.0.26

8 years ago

0.0.25

8 years ago

0.0.24

8 years ago

0.0.23

8 years ago

0.0.22

8 years ago

0.0.20

8 years ago

0.0.19

8 years ago

0.0.18

8 years ago

0.0.17

8 years ago

0.0.16

8 years ago

0.0.15

8 years ago

0.0.13

8 years ago

0.0.12

8 years ago

0.0.11

8 years ago

0.0.10

8 years ago

0.0.9

8 years ago

0.0.8

8 years ago

0.0.7

8 years ago

0.0.6

8 years ago

0.0.5

8 years ago

0.0.4

8 years ago

0.0.3

8 years ago

0.0.2

8 years ago

0.0.1

8 years ago