mirror of
https://github.com/misskey-dev/misskey.git
synced 2026-06-05 12:54:09 +02:00
Follow up per locale bundle (#16381)
* fix docker build * enable check spdx license id in frontend-builder * fix eslint config * run eslint for frontend-builder in ci * fix eslint * add license headers * fix unnecessary comments * update changelog * fix generateDts * fix tsx
This commit is contained in:
@@ -1,6 +1,11 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: syuilo and misskey-project
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
import MagicString from 'magic-string';
|
||||
import type { Locale } from '../../../locales/index.js';
|
||||
import { assertNever } from '../utils.js';
|
||||
import type { Locale, ILocale } from '../../../locales/index.js';
|
||||
import type { TextModification } from '../locale-inliner.js';
|
||||
import type { Logger } from '../logger.js';
|
||||
|
||||
@@ -13,16 +18,16 @@ export function applyWithLocale(
|
||||
) {
|
||||
for (const modification of modifications) {
|
||||
switch (modification.type) {
|
||||
case "delete":
|
||||
case 'delete':
|
||||
sourceCode.remove(modification.begin, modification.end);
|
||||
break;
|
||||
case "insert":
|
||||
case 'insert':
|
||||
sourceCode.appendRight(modification.begin, modification.text);
|
||||
break;
|
||||
case "replace":
|
||||
case 'replace':
|
||||
sourceCode.update(modification.begin, modification.end, modification.text);
|
||||
break;
|
||||
case "localized": {
|
||||
case 'localized': {
|
||||
const accessed = getPropertyByPath(localeJson, modification.localizationKey);
|
||||
if (accessed == null) {
|
||||
fileLogger.warn(`Cannot find localization key ${modification.localizationKey.join('.')}`);
|
||||
@@ -30,7 +35,7 @@ export function applyWithLocale(
|
||||
sourceCode.update(modification.begin, modification.end, JSON.stringify(accessed));
|
||||
break;
|
||||
}
|
||||
case "parameterized-function": {
|
||||
case 'parameterized-function': {
|
||||
const accessed = getPropertyByPath(localeJson, modification.localizationKey);
|
||||
let replacement: string;
|
||||
if (typeof accessed === 'string') {
|
||||
@@ -44,33 +49,33 @@ export function applyWithLocale(
|
||||
sourceCode.update(modification.begin, modification.end, replacement);
|
||||
break;
|
||||
|
||||
function formatFunction(accessed: string): string {
|
||||
function formatFunction(format: string): string {
|
||||
const params = new Set<string>();
|
||||
const components: string[] = [];
|
||||
let lastIndex = 0;
|
||||
for (const match of accessed.matchAll(/\{(.+?)}/g)) {
|
||||
for (const match of format.matchAll(/\{(.+?)}/g)) {
|
||||
const [fullMatch, paramName] = match;
|
||||
if (lastIndex < match.index) {
|
||||
components.push(JSON.stringify(accessed.slice(lastIndex, match.index)));
|
||||
components.push(JSON.stringify(format.slice(lastIndex, match.index)));
|
||||
}
|
||||
params.add(paramName);
|
||||
components.push(paramName);
|
||||
lastIndex = match.index + fullMatch.length;
|
||||
}
|
||||
components.push(JSON.stringify(accessed.slice(lastIndex)));
|
||||
components.push(JSON.stringify(format.slice(lastIndex)));
|
||||
|
||||
// we replace with `(({name,count})=>(name+count+"some"))`
|
||||
const paramList = Array.from(params).join(',');
|
||||
let body = components.filter(x => x != '""').join('+');
|
||||
if (body == '') body = '""'; // if the body is empty, we return empty string
|
||||
let body = components.filter(x => x !== '""').join('+');
|
||||
if (body === '') body = '""'; // if the body is empty, we return empty string
|
||||
return `(({${paramList}})=>(${body}))`;
|
||||
}
|
||||
}
|
||||
case "locale-name": {
|
||||
case 'locale-name': {
|
||||
sourceCode.update(modification.begin, modification.end, modification.literal ? JSON.stringify(localeName) : localeName);
|
||||
break;
|
||||
}
|
||||
case "locale-json": {
|
||||
case 'locale-json': {
|
||||
// locale-json is inlined to place where initialize module-level variable which is executed only once.
|
||||
// In such case we can use JSON.parse to speed up the parsing script.
|
||||
// https://v8.dev/blog/cost-of-javascript-2019#json
|
||||
@@ -84,14 +89,14 @@ export function applyWithLocale(
|
||||
}
|
||||
}
|
||||
|
||||
function getPropertyByPath(localeJson: any, localizationKey: string[]): string | object | null {
|
||||
function getPropertyByPath(localeJson: ILocale, localizationKey: string[]): string | object | null {
|
||||
if (localizationKey.length === 0) return localeJson;
|
||||
let current: any = localeJson;
|
||||
let current: ILocale | string = localeJson;
|
||||
for (const key of localizationKey) {
|
||||
if (typeof current !== 'object' || current === null || !(key in current)) {
|
||||
if (typeof current !== 'object' || !(key in current)) {
|
||||
return null; // Key not found
|
||||
}
|
||||
current = current[key];
|
||||
}
|
||||
return current ?? null;
|
||||
return current;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user