Skip to content

Commit

Permalink
Fix: array of object as return value
Browse files Browse the repository at this point in the history
  • Loading branch information
robisim74 committed Mar 6, 2023
1 parent 36dc2b9 commit 293b701
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 13 deletions.
15 changes: 8 additions & 7 deletions docs/translate.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ $translate('home.title@@Qwik Speak')
}
}
```
- After extraction, it returns the value in json files
- After extraction, it returns the value in files
- In prod mod, `$translate` function is replaced by its translation both in server files and in chunks sent to the browser:
```jsx
`Qwik Speak`
```


## Params
## Params interpolation
`$translate` function accept params as well:
```jsx
$translate('home.greeting@@Hi! I am {{name}}', { name: 'Qwik Speak' })
Expand All @@ -35,7 +35,7 @@ Hi! I am Qwik Speak
```


## Arrays and objects
## Array of keys
`$translate` function accepts array of keys:
```jsx
$translate(['value1@@Value 1', 'value2@@Value 2'])
Expand All @@ -46,7 +46,8 @@ and returns an array of translated values:
["Value 1", "Value 2"]
```

It can get arrays and objects directly from json files:
## Arrays and objects as values
It can get arrays and objects directly from files:
```json
{
"home": {
Expand All @@ -70,15 +71,15 @@ $translate<string[]>('home.array')
$translate<Translation>('home.obj')
```

Finally, it is possible to set arrays and objects directly as the default value:
Finally, it is possible to set arrays and objects passing a _valid stringified_ default value:

```jsx
$translate<string[]>('home.array@@["one","two","three"]')
$translate<Translation>('home.obj@@{"one":"1","two":"2"}')
```
and access by array position:
You can also access by array position:
```jsx
$translate('home.array.2@@three')
```

> Make sure you are passing a valid stringified array or object as default value
> To reduce complexity (arrays and objects are _inlined_ during build) it is recommended to use objects with _a depth not greater than 1_. For the same reason, `params` interpolation is not supported when you return an array or an object
2 changes: 1 addition & 1 deletion src/library/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export const getValue = (
}

if (defaultValue) {
if (!/^[[{](?![[{]).*[\]}]$/.test(defaultValue))
if (!/^[[{].*[\]}]$/.test(defaultValue) || /^{{/.test(defaultValue))
return params ? transpileParams(defaultValue, params) : defaultValue;
// Default value is an array/object
return JSON.parse(defaultValue);
Expand Down
6 changes: 5 additions & 1 deletion src/tests/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ const translationData: Translation = {
array: ['Test1', 'Test2'],
},
one: 'One software developer',
other: '{{value}} software developers'
other: '{{value}} software developers',
arrayObjects: [
{ one: '1' },
{ two: '3' }
]
},
'it-IT': {
test: 'Prova'
Expand Down
9 changes: 8 additions & 1 deletion src/tests/translate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,14 @@ describe('translate function', () => {
});
test('object', () => {
const value = t('nested', {}, ctx);
expect(value).toEqual({ test: 'Test', array: ['Test1', 'Test2'], });
expect(value).toEqual({ test: 'Test', array: ['Test1', 'Test2'] });
});
test('array of objects', () => {
const value = t('arrayObjects', {}, ctx);
expect(value).toEqual([
{ one: '1' },
{ two: '3' }
]);
});
test('translate when locale changes', async () => {
await changeLocale({
Expand Down
4 changes: 3 additions & 1 deletion tools/extract/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,9 @@ export async function qwikSpeakExtract(options: QwikSpeakExtractOptions) {
[key, defaultValue] = key.split(resolvedOptions.keyValueSeparator);

// Objects/arrays
if (/^[[{](?![[{]).*[\]}]$/.test(defaultValue)) defaultValue = JSON.parse(defaultValue);
if (/^[[{].*[\]}]$/.test(defaultValue) && !/^{{/.test(defaultValue)) {
defaultValue = JSON.parse(defaultValue);
}

for (const lang of resolvedOptions.supportedLangs) {
deepSet(translation[lang], key.split(resolvedOptions.keySeparator), defaultValue || '');
Expand Down
6 changes: 6 additions & 0 deletions tools/tests/extract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ describe('extract', () => {
"two",
"three"
],
"arrayObjects": [
{
"one": "1",
"two": "2"
}
],
"dates": "Dates & relative time",
"devs": {
"one": "",
Expand Down
5 changes: 3 additions & 2 deletions tools/tests/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,10 @@ export const Home = component$(() => {
const state = useStore({ count: 0 });
const tArray = t('home.array@@["one", "two"]');
const tArray = t<string[]>('home.array@@["one", "two"]');
const item = t('home.array.2@@three');
const tObject = t('home.obj@@{"one": "1", "two": "2"}');
const tObject = t<Translation>('home.obj@@{"one": "1", "two": "2"}');
const tArrayObjects = t<Translation[]>('home.arrayObjects@@[{"one": "1", "two": "2"}]');
return (
<>
Expand Down

0 comments on commit 293b701

Please sign in to comment.