nanostores / i18n

A tiny (≈600 bytes) i18n library for React/Preact/Vue/Svelte
MIT License
229 stars 12 forks source link

Nesting of translations #29

Closed dartess closed 11 months ago

dartess commented 1 year ago
const messages = i18n('post', {
  a: {
    b: {
      c: 'deep value',
    },
  },
});

Is this a valid construct or not?

If not, it should be described in the documentation.

If valid, we should fix the work of the functions and fix the types:

Now it works, but TS is throwing an error:

Type '{ b: { c: string; }; }' is not assignable to type 'Translation'.
  Object literal may only specify known properties, and 'b' does not exist in type 'TranslationFunction<any[], string | TranslationsJSON | TranslationFunction<any[], string | TranslationsJSON | TranslationFunction<any[], string | TranslationsJSON | TranslationFunction<any[], string | ... 1 more ... | TranslationFunction<...>>>>>'.(2322)

Also it doesn't work with functions:

const messages = i18n('post', {
  a: {
    b: {
      c: count({
        one: '{count} comment',
        many: '{count} comments',
      })
    },
  },
});
Uncaught TypeError: t.a.b.c is not a function
ai commented 1 year ago

I prefer flat structure here. Let’s think that it is not valid.

How do you think we should fix ths docs?

dartess commented 1 year ago

Just to add a note that the translation structure should be flat in the Translations section.

Also the documentation states the following:

Plain JSON translations compatible with online translation services like Weblate.

But Weblate supports both formats: JSON file and JSON nested structure file.

I think this can be misleading.

ai commented 1 year ago

Please, send PR