Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

chore(locale): init locale, copy

Dushusir3779bd1a63b7
diff --git a/.github/contributing-zh.md b/.github/contributing-zh.md
index a1d9761be5..3500e5d4ea 100644
--- a/.github/contributing-zh.md
+++ b/.github/contributing-zh.md
@@ -590,6 +590,10 @@ context.getWorkBook().setPluginMeta < IKeyType < IBandedRange >> (ALTERNATING_CO
- `context.getLocale().get('key')`来取得 key 对应的翻译即可,语言不用特定获取,因为在初始化的时候就配置好了
+1. 语言分开加载:语言包根据用户配置采用异步import的模式加载,避免初始化时一次性加载所有语言包;切换语言时也是异步import加载目标语言包
+2. 移动端:移动端在有需要的情况下,分开key值来取得翻译。比如PC 取得'btn',移动端取得'btn-mobile'
+3. 如何安装:只需在Univer初始化的时候,配置语言即可,插件中会根据配置异步加载对应的语言包
+
### CSS 模块
我们内部使用 `less + CSS module` 模式来编写样式,一个`.module.less`结尾的 css 文件会被识别为 CSS Module,如果您不需要 CSS Module 功能,可以仅仅使用`.less`结尾。
diff --git a/examples/univer-sheet-ts/scripts/common.ts b/examples/univer-sheet-ts/scripts/common.ts
index 2349faf978..c468bd3ab9 100644
--- a/examples/univer-sheet-ts/scripts/common.ts
+++ b/examples/univer-sheet-ts/scripts/common.ts
@@ -1,7 +1,7 @@
// scripts/common.ts
import { BuildOptions } from "esbuild";
import stylePlugin from 'esbuild-style-plugin'
-import { resolve } from "path";
+import path, { resolve } from "path";
import { existsSync, lstatSync } from "fs";
const root = process.cwd();
export const paths = {
diff --git a/examples/univer-sheet-ts/src/main.tsx b/examples/univer-sheet-ts/src/main.tsx
index bad0ee124f..3603913669 100644
--- a/examples/univer-sheet-ts/src/main.tsx
+++ b/examples/univer-sheet-ts/src/main.tsx
@@ -1,4 +1,4 @@
-import { UniverSheet, Univer } from '@univerjs/core';
+import { UniverSheet, Univer, LocaleType } from '@univerjs/core';
import { RenderEngine } from '@univerjs/base-render';
import { SheetPlugin } from '@univerjs/base-sheets';
import { SheetUIPlugin } from '@univerjs/ui-plugin-sheets';
@@ -31,7 +31,9 @@ const sheetUIConfig = {
};
// univer
-const univer = new Univer();
+const univer = new Univer({
+ locale: LocaleType.EN,
+});
// base-render
univer.install(new RenderEngine());
diff --git a/examples/univer-sheet-ts/vite.config.ts b/examples/univer-sheet-ts/vite.config.ts
index 6ea3c75dab..13a5493b98 100644
--- a/examples/univer-sheet-ts/vite.config.ts
+++ b/examples/univer-sheet-ts/vite.config.ts
@@ -10,9 +10,9 @@ const resolve = (url: string) => path.resolve(__dirname, url);
export default defineConfig({
build: {
lib: {
- entry: resolve('src/index.ts'),
+ entry: resolve('src/main.tsx'),
name: 'UniverSheetTs',
- formats: ['es', 'cjs', 'umd', 'iife'],
+ formats: ['es'],
fileName: 'univer-sheet-ts',
},
outDir: './lib',
diff --git a/package.json b/package.json
index 78cec76875..8bb59a878f 100644
--- a/package.json
+++ b/package.json
@@ -18,6 +18,7 @@
"lighthouse-dev": "start /B node scripts/dev && lighthouse http://127.0.0.1:3103 --output html --output-path ./report.html",
"build": "node scripts/build.js",
"build:sheet": "pnpm run --filter univer-sheet-ts build:demo",
+ "build:sheet-esbuild": "pnpm run --filter univer-sheet-ts build:esbuild",
"build:doc": "pnpm run --filter univer-doc-ts build:demo",
"build:slide": "pnpm run --filter univer-slide-ts build:demo",
"link": "node scripts/link.js",
diff --git a/packages/base-ui/src/Basics/Interfaces/ICopyData.ts b/packages/base-ui/src/Basics/Interfaces/ICopyData.ts
index 29dcaf7dce..94aae1ffe3 100644
--- a/packages/base-ui/src/Basics/Interfaces/ICopyData.ts
+++ b/packages/base-ui/src/Basics/Interfaces/ICopyData.ts
@@ -17,7 +17,7 @@ export interface ICopyData {
*
* property: image / filter ..., use plugin name
* e.g
- * <table data-image="[{left:0,right:0,width:100,height:100,url:''}]"
+ * <table data-image="[{left:0,right:0,width:100,height:100,url:''}] data-filter"
*/
tag: string;
diff --git a/packages/base-ui/src/Basics/Register/Copy/CopyExtensionManager.ts b/packages/base-ui/src/Basics/Register/Copy/CopyExtensionManager.ts
index 5050a88bc8..75ca6ec808 100644
--- a/packages/base-ui/src/Basics/Register/Copy/CopyExtensionManager.ts
+++ b/packages/base-ui/src/Basics/Register/Copy/CopyExtensionManager.ts
@@ -90,8 +90,6 @@ export class CopyExtensionManager {
let insertIndex = table.indexOf('<table') + '<table'.length; // 插入位置
let result = table.slice(0, insertIndex) + propertyStr + table.slice(insertIndex); // 插入新字符串
- console.info('handleTableData==》》===', result);
-
this._copyResolver(result);
}
}
diff --git a/packages/core/src/Basics/Context.ts b/packages/core/src/Basics/Context.ts
index 721e3a974a..69bc31b903 100644
--- a/packages/core/src/Basics/Context.ts
+++ b/packages/core/src/Basics/Context.ts
@@ -14,7 +14,9 @@ export class Context extends ContextBase {
super();
this._locale = new Locale();
- this._locale.initialize();
+
+ // Initialize internationalization
+ this._locale.initialize(univerData.locale);
if (univerData.id == null || univerData.id.length === 0) {
this._univerId = Tools.generateRandomId(10);
diff --git a/packages/core/src/Command/Command.ts b/packages/core/src/Command/Command.ts
index aeb5fe68fa..edb56be9d1 100644
--- a/packages/core/src/Command/Command.ts
+++ b/packages/core/src/Command/Command.ts
@@ -93,7 +93,6 @@ export class Command {
}
invoke(): void {
- console.info('invoke')
CommandManager.getCommandInjectorObservers().notifyObservers(
this.getInjector()
);
diff --git a/packages/core/src/Shared/Locale.ts b/packages/core/src/Shared/Locale.ts
index e54b691784..062be70871 100644
--- a/packages/core/src/Shared/Locale.ts
+++ b/packages/core/src/Shared/Locale.ts
@@ -1,7 +1,11 @@
import { Tools } from '.';
import { LocaleType } from '../Enum';
-import { Nullable } from './Types';
+import { IKeyValue, Nullable } from './Types';
+export type ILocales = {
+ en: IKeyValue;
+ zh: IKeyValue;
+};
/**
* The data structure stored by the Locale class
*/
@@ -32,7 +36,7 @@ export type ILocaleOptions = {
* }
* ```
*/
- locales: object;
+ locales: ILocales;
};
/**
@@ -62,7 +66,10 @@ export class Locale {
*
* @private
*/
- private static getValue(locale: object, key: string): Nullable<string | object> {
+ private static getValue(
+ locale: IKeyValue,
+ key: string
+ ): Nullable<string | object> {
try {
return locale[key]
? locale[key]
@@ -74,11 +81,7 @@ export class Locale {
initialize(locale: Nullable<LocaleType>) {
this.options = {
- // use config first, or get language setting from browser
- currentLocale:
- locale || ['zh', 'zh-CN'].includes(Tools.getLanguage())
- ? LocaleType.ZH
- : LocaleType.EN,
+ currentLocale: locale || LocaleType.EN,
locales: { zh, en },
};
}
@@ -120,7 +123,7 @@ export class Locale {
* @returns void
*
*/
- load(locales: object): void {
+ load(locales: Partial<ILocales>): void {
Tools.deepMerge(this.options.locales, locales);
}
diff --git a/packages/sheets-plugin-operation/src/Basics/Register/CopyImageExtension.ts b/packages/sheets-plugin-operation/src/Basics/Register/CopyImageExtension.ts
index f3462e7161..52bea26d72 100644
--- a/packages/sheets-plugin-operation/src/Basics/Register/CopyImageExtension.ts
+++ b/packages/sheets-plugin-operation/src/Basics/Register/CopyImageExtension.ts
@@ -29,6 +29,7 @@ export class CopyImageExtensionFactory extends BaseCopyExtensionFactory<Operatio
if (embed) {
data.key = 'property';
data.tag = 'image';
+ //TODO <table data-image="[]" embed="false">
return this.create(data);
}
diff --git a/packages/sheets-plugin-operation/src/Basics/Register/CutExtension.ts b/packages/sheets-plugin-operation/src/Basics/Register/CutExtension.ts
index 4a9d641581..abdf89cf7d 100644
--- a/packages/sheets-plugin-operation/src/Basics/Register/CutExtension.ts
+++ b/packages/sheets-plugin-operation/src/Basics/Register/CutExtension.ts
@@ -18,7 +18,7 @@ export class CutExtension extends BaseCutExtension<OperationPlugin> {
let colInfo;
let rowInfo;
if (content) {
- // TODO move to CutOfficeExtension
+ // TODO move to CutOfficeExtension
if (content.indexOf('xmlns:x="urn:schemas-microsoft-com:office:excel"') > -1) {
data = handelExcelToJson(content);
colInfo = handleTableColgroup(content);
@@ -38,7 +38,7 @@ export class CutExtension extends BaseCutExtension<OperationPlugin> {
rowInfo,
});
- return actionDataList
+ return actionDataList;
}
}
diff --git a/packages/sheets-plugin-operation/src/Basics/Register/PasteImageExtension.ts b/packages/sheets-plugin-operation/src/Basics/Register/PasteImageExtension.ts
index 0e33f63646..e22b16154c 100644
--- a/packages/sheets-plugin-operation/src/Basics/Register/PasteImageExtension.ts
+++ b/packages/sheets-plugin-operation/src/Basics/Register/PasteImageExtension.ts
@@ -6,8 +6,6 @@ export class PasteImageExtension extends BasePasteExtension<OperationPlugin> {
execute() {
let content = this._data.html || this._data.plain;
- console.info('image content', content);
-
return [];
}
}
@@ -25,6 +23,8 @@ export class PasteImageExtensionFactory extends BasePasteExtensionFactory<Operat
const content = data.html || data.plain;
// TODO 识别univerId,sheetId,rangeData中的范围是否包含image信息,有则直接从本地取
+ // TODO office 来的图片
+ // TODO: embed从manager解析而来
if (content && content.indexOf('data-image') > -1) {
return this.create(data);
}
diff --git a/packages/ui-plugin-sheets/src/SheetUIPlugin.ts b/packages/ui-plugin-sheets/src/SheetUIPlugin.ts
index 9d46ff0022..751ad74b76 100644
--- a/packages/ui-plugin-sheets/src/SheetUIPlugin.ts
+++ b/packages/ui-plugin-sheets/src/SheetUIPlugin.ts
@@ -1,12 +1,12 @@
-import { Plugin, Tools, PLUGIN_NAMES, Context, Univer } from '@univerjs/core';
+import { Plugin, Tools, PLUGIN_NAMES, Context, Univer, IKeyValue, LocaleType } from '@univerjs/core';
import { ComponentManager, getRefElement, RegisterManager, KeyboardManager } from '@univerjs/base-ui';
import { RenderEngine } from '@univerjs/base-render';
import { DefaultSheetUIConfig, installObserver, ISheetUIPluginConfig, SheetUIPluginObserve, SHEET_UI_PLUGIN_NAME } from './Basics';
-import { zh, en } from './Locale';
import { AppUIController } from './Controller/AppUIController';
import { Fx } from './View/FormulaBar';
import { SlotComponentProps } from './Controller/SlotController';
import { IToolbarItemProps } from './Controller';
+// import {zh,en} from './Locale';
export class SheetUIPlugin extends Plugin<SheetUIPluginObserve, Context> {
private _appUIController: AppUIController;
@@ -37,10 +37,37 @@ export class SheetUIPlugin extends Plugin<SheetUIPluginObserve, Context> {
/**
* load more Locale object
*/
- this.getLocale().load({
- zh,
- en,
- });
+ const locale = this.getGlobalContext().getLocale().getCurrentLocale();
+ console.info(`./Locale/${locale}`);
+ if (locale === LocaleType.ZH) {
+ import(`./Locale/zh`).then((module) => {
+ this.loadLocale(locale, module);
+ });
+ } else if (locale === LocaleType.EN) {
+ import(`./Locale/en`).then((module) => {
+ this.loadLocale(locale, module);
+ });
+ }
+
+ // this._componentManager = new ComponentManager();
+ // this._keyboardManager = new KeyboardManager(this);
+ // this._registerManager = new RegisterManager(this);
+ // this._appUIController = new AppUIController(this);
+ // // AppUIController initializes the DOM as an asynchronous rendering process, and must wait for the UI rendering to complete before starting to render the canvas
+ // this.UIDidMount(() => {
+ // this.initRender();
+ // });
+ }
+
+ loadLocale(locale: LocaleType, module: IKeyValue) {
+ // import(`./Locale/${locale}`).then((module) => {
+ // Do something with the module.
+ const localObject: IKeyValue = {};
+ localObject[locale] = module.default;
+
+ console.log('localObject===', locale, localObject);
+
+ this.getLocale().load(localObject);
this._componentManager = new ComponentManager();
this._keyboardManager = new KeyboardManager(this);