cibernox / svelte-intl-precompile

I18n library for Svelte.js that analyzes your keys at build time for max performance and minimal footprint
https://svelte-intl-precompile.com
ISC License
274 stars 13 forks source link

Variables in string inside arrays do not work #62

Closed vegardlarsen closed 1 year ago

vegardlarsen commented 1 year ago

I tried in the playground to test something that was breaking in my project:

{
  "y": "foo {bar}",
  "x": ["{foo} something"],
  "z": { "nested": "foo {bar}" }
}

Output

import { __interpolate } from "svelte-intl-precompile";
export default {
  "y": bar => `foo ${__interpolate(bar)}`,
  "x": ["{foo} something"],
  "z.nested": bar => `foo ${__interpolate(bar)}`
};

Notice that the variable inside the x array is not interpolated.

Expected output

This is what I expected, but it may very well need to be different.

import { __interpolate } from "svelte-intl-precompile";
export default {
  "y": bar => `foo ${__interpolate(bar)}`,
  "x": [foo => `${__interpolate(foo)} something`],
  "z.nested": bar => `foo ${__interpolate(bar)}`
};
cibernox commented 1 year ago

I don't think I've ever considered a translation being an array? What's the usecase for that? I never encountered it. I suspect that since it's not an string the AST transformer just ignores it.

vegardlarsen commented 1 year ago

Good question. We have lists of various "product features" that sometimes contains different items in different languages. In svelte-i18n this was perfectly valid, but it may not be here.

I can probably work around this one if this is by design; and I can see that it may be problematic in terms of how the translations would interact e.g. with $json. Feel free to close this one.

cibernox commented 1 year ago

Out of curiosity, what does svelte-i18n do when you call $t('x')? Just uses the first key in the array? What if there are several entries in this array?

vegardlarsen commented 1 year ago

I don't know that it deals with it using $t. We used $json to extract the array, and then getMessageFormatter to do ICU formatting afterwards. Again, this doesn't make as much sense when precompiling.

My solution now will instead be to not use an array in the translations, and instead newline-separate the individual lines, and we can split them when using it.

cibernox commented 1 year ago

👍