Skip to content

Commit 9be9d76

Browse files
committed
Refactor: setLocale$ to storeLocale$ and QRL for functions
1 parent 57faa72 commit 9be9d76

File tree

12 files changed

+128
-132
lines changed

12 files changed

+128
-132
lines changed

.eslintrc.cjs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,5 @@ module.exports = {
3838
'no-console': 'off',
3939
'max-len': ["error", { "code": 120, "tabWidth": 2, "ignoreRegExpLiterals": true }],
4040
'quotes': ["error", "single", { "avoidEscape": true }],
41-
'qwik/valid-lexical-scope': 'off'
4241
},
4342
};

README.md

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,8 @@ export const resolveLocale$: ResolveLocaleFn = $(() => {
152152
/* Must contain the logic to resolve which locale to use during SSR */
153153
});
154154

155-
export const setLocale$: SetLocaleFn = $((locale: SpeakLocale) => {
156-
/* Must contain the logic to set the locale on Client when changes */
155+
export const storeLocale$: StoreLocaleFn = $((locale: SpeakLocale) => {
156+
/* Must contain the logic to store the locale on Client when changes */
157157
});
158158

159159
export const handleMissingTranslation$: HandleMissingTranslationFn = $((key: string, value?: string, params?: any, ctx?: SpeakState) => {
@@ -251,11 +251,7 @@ npm run build
251251
```
252252

253253
## What's new
254-
> Released v0.0.4
255-
256-
> Translate head
257-
> Preloading data
258-
> Fallback for missing values
254+
> Released v0.0.5
259255
260256
## License
261257
MIT

package-lock.json

Lines changed: 94 additions & 94 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@
3131
"@types/eslint": "8.4.5",
3232
"@types/jest": "^28.1.6",
3333
"@types/node": "latest",
34-
"@typescript-eslint/eslint-plugin": "5.30.6",
35-
"@typescript-eslint/parser": "5.30.6",
36-
"eslint": "8.19.0",
37-
"eslint-plugin-qwik": "0.0.36",
34+
"@typescript-eslint/eslint-plugin": "5.30.7",
35+
"@typescript-eslint/parser": "5.30.7",
36+
"eslint": "8.20.0",
37+
"eslint-plugin-qwik": "0.0.39",
3838
"jest": "^28.1.3",
3939
"node-fetch": "3.2.9",
4040
"np": "7.6.2",

src/app/speak-config.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { $, useUserContext } from '@builder.io/qwik';
22
import { isServer } from '@builder.io/qwik/build';
33
import { useLocation } from '@builder.io/qwik-city';
44
import { SpeakConfig, SpeakLocale, SpeakState, TranslateFn, Translation } from '../library/types';
5-
import { GetTranslationFn, ResolveLocaleFn, SetLocaleFn, HandleMissingTranslationFn } from '../library/types';
5+
import { GetTranslationFn, ResolveLocaleFn, StoreLocaleFn, HandleMissingTranslationFn } from '../library/types';
66
import { getValue } from '../library/core';
77

88
import { appTranslation } from './i18n';
@@ -65,8 +65,8 @@ export const getTranslateFn = (): TranslateFn => {
6565
return result ? JSON.parse(result[1]) : null; */
6666
});
6767

68-
// E.g. Set locale on Client replacing url
69-
const setLocale$: SetLocaleFn = $((locale: SpeakLocale) => {
68+
// E.g. Store locale on Client replacing url
69+
const storeLocale$: StoreLocaleFn = $((locale: SpeakLocale) => {
7070
const url = new URL(window.location.href);
7171
const lang = config.supportedLocales.find(x => url.pathname.startsWith(`/${x.lang}`))?.lang;
7272

@@ -87,7 +87,7 @@ export const getTranslateFn = (): TranslateFn => {
8787

8888
window.history.pushState({}, '', url);
8989

90-
// E.g. Set locale in cookie
90+
// E.g. Store locale in cookie
9191
/* document.cookie = `locale=${JSON.stringify(locale)};path=/`; */
9292
});
9393

@@ -106,7 +106,7 @@ export const getTranslateFn = (): TranslateFn => {
106106
return {
107107
/* getTranslation$: getTranslation$, */
108108
resolveLocale$: resolveLocale$,
109-
setLocale$: setLocale$,
109+
storeLocale$: storeLocale$,
110110
handleMissingTranslation$: handleMissingTranslation$
111111
};
112112
};

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export type {
99
export type {
1010
GetTranslationFn,
1111
ResolveLocaleFn,
12-
SetLocaleFn,
12+
StoreLocaleFn,
1313
HandleMissingTranslationFn,
1414
} from './library/types';
1515
// Hooks
@@ -20,7 +20,7 @@ export {
2020
useSpeakContext,
2121
useSpeakLocale,
2222
useTranslation,
23-
useSpeakConfig
23+
useSpeakConfig,
2424
} from './library/use-functions';
2525
// Functions
2626
export { changeLocale } from './library/change-locale';

src/library/change-locale.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ export const changeLocale = async (newLocale: SpeakLocale, ctx: SpeakState): Pro
1717
Object.assign(translation, newTranslation);
1818
Object.assign(locale, newLocale);
1919

20-
// Set the locale
21-
await translateFn.setLocale$(newLocale);
20+
// Store the locale
21+
await translateFn.storeLocale$(newLocale);
2222

2323
if (qDev) {
2424
console.debug('%cQwik Speak',

src/library/constants.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { $ } from '@builder.io/qwik';
22

33
import type { Translation } from './types';
4-
import type { GetTranslationFn, SetLocaleFn, HandleMissingTranslationFn, ResolveLocaleFn } from './types';
4+
import type { GetTranslationFn, StoreLocaleFn, HandleMissingTranslationFn, ResolveLocaleFn } from './types';
55
import { clone, isObject } from './utils';
66

77
export const getTranslation$: GetTranslationFn = $((lang: string, asset: string | Translation) => {
@@ -12,7 +12,7 @@ export const resolveLocale$: ResolveLocaleFn = $(() => {
1212
return null;
1313
});
1414

15-
export const setLocale$: SetLocaleFn = $(() => { });
15+
export const setLocale$: StoreLocaleFn = $(() => { });
1616

1717
export const handleMissingTranslation$: HandleMissingTranslationFn = $((key: string) => {
1818
return key;

src/library/types.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,22 @@ export type Translation = { [key: string]: any };
3636
/**
3737
* Must contain the logic to get translation data
3838
*/
39-
export type GetTranslationFn = (lang: string, asset: string | Translation) => ValueOrPromise<Translation>;
39+
export type GetTranslationFn = QRL<(lang: string, asset: string | Translation) => ValueOrPromise<Translation>>;
4040

4141
/**
4242
* Must contain the logic to resolve which locale to use during SSR
4343
*/
44-
export type ResolveLocaleFn = () => ValueOrPromise<SpeakLocale | null | undefined>;
44+
export type ResolveLocaleFn = QRL<() => ValueOrPromise<SpeakLocale | null | undefined>>;
4545

4646
/**
47-
* Must contain the logic to set the locale on Client when changes
47+
* Must contain the logic to store the locale on Client when changes
4848
*/
49-
export type SetLocaleFn = (locale: SpeakLocale) => ValueOrPromise<void>;
49+
export type StoreLocaleFn = QRL<(locale: SpeakLocale) => ValueOrPromise<void>>;
5050

5151
/**
5252
* Must contain the logic to handle missing values
5353
*/
54-
export type HandleMissingTranslationFn = (key: string, value?: string, params?: any, ctx?: SpeakState) => any;
54+
export type HandleMissingTranslationFn = QRL<(key: string, value?: string, params?: any, ctx?: SpeakState) => any>;
5555

5656
export interface TranslateFn {
5757
/**
@@ -63,9 +63,9 @@ export interface TranslateFn {
6363
*/
6464
resolveLocale$?: ResolveLocaleFn;
6565
/**
66-
* Function to set the locale on Client
66+
* Function to store the locale on Client
6767
*/
68-
setLocale$?: SetLocaleFn;
68+
storeLocale$?: StoreLocaleFn;
6969
/**
7070
* Function to handle missing values
7171
*/

src/library/use-speak.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export const useSpeak = (config: SpeakConfig, translateFn: TranslateFn = {}, lan
1717
// Assign functions
1818
translateFn.getTranslation$ = translateFn.getTranslation$ ?? getTranslation$;
1919
translateFn.resolveLocale$ = translateFn.resolveLocale$ ?? resolveLocale$;
20-
translateFn.setLocale$ = translateFn.setLocale$ ?? setLocale$;
20+
translateFn.storeLocale$ = translateFn.storeLocale$ ?? setLocale$;
2121
translateFn.handleMissingTranslation$ = translateFn.handleMissingTranslation$ ?? handleMissingTranslation$;
2222

2323
// Set initial state

src/tests/config.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { SpeakLocale, SpeakConfig, SpeakState, Translation } from '../library/types';
1+
import { $ } from '@builder.io/qwik';
2+
import { SpeakLocale, SpeakConfig, Translation, SpeakState } from '../library/types';
23

34
const translationData: Translation = {
45
'en-US': {
@@ -36,9 +37,9 @@ export const ctx: SpeakState = new Proxy({
3637
translation: translationData,
3738
config: config,
3839
translateFn: {
39-
getTranslation$: (lang: string, asset: string | Translation) => (<Translation>asset)[lang],
40-
resolveLocale$: () => null,
41-
setLocale$: () => { },
42-
handleMissingTranslation$: (key: string) => 'not found'
40+
getTranslation$: $((lang: string, asset: string | Translation) => (<Translation>asset)[lang]),
41+
resolveLocale$: $(() => null),
42+
storeLocale$: $(() => { }),
43+
handleMissingTranslation$: $((key: string) => 'not found')
4344
}
4445
}, {});

src/tests/translate.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ describe('translate function', () => {
1515
const value = t(['test', 'testParams'], { param: 'params' }, ctx);
1616
expect(value).toEqual(['Test', 'Test params']);
1717
});
18-
test('missing value', () => {
19-
const value = t('test1', {}, ctx);
18+
test('missing value', async () => {
19+
const value = await t('test1', {}, ctx);
2020
expect(value).toBe('not found');
2121
});
2222
test('key separator', () => {

0 commit comments

Comments
 (0)