Skip to content

Commit

Permalink
Fix: non-serialization of unscoped json (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
robisim74 committed Apr 4, 2023
1 parent 4fe752e commit 65723c1
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ and optionally contains:

### Translation functions
`TranslationFn` interface can be implemented to change the behavior of the library:
- `loadTranslation$?` Function to load translation data
- `loadTranslation$` Function to load translation data

## APIs
### Components
Expand Down
2 changes: 1 addition & 1 deletion docs/adapters.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const loadTranslation$: LoadTranslationFn = server$((lang: string, asset: string
translationData[`/i18n/${lang}/${asset}.json`]?.()
);
```
> Using `server$`, translation data is always accessed on the server
> Using `server$` instead of `$`, translation data is always accessed on the server
If your production environment doesn't support _dynamic import_, you might prefer this way:
```typescript
Expand Down
3 changes: 3 additions & 0 deletions packages/qwik-speak/.eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,8 @@ module.exports = {
'@typescript-eslint/consistent-type-imports': 'warn',
'max-len': ["error", { "code": 120, "tabWidth": 2, "ignoreRegExpLiterals": true, "ignoreStrings": true, "ignoreTemplateLiterals": true }],
'quotes': ["error", "single", { "avoidEscape": true, "allowTemplateLiterals": true }],
"prefer-const": ["error", {
"destructuring": "all"
}],
},
};
9 changes: 7 additions & 2 deletions packages/qwik-speak/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ export const loadTranslations = async (
if (data?.source) {
if (!isDev && isServer && assets.includes(data.asset)) {
// In prod mode, assets are not serialized
for (const [key, value] of Object.entries<Translation>(data.source)) {
for (let [key, value] of Object.entries<Translation>(data.source)) {
// Depth 0: convert string to String object
if (typeof value === 'string') {
value = new String(value);
}
translation[lang][key] = noSerialize(value);
}
} else {
Expand Down Expand Up @@ -87,7 +91,8 @@ export const getValue = (
undefined, data);

if (value) {
if (typeof value === 'string') return params ? transpileParams(value, params) : value;
if (typeof value === 'string' || value instanceof String)
return params ? transpileParams(value.toString(), params) : value.toString();
if (typeof value === 'object') return value;
}

Expand Down
4 changes: 4 additions & 0 deletions packages/qwik-speak/src/tests/core.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ describe('core', () => {
value = getValue('SUBKEY1.BB', { KEY1: 'key1', SUBKEY1: { AA: 'aa' } });
expect(value).toBeUndefined();
});
test('getValue when String', () => {
const value = getValue('KEY1', { KEY1: new String('key1') });
expect(value).toBe('key1');
});
test('transpileParams', () => {
let value = transpileParams('Test {{param}}', { param: 'params' });
expect(value).toBe('Test params');
Expand Down

0 comments on commit 65723c1

Please sign in to comment.