fnando / i18n

A small library to provide the I18n translations on the JavaScript.
https://fnando.github.io/i18n/
MIT License
169 stars 20 forks source link

Fully support dots as part of translation keys when using custom separators. #76

Closed fnando closed 1 year ago

fnando commented 1 year ago

This is a rebase of #63 with some additional changes.

PR Checklist ### PR Structure - [x] This PR has reasonably narrow scope (if not, break it down into smaller PRs). - [ ] This PR avoids mixing refactoring changes with feature changes (split into two PRs otherwise). - [x] This PR's title starts is concise and descriptive. ### Thoroughness - [x] This PR adds tests for the most critical parts of the new functionality or fixes. - [x] I've updated any docs, `.md` files, etc… affected by this change.

What

Added support for the character . in the translation key when using custom separators.

Why

Some translation keys can accurately reflect the translated English sentence, for example: 'Only available for Desktop. On the Mobile App, the display ecc...', so support for all punctuation characters is needed

The 3.9.2 version worked fine with dots.

Known limitations / Additional info

The lodash 'get' function splits by char '.' and looks for the keys, like dictionary[splitKey[0]][splitKey[1]][...];

// doesn't support this:
{
    "standard": {
        "Only available for Desktop. On the Mobile App, the display etc...": "translation",
    }
}

The problem is when a key contains a '.' because the lodash 'get' function interprets it not as part of the key but as a separator. So, the solution is to use a reduce function to iterate through all the keys in the path generated by: https://github.com/fnando/i18n/blob/d4a14ca41d815403ee331f0e63d9b41aa612cbbc/src/helpers/lookup.ts#L34-L36

mattias-persson commented 1 year ago

Is anything keeping this from being merged? 🙂