4.3.7 • Published 1 month ago

@umatch/language v4.3.7

Weekly downloads
-
License
MIT
Repository
github
Last release
1 month ago

Language

Simple internationalization framework

Check out tests/language.test.ts for some examples.

deepReplace

Replaces all occurrences of {{ value }} in all strings inside target object (which may be nested) with the corresponding value in the replacements object (dictionary).

Throws an error if an entry is not in the dictionary. This can be avoided by whitelisting specific keys, or all of them, via the allowedLeftovers parameter.

Automatically chooses between the "zero", "one" or "many" options for an entry If the entry only has keys with those names and the dictionary has a count property that is a number. Example:

const obj = {
  one: 'You have a new match, {{ name }}!',
  many: 'You have {{ count }} new matches, {{ name }}!',
};
// returns 'You have a new match, Bob!'
deepReplace(obj, { name: 'Bob', count: 1 });
// returns 'You have 2 new matches, Bob!'
deepReplace(obj, { name: 'Bob', count: 2 });

If the entry equivalent to the count missing, throws an error. For example:

// throws "Missing translation for count 'zero'"
deepReplace(obj, { name: 'Bob', count: 0 });

Finally, if not all keys in the target are "zero", "one" or "many", behaves normally.

const obj2 = {
  one: 'You have a new match, {{ name }}!',
  many: 'You have {{ count }} new matches, {{ name }}!',
  too_many: 'WOW! You have over 9000 new matches, {{ name }}!',
};
// returns {
//     one: "You have a new match, Bob!",
//     many: "You have 0 new matches, Bob!",
//     too_many: "WOW! You have over 9000 new matches, Bob!"
// }
deepReplace(obj2, { name: 'Bob', count: 0 });
4.3.6

1 month ago

4.3.7

1 month ago

4.3.5

5 months ago

4.3.4

5 months ago

4.3.3

5 months ago

4.3.2

8 months ago

4.3.1

8 months ago

4.3.0

8 months ago

4.2.0

10 months ago

4.1.1

11 months ago

4.1.0

11 months ago

3.2.0

12 months ago

3.1.0

12 months ago

4.0.1

12 months ago

4.0.0

12 months ago

3.0.0

1 year ago

2.1.2

1 year ago

2.1.1

1 year ago

2.1.4

1 year ago

2.1.3

1 year ago

2.1.0

1 year ago

2.0.0

1 year ago

1.2.2

1 year ago

1.2.0

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.1.3

2 years ago

1.2.1

2 years ago

1.1.2

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago