1.9.0 • Published 5 years ago

grunt-nks-i18n v1.9.0

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

I18n

Download

  $ bower install nks-i18n;
  $ git clone https://github.com/konstantin-nizhinskiy/I18n.git;

Build json translation

Getting Started

If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:

    npm install grunt-nks-i18n --save-dev

Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:

   grunt.loadNpmTasks('grunt-nks-i18n');

i18n task

Run this task with the grunt i18n command.

Task targets, files and options may be specified according to the grunt Configuring tasks guide.

Options

locales

Type: Array
Default: ['en']

Specify the location you want to create

cache

Type: Boolean
Default: true

Cache ald bundle json file before build new. Save in one cache file all keys, value

cacheFile

Type: Boolean
Default: true

On/off cache bundle file translation

cacheDir

Type: String
Default: cache/i18n/

Cache directory

buildDoc

Type: Boolean
Default: true

bundleFile

Type: Boolean
Default: true

Saving a file translate where he was taken and placed in the cache

typeTranslation

Type: String Default: json

Type build translation file (json,yml,js)

loadJsTranslation:

Type: String Default:

    if (typeof define === 'function' && define.amd) {
        define(['i18n'], function (i18n) {
            return i18n.setTranslation(factory())
        })
    } else {
        i18n.setTranslation(factory());
    }

Load function translation if you select type typeTranslation=='js'

file:

  • AllKeys.md - All keys translation
  • AllKeysFileBuild.md - All keys file
  • AllKeysSize.md - All keys size translation
  • InfoBuild.md - Info last build translation

reg

Type: String
Default: (i18n\\.get\\([ ]{0,}[\'"])([A-Za-z.]+)([\'"])

This Regex finds keys in your file project

grunt config

grunt.initConfig({
    i18n: {
        options: {
            locales:['ua','en']
        },
        bundle1: {
            files: {
                'test/translations/bundle1.min.js':[
                    'test/**'
                ]
            }
        },
        bundle2: {
            files: {
                'test/translations/bundle2.min.js':[
                    'test2/**'
                ]
            }
        }
    }
});
//...

build file:

  • bundle1.min.en.json
  • bundle1.min.ua.json
  • bundle2.min.en.json
  • bundle2.min.ua.json

Loader supports

* AMD
* CommonJS

Config

    i18n.setProperty({
        locale: 'EN', // location on
        localeDefault: 'EN', //locale on default if key of main locale is empty
        versionJson: '1.1.1', // versionJson add to url params load file translation
        defaultValue: '' // You can set default value for translated
    });

Methods

MethodArgumentsinfo
changeLocalelocale,callbackChange locale
getkey, params, optionGet translations
getByDataparams, optionGet translations by data param set in function
getLocaleGet locale now
loadurl, callbackLoad translations JSON
setPropertyparamsSet property i18n
onevent, callbackBind event callback
offevent, callbackUnbind event callback
onceevent, callbackBind once event callback
triggerevent, param1,...Trigger event

Method changeLocale

Change locale

Arguments

ArgumentsTypeinfo
localestringlocale ua,en...
callbackfunctionCallback on success change locale

Events

EventsArgumentsinfo
changeLocalelocaleEvent on success change locale

Example

    i18n.on("changeLocale",function(locale) {
      console.log("Event changeLocale",locale)
    });
    i18n.changeLocale('en');
    i18n.changeLocale('en',function(){console.log("run callback in function")});
    

Method get

Get translations

Arguments

ArgumentsTypeinfo
keystringKey translation
paramsfunctionParams merge with translation messengers
optionobjectOptions translation
option.defaultValuestringdefault value translation

Events

EventsArgumentsinfo
error:translation:keykey,localeNot fount key translation
errortype_error,key,localeNot fount key translation

Example

/**
* File translations
* {
    "test.test.test": "Test test test",
    "count.books": "{{ count}} books",
    "book.name.pages.number": "Book {{ name }} page {{ number }}"
  }
*/
i18n.setProperty({
   locale: 'en',
   localeDefault: 'en'
});
console.log(i18n.get("test.test.test")); // Test test test
console.log(i18n.get("count.books", {count: 20}));  // 20 books
console.log(i18n.get("book.name.pages.number", {name: 'JS', number: 201})); // Book JS page 201
console.log(i18n.get("test.test.test1")); // test.test.test1
console.log(i18n.get("test.test.test1",{},{defaultValue:'my value'})); // my value

Method getByData

Get translations by data param set in function

Arguments

ArgumentsTypeinfo
params{string,object}Params translation
optionobjectOptions translation
option.defaultValuestringdefault value translation

Events

-

Example

i18n.setProperty({
  locale: 'en',
  localeDefault: 'en'
});

console.log(i18n.getByData({"en":"test en","ua":"test ua"})); // test en
console.log(i18n.getByData({"EN":"test en","UA":"test ua"})); // test en
console.log(i18n.getByData("My data")); // My data
console.log(i18n.getByData({"UA":"test ua"},{defaultValue:'my value'})); // my value

Method getLocale

Get locale now

Arguments

-

Events

-

Example

i18n.setProperty({
  locale: 'en',
  localeDefault: 'en'
});

console.log(i18n.getLocale()); // en

Method load

Load translations JSON

Arguments

ArgumentsTypeinfo
urlsstring[],stringUrl json file translations
callbackfunctionCallback function success load
optionobjectOptions translation
option.modulePrefixstringYou can set prefix url

Events

EventsArgumentsinfo
loadurl,data_json_fileload file json success
error:loadxhr.statusText, xhr.status, xhrerror load json
errortype_error,xhr.statusText, xhr.status, xhrerror load json

Example

    i18n.load(
          ['translations/bundle1.min','translations/bundle1.min'], 
          function () {
              console.log("success load")
          },
          {
              modulePrefix:"my_prefix"
          });

    i18n.load(
          'translations/bundle1.min', 
          function () {
              console.log("success load")
          },
          {
              modulePrefix:"my_prefix"
          });
    
    i18n.load('translations/bundle1.min');
    
    i18n.load(['translations/bundle1.min','translations/bundle2.min']);

Method setProperty

Set property i18n

Arguments

ArgumentsTypeinfo
optionobjectOptions translation
option.localestringLocation default:ua
option.localeDefaultstringLocale on default if key of main locale is empty default:ua
option.versionJsonstringVersion json add to url params load file translation default:unix
option.defaultValuestringDefault value translation
option.modulePrefixobjectPrefix url to module

Events

Example

    i18n.setProperty({
        locale: 'en',
        localeDefault: 'en'
    });

Events

EventArgumentsinfo
changeLocalelocaleEvent change locale
loadurl, JSONEvent load file json
error:translation:keykey, localeEvent not fount key translation
error:loadstatusText, status, xhrEvent error load json translation
errortype, *All error event error:*

Example

<!-- ... -->
    <script src="dist/I18n.js" type="application/javascript"></script>
    <script>
        i18n.setProperty({
            locale: 'en',
            localeDefault: 'en'
        });
        var demoFunction=function(){
            console.log(i18n.get("test.test.test")); // Test test test
            console.log(i18n.get("count.books", {count: 20}));  // 20 books
            console.log(i18n.get("book.name.pages.number", {name: 'JS', number: 201})); // Book JS page 201
            console.log(i18n.get("test.test.test1")); // test.test.test1
            console.log(i18n.get("test.test.test1",{},{defaultValue:'my value'})); // my value
            console.log(i18n.getByData({"en":"test.en","ua":"test.ua"})); // test.en
        };
        i18n.load('translations/bundle1.min', demoFunction );
        i18n.on('changeLocale',function(){
            console.log('change locale');
            demoFunction();
        });
    </script>
  
            
    <button onclick="i18n.changeLocale('ua')">UA</button>
    <button onclick="i18n.changeLocale('en')">EN</button>


<!-- ... -->
1.9.0

5 years ago

1.8.2

5 years ago

1.8.1

5 years ago

1.8.0

5 years ago

1.7.0

6 years ago

1.6.0

6 years ago

1.4.0

6 years ago

1.2.1

7 years ago

1.2.0

7 years ago

1.1.0

7 years ago

1.0.0

8 years ago

0.9.8

8 years ago

0.9.7

8 years ago

0.9.6

8 years ago

0.9.5

8 years ago

0.9.4

8 years ago

0.9.3

8 years ago

0.9.2

8 years ago

0.9.1

8 years ago

0.9.0

8 years ago