Skip to content

Commit d594034

Browse files
authored
Merge pull request #16139 from rak-phillip/task/export-i18n-types
Improve type generation of the i18n plugin
2 parents 4b6bb7d + 6678926 commit d594034

File tree

4 files changed

+20
-1
lines changed

4 files changed

+20
-1
lines changed

shell/composables/useI18n.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,16 @@ let store: Store<any> | null = null;
1111
* @returns A translated string or the raw value if the raw parameter is set to true.
1212
*/
1313
const t = (key: string, args?: unknown, raw?: boolean): string => {
14-
return stringFor(store, key, args, raw);
14+
if (!store) {
15+
if (!!process.env.dev) {
16+
// eslint-disable-next-line no-console
17+
console.warn('useI18n: store not available');
18+
}
19+
20+
return key;
21+
}
22+
23+
return stringFor(store, key, args as any, raw);
1524
};
1625

1726
export type I18n = { t: typeof t };

shell/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"license": "Apache-2.0",
77
"author": "SUSE",
88
"private": false,
9+
"types": "types/shell/index.d.ts",
910
"engines": {
1011
"node": ">=20.0.0"
1112
},

shell/plugins/i18n.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@ import { escapeHtml } from '../utils/string';
33
import { watchEffect, ref, h } from 'vue';
44
import { useStore } from 'vuex';
55

6+
/**
7+
* @param {import('vuex').Store<any>} store
8+
* @param {string} key
9+
* @param {Record<string, any>} [args]
10+
* @param {boolean} [raw]
11+
* @param {boolean} [escapehtml]
12+
* @returns {string}
13+
*/
614
export function stringFor(store, key, args, raw = false, escapehtml = true) {
715
const translation = store.getters['i18n/t'](key, args);
816

shell/scripts/typegen.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ ${BASE_DIR}/node_modules/.bin/tsc ${SHELL_DIR}/store/prefs.js --declaration --al
2929
${BASE_DIR}/node_modules/.bin/tsc ${SHELL_DIR}/store/plugins.js --declaration --allowJs --emitDeclarationOnly --outDir ${SHELL_DIR}/tmp/store > /dev/null
3030

3131
# # plugins
32+
${BASE_DIR}/node_modules/.bin/tsc ${SHELL_DIR}/plugins/i18n.js --declaration --allowJs --emitDeclarationOnly --outDir ${SHELL_DIR}/tmp/ > /dev/null
3233
${BASE_DIR}/node_modules/.bin/tsc ${SHELL_DIR}/plugins/dashboard-store/normalize.js --declaration --allowJs --emitDeclarationOnly --outDir ${SHELL_DIR}/tmp/plugins/dashboard-store/ > /dev/null
3334
${BASE_DIR}/node_modules/.bin/tsc ${SHELL_DIR}/plugins/dashboard-store/resource-class.js --declaration --allowJs --emitDeclarationOnly --outDir ${SHELL_DIR}/tmp/plugins/dashboard-store/ > /dev/null
3435
${BASE_DIR}/node_modules/.bin/tsc ${SHELL_DIR}/plugins/dashboard-store/classify.js --declaration --allowJs --emitDeclarationOnly --outDir ${SHELL_DIR}/tmp/plugins/dashboard-store/ > /dev/null

0 commit comments

Comments
 (0)