Skip to content

Commit 30ad604

Browse files
committed
feat: generic useI18n
BREAKING CHANGE: related types renamed
1 parent 66ba2f1 commit 30ad604

File tree

20 files changed

+165
-74
lines changed

20 files changed

+165
-74
lines changed
File renamed without changes.

packages/common-helpers/.npmignore

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# folders
2+
.vscode
3+
.circleci
4+
docs
5+
src
6+
node_modules
7+
playground
8+
9+
# files
10+
.releaserc
11+
Procfile
12+
template.njk
13+
package-lock.json
14+
*.lock
15+
*.tsbuildinfo
16+
*.log
17+
*.html

packages/common-helpers/src/i18n.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
import _ from "lodash";
22

3-
import type { iPluginOptions, tPluginLocaleKey } from "@open-xamu-co/ui-common-types";
4-
53
/**
64
* I18n Composable
75
*
86
* @composable
97
*/
10-
export default function useI18n(options: iPluginOptions = {}) {
8+
export default function useI18n<L extends Record<string, string | Record<string, string>>>(
9+
options: { locale?: L } = {}
10+
) {
1111
/**
1212
* Interpolates localized text
1313
*
1414
* @param key key to the text to interpolate. Ex: "hello_name" => "Hello {name}!"
1515
* @param data Optional number or variables to interpolate into text
1616
* @returns {string}
1717
*/
18-
function t(
19-
key: tPluginLocaleKey,
18+
function t<K extends string & keyof L, Ko extends L[K], KA extends string & keyof Ko>(
19+
key: Ko extends string ? K : `${K}.${KA}`,
2020
data: number | { [key: string]: unknown; count?: number } = {},
2121
fallback = `No locale for "${key}" provided`
2222
): string {
@@ -48,7 +48,9 @@ export default function useI18n(options: iPluginOptions = {}) {
4848
* @param key interpolation key to check
4949
* @returns {boolean} true if the key exists
5050
*/
51-
function te(key: string): key is tPluginLocaleKey {
51+
function te<K extends string & keyof L, Ko extends L[K], KA extends string & keyof Ko>(
52+
key: string
53+
): key is Ko extends string ? K : `${K}.${KA}` {
5254
return _.has(options.locale || {}, key);
5355
}
5456

packages/common-helpers/src/locale/en.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import _ from "lodash";
22

33
import type {
4-
iLocaleBase,
5-
iLocaleForm,
6-
iLocaleInput,
7-
iLocaleModal,
8-
iLocalePagination,
9-
iLocaleTable,
4+
tLocaleBase,
5+
tLocaleForm,
6+
tLocaleInput,
7+
tLocaleModal,
8+
tLocalePagination,
9+
tLocaleTable,
1010
tPluginLocale,
1111
} from "@open-xamu-co/ui-common-types";
1212

@@ -15,7 +15,7 @@ import type {
1515
*
1616
* @locale en
1717
*/
18-
export const localeBase: iLocaleBase = {
18+
export const localeBase: tLocaleBase = {
1919
yes: "Yes",
2020
no: "No",
2121
increase: "increase",
@@ -56,7 +56,7 @@ export const localeBase: iLocaleBase = {
5656
*
5757
* @locale en
5858
*/
59-
export const localeInput: iLocaleInput = {
59+
export const localeInput: tLocaleInput = {
6060
select_selected: "Selected",
6161
select_placeholder: "--SELECT--",
6262
select_restablish_field: "Restablish field",
@@ -88,7 +88,7 @@ export const localeInput: iLocaleInput = {
8888
*
8989
* @locale en
9090
*/
91-
export const localeModal: iLocaleModal = {
91+
export const localeModal: tLocaleModal = {
9292
modal_taking_too_long: "Taking too long?",
9393
swal: {
9494
modal_unauthorized: "Unauthorized",
@@ -101,7 +101,7 @@ export const localeModal: iLocaleModal = {
101101
*
102102
* @locale en
103103
*/
104-
export const localeForm: iLocaleForm = {
104+
export const localeForm: tLocaleForm = {
105105
form_required_options: "Options are required",
106106
form_requires_n_values:
107107
"No values are required | A value is required | {count} values are required",
@@ -134,7 +134,7 @@ export const localeForm: iLocaleForm = {
134134
*
135135
* @locale en
136136
*/
137-
export const localeTable: iLocaleTable = {
137+
export const localeTable: tLocaleTable = {
138138
table_see_values: "See {name}",
139139
table_see_name: 'See: "{name}"',
140140
table_create_new: "Create new",
@@ -181,7 +181,7 @@ export const localeTable: iLocaleTable = {
181181
*
182182
* @locale en
183183
*/
184-
export const localePagination: iLocalePagination = {
184+
export const localePagination: tLocalePagination = {
185185
pagination_items: "No items | Single item | {count} items",
186186
pagination_pages: "No pages | Single page | {count} pages",
187187
pagination_order_relevance: "Order: relevance",
@@ -200,7 +200,7 @@ export const localePagination: iLocalePagination = {
200200
*
201201
* @locale en
202202
*/
203-
const enLocale: tPluginLocale = {
203+
const etLocale: tPluginLocale = {
204204
..._.omit(localeBase, "swal"),
205205
..._.omit(localeInput, "swal"),
206206
..._.omit(localeModal, "swal"),
@@ -217,4 +217,4 @@ const enLocale: tPluginLocale = {
217217
},
218218
};
219219

220-
export default enLocale;
220+
export default etLocale;

packages/common-helpers/src/locale/es.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import _ from "lodash";
22

33
import type {
4-
iLocaleBase,
5-
iLocaleForm,
6-
iLocaleInput,
7-
iLocaleModal,
8-
iLocalePagination,
9-
iLocaleTable,
4+
tLocaleBase,
5+
tLocaleForm,
6+
tLocaleInput,
7+
tLocaleModal,
8+
tLocalePagination,
9+
tLocaleTable,
1010
tPluginLocale,
1111
} from "@open-xamu-co/ui-common-types";
1212

@@ -15,7 +15,7 @@ import type {
1515
*
1616
* @locale es
1717
*/
18-
export const localeBase: iLocaleBase = {
18+
export const localeBase: tLocaleBase = {
1919
yes: "Si",
2020
no: "No",
2121
increase: "Aumentar",
@@ -56,7 +56,7 @@ export const localeBase: iLocaleBase = {
5656
*
5757
* @locale es
5858
*/
59-
export const localeInput: iLocaleInput = {
59+
export const localeInput: tLocaleInput = {
6060
select_selected: "Seleccionado",
6161
select_placeholder: "--SELECCIONAR--",
6262
select_restablish_field: "Restablecer campo",
@@ -89,7 +89,7 @@ export const localeInput: iLocaleInput = {
8989
*
9090
* @locale es
9191
*/
92-
export const localeModal: iLocaleModal = {
92+
export const localeModal: tLocaleModal = {
9393
modal_taking_too_long: "¿Esta tardando demasiado?",
9494
swal: {
9595
modal_unauthorized: "No autorizado",
@@ -102,7 +102,7 @@ export const localeModal: iLocaleModal = {
102102
*
103103
* @locale es
104104
*/
105-
export const localeForm: iLocaleForm = {
105+
export const localeForm: tLocaleForm = {
106106
form_required_options: "Las opciones son requeridas",
107107
form_requires_n_values:
108108
"No se requieren valores | Se requiere un valor | {count} valores son requeridos",
@@ -135,7 +135,7 @@ export const localeForm: iLocaleForm = {
135135
*
136136
* @locale es
137137
*/
138-
export const localeTable: iLocaleTable = {
138+
export const localeTable: tLocaleTable = {
139139
table_see_values: "Ver {name}",
140140
table_see_name: 'Ver: "{name}"',
141141
table_create_new: "Crear nuevo",
@@ -182,7 +182,7 @@ export const localeTable: iLocaleTable = {
182182
*
183183
* @locale es
184184
*/
185-
export const localePagination: iLocalePagination = {
185+
export const localePagination: tLocalePagination = {
186186
pagination_items: "Sin elementos | Único elemento | {count} elementos",
187187
pagination_pages: "Sin páginas | Única página | {count} páginas",
188188
pagination_order_relevance: "Orden: relevancia",

packages/common-types/.npmignore

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# folders
2+
.vscode
3+
.circleci
4+
docs
5+
src
6+
node_modules
7+
playground
8+
9+
# files
10+
.releaserc
11+
Procfile
12+
template.njk
13+
package-lock.json
14+
*.lock
15+
*.tsbuildinfo
16+
*.log
17+
*.html

packages/common-types/src/locale.ts

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* @localeType
55
*/
6-
export interface iLocaleBase {
6+
export type tLocaleBase = {
77
/** @example "Yes" */
88
yes: string;
99
/** @example "No" */
@@ -66,14 +66,14 @@ export interface iLocaleBase {
6666
/** @example "Don't close this window while we finish the task" */
6767
dont_close_window: string;
6868
};
69-
}
69+
};
7070

7171
/**
7272
* Input locale
7373
*
7474
* @localeType
7575
*/
76-
export interface iLocaleInput {
76+
export type tLocaleInput = {
7777
/** @example "Selected" */
7878
select_selected: string;
7979
/** @example "--SELECT--" */
@@ -118,14 +118,14 @@ export interface iLocaleInput {
118118
/** @example "There was an error uploding the files, try again later" */
119119
file_unknown_error_text: string;
120120
};
121-
}
121+
};
122122

123123
/**
124124
* Modal locale
125125
*
126126
* @localeType
127127
*/
128-
export interface iLocaleModal {
128+
export type tLocaleModal = {
129129
/** @example "Taking too long?" */
130130
modal_taking_too_long: string;
131131
swal: {
@@ -134,14 +134,14 @@ export interface iLocaleModal {
134134
/** @example "You are not allowed to perform this action" */
135135
modal_unauthorized_text: string;
136136
};
137-
}
137+
};
138138

139139
/**
140140
* Form locale
141141
*
142142
* @localeType
143143
*/
144-
export interface iLocaleForm {
144+
export type tLocaleForm = {
145145
/** @example "Options are required" */
146146
form_required_options: string;
147147
/** @example "No values are required | A value is required | {count} values are required" */
@@ -189,14 +189,14 @@ export interface iLocaleForm {
189189
/** @example "Invalid data" */
190190
form_invalid_data: string;
191191
// swal: {};
192-
}
192+
};
193193

194194
/**
195195
* Table locale
196196
*
197197
* @localeType
198198
*/
199-
export interface iLocaleTable {
199+
export type tLocaleTable = {
200200
/** @example "See {name}" */
201201
table_see_values: string;
202202
/** @example "See: \"{name}\"" */
@@ -259,14 +259,14 @@ export interface iLocaleTable {
259259
/** @example "The item may not have been cloned | The items may not have been cloned" */
260260
table_possibly_not_cloned: string;
261261
};
262-
}
262+
};
263263

264264
/**
265265
* Pagination locale
266266
*
267267
* @localeType
268268
*/
269-
export interface iLocalePagination {
269+
export type tLocalePagination = {
270270
/** @example "No items | Single item | {count} items" */
271271
pagination_items: string;
272272
/** @example "No pages | Single page | {count} pages" */
@@ -288,25 +288,16 @@ export interface iLocalePagination {
288288
/** @example "Filter by:" */
289289
pagination_filter_by: string;
290290
// swal: {};
291-
}
291+
};
292292

293293
/**
294294
* Plugin locale
295295
*
296296
* @localeType
297297
*/
298-
export type tPluginLocale = iLocaleBase &
299-
iLocaleInput &
300-
iLocaleModal &
301-
iLocaleForm &
302-
iLocaleTable &
303-
iLocalePagination;
304-
305-
/**
306-
* Plugin locale key union
307-
*
308-
* @localeType
309-
*/
310-
export type tPluginLocaleKey =
311-
| Exclude<keyof tPluginLocale, "swal">
312-
| `swal.${keyof Required<tPluginLocale>["swal"]}`;
298+
export type tPluginLocale = tLocaleBase &
299+
tLocaleInput &
300+
tLocaleModal &
301+
tLocaleForm &
302+
tLocaleTable &
303+
tLocalePagination;

packages/components-vue/.npmignore

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# folders
2+
.vscode
3+
.circleci
4+
docs
5+
node_modules
6+
playground
7+
8+
# files
9+
.releaserc
10+
Procfile
11+
template.njk
12+
package-lock.json
13+
*.lock
14+
*.tsbuildinfo
15+
*.log
16+
*.html

packages/components-vue/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
"@open-xamu-co/ui-common-types": "link:../common-types",
6262
"@types/lodash": "^4.14.192",
6363
"@types/validator": "^13.11.1",
64+
"js-md5": "^0.8.3",
6465
"lodash": "^4.17.21",
6566
"nanoid": "^4.0.2",
6667
"sweetalert2": "^11.7.5",

0 commit comments

Comments
 (0)