mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-17 17:36:08 +08:00
core: i18n: better performance
This commit is contained in:
5 files changed
+10
-27
No files matched your search
@@ -131,6 +131,7 @@ Hydro 用户群:1085853538
|
||||
- core: Settings: 支持使用 Schemastery
|
||||
- ui: 更新系统设置页面样式
|
||||
- import-qduoj: 修复 spj=null
|
||||
- core: 修复文件复制
|
||||
|
||||
### Breaking API Changes
|
||||
|
||||
@@ -141,6 +142,7 @@ Hydro 用户群:1085853538
|
||||
- core: 移除 global.Hydro.service
|
||||
- core: 移除 global.Hydro.ui.template
|
||||
- core: 移除 global.Hydro.lib
|
||||
- core: 移除 String.prototype.translate (使用 app.i18n.translate)
|
||||
|
||||
## Hydro 4.19.0 / UI 4.57.0
|
||||
|
||||
|
||||
@@ -22,7 +22,7 @@ export default class GeoIPService extends Service {
|
||||
provider = '<a href="http://www.maxmind.com" target="_blank">MaxMind</a>';
|
||||
lookup(ip: string, locale: string): Result {
|
||||
const res: any = reader.get(ip);
|
||||
if (!res) return { display: 'Unknown address'.translate(locale) };
|
||||
if (!res) return { display: this.ctx.i18n.translate('Unknown address', [locale]) };
|
||||
const ret: Result = { display: '' };
|
||||
if (res.location) ret.location = res.location;
|
||||
if (res.continent) ret.continent = res.continent.names[locale] || res.continent.names.en;
|
||||
|
||||
@@ -12,11 +12,6 @@ declare module '../service/bus' {
|
||||
'app/i18n/update': (lang: string) => void;
|
||||
}
|
||||
}
|
||||
declare global {
|
||||
interface String {
|
||||
translate: (...languages: string[]) => string;
|
||||
}
|
||||
}
|
||||
|
||||
class I18nService extends Service {
|
||||
constructor(ctx: Context) {
|
||||
@@ -47,14 +42,14 @@ class I18nService extends Service {
|
||||
if (languages[0]?.startsWith('en')) {
|
||||
// For most use cases, source text equals to translated text in English.
|
||||
// So if it doesn't exist, we should use the original text instead of fallback.
|
||||
return app.i18n.get(str, languages[0]) || app.i18n.get(str, 'en') || this.toString();
|
||||
return this.get(str, languages[0]) || this.get(str, 'en') || this.toString();
|
||||
}
|
||||
for (const language of languages.filter(Boolean)) {
|
||||
const curr = app.i18n.get(str, language) || app.i18n.get(str, language.split('_')[0])
|
||||
|| app.i18n.get(str, language.split('-')[0]);
|
||||
const curr = this.get(str, language) || this.get(str, language.split('_')[0])
|
||||
|| this.get(str, language.split('-')[0]);
|
||||
if (curr) return curr;
|
||||
}
|
||||
return this.toString();
|
||||
return str.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,20 +59,6 @@ export function apply(ctx: Context) {
|
||||
ctx.i18n = new I18nService(ctx);
|
||||
}
|
||||
|
||||
String.prototype.translate = function translate(...languages: string[]) {
|
||||
if (languages[0]?.startsWith('en')) {
|
||||
// For most use cases, source text equals to translated text in English.
|
||||
// So if it doesn't exist, we should use the original text instead of fallback.
|
||||
return app.i18n.get(this, languages[0]) || app.i18n.get(this, 'en') || this.toString();
|
||||
}
|
||||
for (const language of languages.filter(Boolean)) {
|
||||
const curr = app.i18n.get(this, language) || app.i18n.get(this, language.split('_')[0])
|
||||
|| app.i18n.get(this, language.split('-')[0]);
|
||||
if (curr) return curr;
|
||||
}
|
||||
return this.toString();
|
||||
};
|
||||
|
||||
function collect(lang: string) {
|
||||
const s = translations[lang];
|
||||
if (!(s instanceof Array)) return {};
|
||||
|
||||
@@ -80,7 +80,7 @@ class MessageModel {
|
||||
const targets = await user.getMulti({ priv: { $bitsAllSet: PRIV.PRIV_VIEW_SYSTEM_NOTIFICATION } })
|
||||
.project({ _id: 1, viewLang: 1 }).toArray();
|
||||
return Promise.all(targets.map(({ _id, viewLang }) => {
|
||||
const msg = message.translate(viewLang || system.get('server.language')).format(...args);
|
||||
const msg = app.i18n.translate(message, [viewLang || system.get('server.language')]).format(...args);
|
||||
return MessageModel.send(1, _id, msg, MessageModel.FLAG_RICHTEXT);
|
||||
}));
|
||||
}
|
||||
|
||||
@@ -160,8 +160,8 @@ export async function apply(ctx: Context) {
|
||||
if (!str) return '';
|
||||
const lang = this.user?.viewLang || this.session?.viewLang;
|
||||
const res = lang
|
||||
? str.toString().translate(lang, ...this.context.acceptsLanguages())
|
||||
: str.toString().translate(...this.context.acceptsLanguages(), system.get('server.language'));
|
||||
? ctx.i18n.translate(str.toString(), [lang, ...this.context.acceptsLanguages()])
|
||||
: ctx.i18n.translate(str.toString(), [...this.context.acceptsLanguages(), system.get('server.language')]);
|
||||
return res;
|
||||
},
|
||||
paginate<T>(cursor: FindCursor<T>, page: number, key: string | number) {
|
||||
|
||||
Reference in new issue
Block a user