Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🌐 chore: translate non-English comments to English in utils/client (#10029)

LobeHub Bot9c0ac419d0bd
diff --git a/packages/utils/src/client/clipboard.ts b/packages/utils/src/client/clipboard.ts
index c66b17d2a42..a5f31b194a6 100644
--- a/packages/utils/src/client/clipboard.ts
+++ b/packages/utils/src/client/clipboard.ts
@@ -16,7 +16,7 @@ const copyUsingFallback = (imageUrl: string) => {
});
});
} catch {
- // 如果 toBlob 或 ClipboardItem 不被支持,使用 data URL
+ // If toBlob or ClipboardItem is not supported, use data URL
const dataURL = canvas.toDataURL('image/png');
const textarea = document.createElement('textarea');
textarea.value = dataURL;
@@ -44,7 +44,7 @@ const copyUsingModernAPI = async (imageUrl: string) => {
};
export const copyImageToClipboard = async (imageUrl: string) => {
- // 检查是否支持现代 Clipboard API
+ // Check if modern Clipboard API is supported
if (navigator.clipboard && 'write' in navigator.clipboard) {
await copyUsingModernAPI(imageUrl);
} else {
diff --git a/packages/utils/src/client/exportFile.ts b/packages/utils/src/client/exportFile.ts
index 05d1f69b8a0..a352c1d5e54 100644
--- a/packages/utils/src/client/exportFile.ts
+++ b/packages/utils/src/client/exportFile.ts
@@ -1,41 +1,41 @@
export const exportFile = (content: string, filename?: string) => {
- // 创建一个 Blob 对象
+ // Create a Blob object
const blob = new Blob([content], { type: 'plain/text' });
- // 创建一个 URL 对象,用于下载
+ // Create a URL object for download
const url = URL.createObjectURL(blob);
- // 创建一个 <a> 元素,设置下载链接和文件名
+ // Create an <a> element, set download link and filename
const a = document.createElement('a');
a.href = url;
a.download = filename || 'file.txt';
- // 触发 <a> 元素的点击事件,开始下载
+ // Trigger the click event of the <a> element to start download
document.body.append(a);
a.click();
- // 下载完成后,清除 URL 对象
+ // After download is complete, clear the URL object
URL.revokeObjectURL(url);
a.remove();
};
export const exportJSONFile = (data: object, fileName: string) => {
- // 创建一个 Blob 对象
+ // Create a Blob object
const blob = new Blob([JSON.stringify(data)], { type: 'application/json' });
- // 创建一个 URL 对象,用于下载
+ // Create a URL object for download
const url = URL.createObjectURL(blob);
- // 创建一个 <a> 元素,设置下载链接和文件名
+ // Create an <a> element, set download link and filename
const a = document.createElement('a');
a.href = url;
a.download = fileName;
- // 触发 <a> 元素的点击事件,开始下载
+ // Trigger the click event of the <a> element to start download
document.body.append(a);
a.click();
- // 下载完成后,清除 URL 对象
+ // After download is complete, clear the URL object
URL.revokeObjectURL(url);
a.remove();
};
diff --git a/packages/utils/src/client/parserPlaceholder.ts b/packages/utils/src/client/parserPlaceholder.ts
index 81e3c7ffedc..8264430a7da 100644
--- a/packages/utils/src/client/parserPlaceholder.ts
+++ b/packages/utils/src/client/parserPlaceholder.ts
@@ -10,7 +10,7 @@ const placeholderVariablesRegex = /{{(.*?)}}/g;
/* eslint-disable sort-keys-fix/sort-keys-fix */
export const VARIABLE_GENERATORS = {
/**
- * 时间类模板变量
+ * Time-related template variables
*
* | Value | Example |
* |-------|---------|
@@ -46,12 +46,12 @@ export const VARIABLE_GENERATORS = {
year: () => new Date().getFullYear().toString(),
/**
- * 用户信息类模板变量
+ * User information template variables
*
* | Value | Example |
* |-------|---------|
* | `{{email}}` | [email protected] |
- * | `{{nickname}}` | 社区版用户 |
+ * | `{{nickname}}` | Community User |
* | `{{username}}` | LobeChat |
*
*/
@@ -63,7 +63,7 @@ export const VARIABLE_GENERATORS = {
'',
/**
- * 随机值类模板变量
+ * Random value template variables
*
* | Value | Example |
* |-------|---------|
@@ -99,7 +99,7 @@ export const VARIABLE_GENERATORS = {
uuid_short: () => uuid().split('-')[0],
/**
- * 平台类模板变量
+ * Platform-related template variables
*
* | Value | Example |
* |-------|---------|
@@ -114,9 +114,9 @@ export const VARIABLE_GENERATORS = {
} as Record<string, () => string>;
/**
- * 从文本中提取所有 {{variable}} 占位符的变量名
- * @param text 包含模板变量的字符串
- * @returns 变量名数组,如 ['date', 'nickname']
+ * Extract all {{variable}} placeholder variable names from text
+ * @param text String containing template variables
+ * @returns Array of variable names, e.g., ['date', 'nickname']
*/
const extractPlaceholderVariables = (text: string): string[] => {
const matches = [...text.matchAll(placeholderVariablesRegex)];
@@ -124,15 +124,15 @@ const extractPlaceholderVariables = (text: string): string[] => {
};
/**
- * 将模板变量替换为实际值,并支持递归解析嵌套变量
- * @param text - 含变量的原始文本
- * @param depth - 递归深度,默认 1,设置更高可支持 {{text}} 中的 {{date}} 等
- * @returns 替换后的文本
+ * Replace template variables with actual values, supporting recursive parsing of nested variables
+ * @param text - Original text containing variables
+ * @param depth - Recursion depth, default 1, set higher to support {{date}} within {{text}} etc.
+ * @returns Replaced text
*/
export const parsePlaceholderVariables = (text: string, depth = 2): string => {
let result = text;
- // 递归解析,用于处理如 {{text}} 存在额外预设变量
+ // Recursive parsing to handle cases where {{text}} contains additional preset variables
for (let i = 0; i < depth; i++) {
try {
const variables = Object.fromEntries(
@@ -154,9 +154,9 @@ export const parsePlaceholderVariables = (text: string, depth = 2): string => {
};
/**
- * 解析消息内容,替换占位符变量
- * @param messages 原始消息数组
- * @returns 处理后的消息数组
+ * Parse message content, replace placeholder variables
+ * @param messages Original message array
+ * @returns Processed message array
*/
export const parsePlaceholderVariablesMessages = (messages: any[]): any[] =>
messages.map((message) => {
@@ -164,12 +164,12 @@ export const parsePlaceholderVariablesMessages = (messages: any[]): any[] =>
const { content } = message;
- // 字符串类型直接处理
+ // Process string type directly
if (typeof content === 'string') {
return { ...message, content: parsePlaceholderVariables(content) };
}
- // 数组类型处理其中的 text 元素
+ // Process text elements in array type
if (Array.isArray(content)) {
return {
...message,
diff --git a/packages/utils/src/client/topic.ts b/packages/utils/src/client/topic.ts
index bfe9b873cba..c1a7b328911 100644
--- a/packages/utils/src/client/topic.ts
+++ b/packages/utils/src/client/topic.ts
@@ -3,7 +3,7 @@ import dayjs from 'dayjs';
import isToday from 'dayjs/plugin/isToday';
import isYesterday from 'dayjs/plugin/isYesterday';
-// 初始化 dayjs 插件
+// Initialize dayjs plugins
dayjs.extend(isToday);
dayjs.extend(isYesterday);
@@ -19,32 +19,32 @@ const getTopicGroupId = (timestamp: number): TimeGroupId => {
return 'yesterday';
}
- // 7天内(不包括今天和昨天)
+ // Within 7 days (excluding today and yesterday)
const weekAgo = now.subtract(7, 'day');
if (date.isAfter(weekAgo) && !date.isToday() && !date.isYesterday()) {
return 'week';
}
- // 当前月份(不包括前面已经分组的日期)
- // 使用原生的月份和年份比较
+ // Current month (excluding dates already grouped above)
+ // Use native month and year comparison
if (date.month() === now.month() && date.year() === now.year()) {
return 'month';
}
- // 当年的其他月份
+ // Other months of the current year
if (date.year() === now.year()) {
return `${date.year()}-${(date.month() + 1).toString().padStart(2, '0')}`;
}
- // 更早的年份
+ // Earlier years
return `${date.year()}`;
};
-// 确保分组的排序
+// Ensure group sorting
const sortGroups = (groups: GroupedTopic[]): GroupedTopic[] => {
const orderMap = new Map<string, number>();
- // 设置固定分组的顺序
+ // Set the order of fixed groups
orderMap.set('today', 0);
orderMap.set('yesterday', 1);
orderMap.set('week', 2);
@@ -58,12 +58,12 @@ const sortGroups = (groups: GroupedTopic[]): GroupedTopic[] => {
return orderA - orderB;
}
- // 对于年月格式和年份格式的分组,按时间倒序排序
+ // For year-month and year format groups, sort in descending chronological order
return b.id.localeCompare(a.id);
});
};
-// 时间分组的具体实现
+// Specific implementation of time grouping
export const groupTopicsByTime = (topics: ChatTopic[]): GroupedTopic[] => {
if (!topics.length) return [];
diff --git a/packages/utils/src/client/xor-obfuscation.ts b/packages/utils/src/client/xor-obfuscation.ts
index 26f5fdf0ca7..8eca7791bf2 100644
--- a/packages/utils/src/client/xor-obfuscation.ts
+++ b/packages/utils/src/client/xor-obfuscation.ts
@@ -1,30 +1,30 @@
import { SECRET_XOR_KEY } from '@/const/auth';
/**
- * 将字符串转换为 Uint8Array (UTF-8 编码)
+ * Convert string to Uint8Array (UTF-8 encoding)
*/
const stringToUint8Array = (str: string): Uint8Array => {
return new TextEncoder().encode(str);
};
/**
- * 对 Uint8Array 进行 XOR 运算
- * @param data 要处理的 Uint8Array
- * @param key 用于 XOR 的密钥 (Uint8Array)
- * @returns 经过 XOR 运算的 Uint8Array
+ * Perform XOR operation on Uint8Array
+ * @param data The Uint8Array to process
+ * @param key The key used for XOR operation (Uint8Array)
+ * @returns The Uint8Array after XOR operation
*/
const xorProcess = (data: Uint8Array, key: Uint8Array): Uint8Array => {
const result = new Uint8Array(data.length);
for (const [i, datum] of data.entries()) {
- result[i] = datum ^ key[i % key.length]; // 密钥循环使用
+ result[i] = datum ^ key[i % key.length]; // Key is used cyclically
}
return result;
};
/**
- * 对 payload 进行 XOR 混淆并 Base64 编码
- * @param payload 要混淆的 JSON 对象
- * @returns Base64 编码后的混淆字符串
+ * Obfuscate payload with XOR and encode to Base64
+ * @param payload The JSON object to obfuscate
+ * @returns The obfuscated string encoded in Base64
*/
export const obfuscatePayloadWithXOR = <T>(payload: T): string => {
const jsonString = JSON.stringify(payload);
@@ -33,7 +33,7 @@ export const obfuscatePayloadWithXOR = <T>(payload: T): string => {
const xoredBytes = xorProcess(dataBytes, keyBytes);
- // 将 Uint8Array 转换为 Base64 字符串
- // 浏览器环境 btoa 只能处理 Latin-1 字符,所以需要先转换为适合 btoa 的字符串
+ // Convert Uint8Array to Base64 string
+ // In browser environment, btoa can only handle Latin-1 characters, so we need to convert to a format suitable for btoa first
return btoa(String.fromCharCode(...xoredBytes));
};