4.5.0 • Published 5 days ago

@markuplint/i18n v4.5.0

Weekly downloads
34
License
MIT
Repository
github
Last release
5 days ago

@markuplint/i18n

npm version

Install

markuplint package includes this package.

$ npm install @markuplint/i18n

$ yarn add @markuplint/i18n

API

import { translator } from '@markuplint/i18n';

const t = translator({
  locale: 'ja',
  ...require('@markuplint/i18n/locales/ja.json'),
});

The translator function creates the t function. It is an overloading function that accepts kind of arguments below:

Translate sentence

type T = (template?: string, ...values: string[]) => string;
const message = t(
  // Template #1
  '{0} is {1:c}',
  // The {0} value of template #1
  t(
    // Template #2
    '{0} of {1}',
    // The {0} value of template #2
    t(
      // Template #3
      'the {0}',
      // The {0} value of template #3
      'value',
    ),
    // The {1} value of template #2
    t(
      // Template #4
      'the "{0*}" {1}',
      // The {0} value of template #4
      'id',
      // The {1} value of template #4
      'attribute',
    ),
  ),
  // The {1} value of template #1
  'duplicated',
);

console.log(message);
// => 属性「id」の値が重複しています

Placeholder

There is a placeholder that the number is surrounded by {} on template strings. It is replaced argument as a phrase. It translates the phrase if it matches the keyword defined in the dictionary.

Tagged templates syntax

:warning: It is experimental.

import { taggedTemplateTranslator } from '@markuplint/i18n';

const _ = taggedTemplateTranslator({
  locale: 'ja',
  ...require('path/to/dictionary/ja.json'),
});

const message = _`${
  //
  _`${
    //
    _`the ${'value'}`
  } of ${
    //
    _`the "${'id'}" ${'attribute'}`
  }`
} is ${
  //
  'c:duplicated'
}`;

console.log(message);
// => 属性「id」の値が重複しています

Translate a phrase

type T = (phrase: string) => string;
const phrase = t('element');

console.log(phrase);
// => 要素

Translate listed phrases

type T = (phrases: string[], useLastSeparator?: boolean) => string;
const list = t(['element', 'attribute', 'value']);

console.log(list);
// => 「要素」「属性」「値」

/* If locale is "en" */
console.log(list);
// => "element", "attribute", "value"
const list = t(['element', 'attribute', 'value'], true);

console.log(list);
// => 「要素」「属性」「値」

/* If locale is "en" */
console.log(list);
// => "element", "attribute" and "value"

It converts the character-separated list specified in each locale.

LocaleSeparatorBefore CharAfter CharLast Separator
en, (comma + space)" (double quote)" (double quote)and (space + chars + space )
janone (empty string) (left corner bracket) (right corner bracket)none (empty string)

Avoid translation

The autocomplete is defined as オートコンプリート in the JA dictionary. However, It avoids translation if the number placeholder includes * (asterisk). It is an effective means if you want a code or a specific name.

const phrase = t('the "{0}" {1}', 'autocomplete', 'attribute');
console.log(phrase);
// => 属性「オートコンプリート」

const phrase = t('the "{0*}" {1}', 'autocomplete', 'attribute');
console.log(phrase);
// => 属性「autocomplete」

Another means is that it surrounds % (percentage) to a phrase. It is effective when you use listing.

const phrase = t('the "{0}" {1}', '%autocomplete%', 'attribute');
console.log(phrase);
// => 属性「autocomplete」

const list = t(['element', '%attribute%', 'value']);
console.log(list);
// => 「要素」「attribute」「値」
4.5.0-alpha.0

5 days ago

4.5.0

5 days ago

4.4.0

25 days ago

4.3.0

1 month ago

4.2.0

1 month ago

4.2.1-dev.1

1 month ago

4.1.1-alpha.0

2 months ago

4.1.0

3 months ago

4.0.1

3 months ago

4.0.0

3 months ago

4.0.0-rc.1

3 months ago

4.0.0-alpha.12

3 months ago

4.0.0-rc.0

3 months ago

4.0.0-alpha.11

3 months ago

4.0.0-alpha.10

3 months ago

4.0.0-dev.23

3 months ago

4.0.0-dev.12

4 months ago

4.0.0-alpha.9

4 months ago

4.0.0-alpha.8

4 months ago

4.0.0-alpha.7

4 months ago

4.0.0-dev.20

5 months ago

4.0.0-dev.10

5 months ago

4.0.0-alpha.5

7 months ago

4.0.0-alpha.6

5 months ago

4.0.0-alpha.3

7 months ago

4.0.0-alpha.4

7 months ago

4.0.0-alpha.1

9 months ago

3.0.0-dev.300

7 months ago

4.0.0-alpha.2

7 months ago

3.10.0

8 months ago

3.12.0

5 months ago

4.0.0-dev.0

5 months ago

3.0.0-dev.290

7 months ago

4.0.0-dev.28

7 months ago

3.9.0

11 months ago

3.0.0-dev.186

1 year ago

3.8.0

1 year ago

3.4.0

1 year ago

3.6.0

1 year ago

3.0.0-dev.96

1 year ago

3.0.0-dev.95

1 year ago

3.7.0

1 year ago

3.5.0

1 year ago

3.0.0-dev.177

1 year ago

3.0.0-dev.176

1 year ago

3.3.0

1 year ago

3.0.0-alpha.6

1 year ago

3.0.0-alpha.5

1 year ago

3.0.0-dev.43

1 year ago

3.0.0-dev.42

1 year ago

3.0.0-dev.40

1 year ago

3.2.0

1 year ago

3.0.0-canary.25

1 year ago

3.0.0-alpha.27

1 year ago

3.0.0-dev.54

1 year ago

3.0.0-dev.51

1 year ago

2.1.2

1 year ago

3.0.0-dev.38

1 year ago

3.0.0-rc.2

1 year ago

3.0.0-rc.1

1 year ago

3.0.0-rc.0

1 year ago

3.0.0-rc.5

1 year ago

3.0.0-rc.4

1 year ago

3.0.0-rc.3

1 year ago

3.0.0-dev.50.40

1 year ago

2.1.2-canary.24

1 year ago

2.1.2-canary.22

1 year ago

3.0.0-alpha.1

2 years ago

3.0.0-alpha.3

2 years ago

3.0.0-alpha.2

2 years ago

3.0.0-alpha.4

2 years ago

3.0.0-alpha.0

2 years ago

2.1.1

2 years ago

2.1.0

2 years ago

2.0.0-rc.3

2 years ago

2.0.0-rc.4

2 years ago

2.0.0-rc.5

2 years ago

2.0.0-rc.1

2 years ago

2.0.0

2 years ago

2.0.0-rc.0

2 years ago

2.0.0-alpha.0

2 years ago

2.0.0-dev.36

2 years ago

2.0.0-alpha.0.36

2 years ago

1.2.0

3 years ago

1.1.2

3 years ago

1.1.1

3 years ago

1.2.0-alpha.33

3 years ago

1.1.0

3 years ago

1.0.0

4 years ago

1.0.0-alpha.10

4 years ago

1.0.0-alpha.9

4 years ago

1.0.0-alpha.8

4 years ago

1.0.0-alpha.7

4 years ago

1.0.0-alpha.6

4 years ago

1.0.0-alpha.5

5 years ago

1.0.0-alpha.4

5 years ago

1.0.0-alpha.3

5 years ago

1.0.0-alpha.2

5 years ago

1.0.0-alpha.1

5 years ago

1.0.0-alpha.0

5 years ago