🚀 Socket Launch Week Day 5:Introducing Repository Access Permissions and Custom Roles.Learn more
Sign In

@mooljs/plugin-locale

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@mooljs/plugin-locale - npm Package Compare versions

Comparing version
0.1.3
to
0.2.0
+133
dist/locale.mjs
import { createI18n as u } from "vue-i18n";
import { inject as g } from "vue";
const c = "zh-CN",
i = "mooljs-locale",
l = Symbol("i18n");
class a {
constructor(e) {
(this._config = {
persistent: !0,
fallbackLocale: "en-US",
legacy: !1,
allowComposition: !0,
}),
(this.app = e);
}
static create(e) {
return this._instance || (this._instance = new a(e)), this._instance;
}
/**
* 初始化国际化实例
*/
async initialize(e = {}) {
this._config = { ...this._config, ...e };
const t = await this.loadLocaleModules();
this.setupI18n(t);
}
/**
* 动态加载语言模块
*/
async loadLocaleModules() {
const modules = import.meta.glob("/src/locale/*.ts", { eager: true });
return Object.entries(modules).reduce((t, [o, r]) => {
const n = this.extractLanguageCode(o);
return n && (t[n] = r.default), t;
}, {});
}
/**
* 创建 I18n 实例
*/
setupI18n(e) {
(this._i18n = u({
locale: this.currentLocale,
messages: e,
...this._config,
})),
this.app.use(this._i18n),
this.provideToApp();
}
/**
* 向应用提供实例
*/
provideToApp() {
this.app.provide(l, this._i18n);
}
/**
* 开发工具集成
*/
// private setupDevTools() {
// if (import.meta.env.DEV) {
// this._store.register('@@i18n-debug', () => ({
// locales: this.availableLocales,
// current: this.currentLocale,
// messages: this._i18n.global.messages
// }));
// }
// }
/**
* 获取当前语言
*/
get currentLocale() {
return (this._config.persistent && localStorage.getItem(i)) || c;
}
/**
* 可用语言列表
*/
get availableLocales() {
return Object.keys(this._i18n.global.messages.value);
}
/**
* 更新当前语言
*/
updateLocale(e) {
(this._i18n.global.locale.value = e),
this._config.persistent && localStorage.setItem(i, e);
}
/**
* 增加多语言
*/
addLocale(e, t) {
if (this._i18n.global.messages[e]) {
console.warn(`Locale ${e} already exists.`);
return;
}
this._i18n.global.setLocaleMessage(e, t),
this._config.persistent && localStorage.setItem(i, e);
}
/**
* 获取原生实例
*/
static get instance() {
return this._instance;
}
extractLanguageCode(e) {
var t;
return ((t = e.match(/([^/]+)\.ts$/)) == null ? void 0 : t[1]) || null;
}
}
const p = () => {
const s = g(l);
return {
t: s.global.t,
getLocale: () => s.global.locale,
addLocale: (e, t) => {
a.instance.addLocale(e, t);
},
getAllLocales: () => Object.keys(s.global.messages.value),
setLocale: function (e) {
const t = a.instance;
t.availableLocales.includes(e) && t.updateLocale(e);
},
};
},
d = (s, e) => {
const t = a.create(s);
return t.initialize(e), t;
};
export {
l as LOCALE_INJECTION_KEY,
a as LocaleManager,
d as setupLocale,
p as useLocale,
};
+38
-5

@@ -1,8 +0,41 @@

const tranform = require("@mooljs/cli-service/lib/vitePlugins/tranform");
const { readFileSync, readdirSync } = require('node:fs');
module.exports = (api, options) => {
api.chainVite((config) => {
config.plugins.push(
tranform(api,options),
)
api.applyPlugins((config) => {
// 检查 @mooljs/plugin-layout 插件是否存在,且 @mooljs/plugin-access 插件尚未添加
if (options.locale) {
config.plugins.push(
{
name: '@mooljs/plugin-locale',
after: [],
injectImports: (opt) => {
return [` import { setupLocale } from 'virtual:locale';`];
},
// 运行时逻辑
runtime: (ctx) => `
app.use(setupLocale, ${JSON.stringify(options.locale)});
`,
// 虚拟模块定义
virtualModule: () => ({
id: 'virtual:locale',
content: readFileSync(api.resolve('node_modules/@mooljs/plugin-locale/dist/locale.mjs'), 'utf-8')
}),
injectMool: () => {
return [`export {useLocale} from 'virtual:locale';`]
},
injectModuleType: () => {
const locales = readdirSync(api.resolve('src/locale'))
.filter((file) => file.endsWith(".ts"))
.map((file) => ` "${file.replace(".ts", "")}":string;\n `).join('');
return [
` interface LocaleModule {\n ${locales} \n }\n interface UseLocaleReturn {\n t: (key: string, ...args: any[]) => string;\n getLocale: ()=>Ref<string>;\n addLocale: (lang: string, message: Record<string,any>) => void;\n getAllLocales: () => string[];\n setLocale: (lang: keyof LocaleModule) => void;\n }\n export const useLocale: () => UseLocaleReturn
`
]
}
}
);
}
});
};
{
"name": "@mooljs/plugin-locale",
"version": "0.1.3",
"version": "0.2.0",
"description": "i18n plugin for mool",

@@ -17,3 +17,9 @@ "main": "index.js",

],
"files": [
"dist"
],
"author": "Merlin Hong",
"scripts": {
"build":"vite build"
},
"license": "MIT",

@@ -33,4 +39,6 @@ "bugs": {

"devDependencies": {
"@vue/cli-test-utils": "^5.0.8"
"@vue/cli-test-utils": "^5.0.8",
"vite": "^5.3.4"
}
}
-9
module.exports = (api, { config, lintOn = [] }, rootOptions, invoking) => {
api.extendPackage({
dependencies: {
"vue-i18n": "^11.1.2",
},
});
api.render("./template");
};