1.0.8 • Published 5 years ago

@huzapi/dictionary-handler v1.0.8

Weekly downloads
-
License
ISC
Repository
gitlab
Last release
5 years ago

Huz Api / Dictionary Handler

Parses and Builds dictionary data

  • TDD (Test driven)
  • Fully JsDoc
  • index.d.ts included (built-in @types support)

Test

npm test

Install to your project

npm i @huzapi/dictionary-handler

Import

const {DictionaryInstance, DictionaryConfig} = require('@huzapi/dictionary-handler');

Intercepts

Current User's Language Lambda

It's a lambda(callback) function in order to learn current user's language

  • Callback must NOT be Promise
  • Callback must return string
  • If returns empty, it will be evaluated as default language
DictionaryConfig.setCurrentLanguageFn(req => {
    //You can decorate this part, req: Express.Request
    return req._userLanguage; // it should be same format with language-code at dictionary which you set
    //or you can return static, example ==> return 'en';
} );

Current User's Tenant Lambda

It's a lambda(callback) function in order to learn current user's tenant (project, vendor etc)

  • Callback must NOT be Promise
  • Callback must return string
  • If returns empty, it will be evaluated as default tenant-key
DictionaryConfig.setCurrentTenantFn(req => {
    //You can decorate this part, req: Express.Request
    return req._tenantId; // it should be same format with tenant-key at dictionary which you set
    //or you can return static, example ==> return 'master';
} );

Set Default Language

You can define default language for your system

  • If you dont use this option, default language will be 'en'
DictionaryConfig.setDefaultLanguage('de');

Load Data

You must create a new instance for each module, part if you want to use isolated dictionary

const dictionary = new DictionaryInstance();
//for all languages without multi-tenancy
dictionary.appendRoot({
    en: {
        id_box: 'Game Id',
        name_box: 'Name of Game',
        name_placeholder: 'Type game name ...',
    },
    fr: {
    }
});
//for all languages with multi-tenancy
dictionary.appendRoot({
    en: {
        id_box: 'Game Id', //no multi-tenancy, text are shared by all tenants
        name_box: 'Name of Game', //no multi-tenancy, text are shared by all tenants
        name_placeholder: {
            $default: 'Type game name ...',
            'vendor-1': 'Please fill game...'
        },
    },
    fr: {
        //same keys with another language
    }
});
//for only one language
dictionary.appendForLanguage('en', {
    id_box: 'Game Id',
    name_box: 'Name of Game',
    name_placeholder: 'Type game name ...',
});
//with multi-support
dictionary.appendForLanguage('en', {
    welcome: {
        $default: 'Welcome to out page',
        'vendor-1': 'Welcome to vendor 1 awesome page',
        'vendor-2': 'Welcome to vendor 2 dark page'
    },
    bye: {
       $default: 'Bye bye',
       'vendor-1': 'Bye bye Vendor 1, vendor1.com',
       'vendor-2': 'Goodbye our visitor, vendor2.com'
    }
});

Get text value for a key

Get text value by key

Language and tenant-key is produced from your callback if you defined it

//get text of 'welcome' key
dictionary.get(req, 'welcome');
//or with ignoring language and tenant
dictionary.get('welcome');

Get text value by key, language & tenant

//get text of 'welcome' key for English without multi-tenancy
dictionary.getDirect('welcome', 'en');
//get text of 'welcome' key for German with vendor2
dictionary.getDirect('welcome', 'de', 'vendor-2');

Clear Data

//clear all languages
dictionary.clearRoot();
//clear only one language
dictionary.clearForLanguage('en');

Get Stored Data

//get all languages
console.log(dictionary.mapRoot());

Returns Map<string, Map<string, Map<string, string>>>

or language > key > tenant: text

//get only one languages
console.log(dictionary.mapForLanguage('en'));

Returns Map<string, Map<string, string>>

or key > tenant: text

Get Flat Data

It's same with previous, but all keys will be flatted with dot

//get all languages
console.log(dictionary.flatRoot());

Returns Map<string, string>

or language.key.tenant: text

//get only one languages
console.log(dictionary.flatForLanguage('en'));

Returns Map<string, string>

or key.tenant: text

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.1

5 years ago