@vocab/react 0.0.0-intl-dep-bump-202301323944 → 0.0.0-package-files-20231142931
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +13 -4
- package/package.json +5 -2
- package/src/index.tsx +0 -167
package/CHANGELOG.md
CHANGED
|
@@ -1,13 +1,22 @@
|
|
|
1
1
|
# @vocab/react
|
|
2
2
|
|
|
3
|
-
## 0.0.0-
|
|
3
|
+
## 0.0.0-package-files-20231142931
|
|
4
4
|
|
|
5
5
|
### Patch Changes
|
|
6
6
|
|
|
7
|
-
- [`
|
|
7
|
+
- [`29c81d3`](https://github.com/seek-oss/vocab/commit/29c81d370799f631d97c45e727b8cf81453bd398) Thanks [@askoufis](https://github.com/askoufis)! - Exclude source files from package build
|
|
8
8
|
|
|
9
|
-
- Updated dependencies [[`
|
|
10
|
-
- @vocab/types@0.0.0-
|
|
9
|
+
- Updated dependencies [[`29c81d3`](https://github.com/seek-oss/vocab/commit/29c81d370799f631d97c45e727b8cf81453bd398)]:
|
|
10
|
+
- @vocab/types@0.0.0-package-files-20231142931
|
|
11
|
+
|
|
12
|
+
## 1.1.3
|
|
13
|
+
|
|
14
|
+
### Patch Changes
|
|
15
|
+
|
|
16
|
+
- [`e5a066c`](https://github.com/seek-oss/vocab/commit/e5a066c8a7539a62a9c1c4d813aa87461ba43cdc) [#96](https://github.com/seek-oss/vocab/pull/96) Thanks [@askoufis](https://github.com/askoufis)! - Update `intl-messageformat` dependencies
|
|
17
|
+
|
|
18
|
+
- Updated dependencies [[`e5a066c`](https://github.com/seek-oss/vocab/commit/e5a066c8a7539a62a9c1c4d813aa87461ba43cdc)]:
|
|
19
|
+
- @vocab/types@1.1.1
|
|
11
20
|
|
|
12
21
|
## 1.1.2
|
|
13
22
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vocab/react",
|
|
3
|
-
"version": "0.0.0-
|
|
3
|
+
"version": "0.0.0-package-files-20231142931",
|
|
4
4
|
"main": "dist/vocab-react.cjs.js",
|
|
5
5
|
"module": "dist/vocab-react.esm.js",
|
|
6
6
|
"author": "SEEK",
|
|
@@ -8,8 +8,11 @@
|
|
|
8
8
|
"peerDependencies": {
|
|
9
9
|
"react": ">=16.3.0"
|
|
10
10
|
},
|
|
11
|
+
"files": [
|
|
12
|
+
"dist"
|
|
13
|
+
],
|
|
11
14
|
"dependencies": {
|
|
12
|
-
"@vocab/types": "^0.0.0-
|
|
15
|
+
"@vocab/types": "^0.0.0-package-files-20231142931",
|
|
13
16
|
"intl-messageformat": "^10.0.0"
|
|
14
17
|
},
|
|
15
18
|
"devDependencies": {
|
package/src/index.tsx
DELETED
|
@@ -1,167 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
TranslationFile,
|
|
3
|
-
LanguageName,
|
|
4
|
-
ParsedFormatFnByKey,
|
|
5
|
-
ParsedFormatFn,
|
|
6
|
-
} from '@vocab/types';
|
|
7
|
-
import React, {
|
|
8
|
-
ReactNode,
|
|
9
|
-
useContext,
|
|
10
|
-
useMemo,
|
|
11
|
-
useReducer,
|
|
12
|
-
isValidElement,
|
|
13
|
-
cloneElement,
|
|
14
|
-
useCallback,
|
|
15
|
-
} from 'react';
|
|
16
|
-
|
|
17
|
-
type Locale = string;
|
|
18
|
-
|
|
19
|
-
interface TranslationsValue {
|
|
20
|
-
language: LanguageName;
|
|
21
|
-
locale?: Locale;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
const TranslationsContext = React.createContext<TranslationsValue | undefined>(
|
|
25
|
-
undefined,
|
|
26
|
-
);
|
|
27
|
-
|
|
28
|
-
interface VocabProviderProps extends TranslationsValue {
|
|
29
|
-
children: ReactNode;
|
|
30
|
-
}
|
|
31
|
-
export const VocabProvider = ({
|
|
32
|
-
children,
|
|
33
|
-
language,
|
|
34
|
-
locale,
|
|
35
|
-
}: VocabProviderProps) => {
|
|
36
|
-
const value = useMemo(() => ({ language, locale }), [language, locale]);
|
|
37
|
-
|
|
38
|
-
return (
|
|
39
|
-
<TranslationsContext.Provider value={value}>
|
|
40
|
-
{children}
|
|
41
|
-
</TranslationsContext.Provider>
|
|
42
|
-
);
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
export const useLanguage = (): TranslationsValue => {
|
|
46
|
-
const context = useContext(TranslationsContext);
|
|
47
|
-
if (!context) {
|
|
48
|
-
throw new Error(
|
|
49
|
-
'Attempted to access translation without Vocab context set. Did you forget to render VocabProvider?',
|
|
50
|
-
);
|
|
51
|
-
}
|
|
52
|
-
if (!context.language) {
|
|
53
|
-
throw new Error(
|
|
54
|
-
'Attempted to access translation without language set. Did you forget to pass language to VocabProvider?',
|
|
55
|
-
);
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
return context;
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
const SERVER_RENDERING = typeof window === 'undefined';
|
|
62
|
-
|
|
63
|
-
type FormatXMLElementReactNodeFn = (parts: ReactNode[]) => ReactNode;
|
|
64
|
-
|
|
65
|
-
type MapToReactNodeFunction<Params extends Record<string, any>> = {
|
|
66
|
-
[key in keyof Params]: Params[key] extends ParsedFormatFn
|
|
67
|
-
? FormatXMLElementReactNodeFn
|
|
68
|
-
: Params[key];
|
|
69
|
-
};
|
|
70
|
-
|
|
71
|
-
type TranslateFn<FormatFnByKey extends ParsedFormatFnByKey> = {
|
|
72
|
-
<TranslationKey extends keyof FormatFnByKey>(
|
|
73
|
-
key: TranslationKey,
|
|
74
|
-
params: MapToReactNodeFunction<
|
|
75
|
-
Parameters<FormatFnByKey[TranslationKey]>[0]
|
|
76
|
-
>,
|
|
77
|
-
): ReturnType<FormatFnByKey[TranslationKey]> extends string
|
|
78
|
-
? string
|
|
79
|
-
: ReactNode | string | Array<ReactNode | string>;
|
|
80
|
-
<TranslationKey extends keyof FormatFnByKey>(
|
|
81
|
-
key: Parameters<FormatFnByKey[TranslationKey]>[0] extends Record<
|
|
82
|
-
string,
|
|
83
|
-
any
|
|
84
|
-
>
|
|
85
|
-
? never
|
|
86
|
-
: TranslationKey,
|
|
87
|
-
): string;
|
|
88
|
-
};
|
|
89
|
-
|
|
90
|
-
export function useTranslations<
|
|
91
|
-
Language extends string,
|
|
92
|
-
FormatFnByKey extends ParsedFormatFnByKey,
|
|
93
|
-
>(
|
|
94
|
-
translations: TranslationFile<Language, FormatFnByKey>,
|
|
95
|
-
): {
|
|
96
|
-
ready: boolean;
|
|
97
|
-
t: TranslateFn<FormatFnByKey>;
|
|
98
|
-
} {
|
|
99
|
-
const { language, locale } = useLanguage();
|
|
100
|
-
const [, forceRender] = useReducer((s: number) => s + 1, 0);
|
|
101
|
-
|
|
102
|
-
const translationsObject = translations.getLoadedMessages(
|
|
103
|
-
language as any,
|
|
104
|
-
locale || language,
|
|
105
|
-
);
|
|
106
|
-
|
|
107
|
-
let ready = true;
|
|
108
|
-
|
|
109
|
-
if (!translationsObject) {
|
|
110
|
-
if (SERVER_RENDERING) {
|
|
111
|
-
throw new Error(
|
|
112
|
-
`Translations not synchronously available on server render. Applying translations dynamically server-side is not supported.`,
|
|
113
|
-
);
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
translations.load(language as any).then(() => {
|
|
117
|
-
forceRender();
|
|
118
|
-
});
|
|
119
|
-
ready = false;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
const t = useCallback(
|
|
123
|
-
(key: string, params?: any) => {
|
|
124
|
-
if (!translationsObject) {
|
|
125
|
-
return ' ';
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
const message = translationsObject?.[key];
|
|
129
|
-
|
|
130
|
-
if (!message) {
|
|
131
|
-
// eslint-disable-next-line no-console
|
|
132
|
-
console.error(
|
|
133
|
-
`Unable to find translation for key "${key}". Possible keys are ${Object.keys(
|
|
134
|
-
translationsObject,
|
|
135
|
-
)
|
|
136
|
-
.map((v) => `"${v}"`)
|
|
137
|
-
.join(', ')}`,
|
|
138
|
-
);
|
|
139
|
-
return '';
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
const result = message.format(params);
|
|
143
|
-
|
|
144
|
-
if (Array.isArray(result)) {
|
|
145
|
-
for (let i = 0; i < result.length; i++) {
|
|
146
|
-
const item = result[i];
|
|
147
|
-
if (
|
|
148
|
-
typeof item === 'object' &&
|
|
149
|
-
item &&
|
|
150
|
-
!item.key &&
|
|
151
|
-
isValidElement(item)
|
|
152
|
-
) {
|
|
153
|
-
result[i] = cloneElement(item, { key: `_vocab-${i}` });
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
return result;
|
|
159
|
-
},
|
|
160
|
-
[translationsObject],
|
|
161
|
-
);
|
|
162
|
-
|
|
163
|
-
return {
|
|
164
|
-
ready,
|
|
165
|
-
t,
|
|
166
|
-
};
|
|
167
|
-
}
|