mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-19 15:33:12 +09:00
Show localised names of mod settings in the web UI (#1181)
The mod pack page only looked up mod-setting-name.<name>, so settings with a localised_name showed the internal name, and rich text tags in the locale showed up as raw text. Resolve localised_name and localised_description from the exported settings prototypes against the exported locale, falling back to the mod-setting-name/mod-setting-description keys and then the internal name. The resolver handles parameters, the "" and "?" keys and __ITEM__/__ENTITY__/__TILE__/__FLUID__ references. Rich text formatting tags are removed and prototype icons are replaced with their name. Fixes #768 Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
c6867de13d
commit
6d9a32d28b
5 files changed
+290
-4
No files matched your search
@@ -23,6 +23,7 @@ import { useMods } from "../model/mods";
|
||||
import { useClipboard } from "../util/clipboard";
|
||||
import { modSettingFields, ModSettingField, ModSettingScope } from "../util/mod_settings";
|
||||
import notify, { notifyErrorHandler } from "../util/notify";
|
||||
import { modSettingDescription, modSettingName, modSettingValueName } from "../util/locale";
|
||||
import useResourceState from "../util/useResourceState";
|
||||
import PageHeader from "./PageHeader";
|
||||
import PageLayout from "./PageLayout";
|
||||
@@ -901,6 +902,8 @@ type SettingsTableFieldProps = {
|
||||
type: string;
|
||||
scope: ModSettingScope;
|
||||
allowedValues: any;
|
||||
localisedName?: lib.FactorioLocalisedString;
|
||||
localisedDescription?: lib.FactorioLocalisedString;
|
||||
locale: Map<string, any>;
|
||||
changed: boolean;
|
||||
/** Saved value of the setting, used when the field is created and when the form is reset */
|
||||
@@ -917,6 +920,7 @@ const SettingsTableField = memo((props: SettingsTableFieldProps) => {
|
||||
const isColor = props.type === "color-setting";
|
||||
const isNumber = ["int-setting", "double-setting"].includes(props.type);
|
||||
const isSelect = Boolean(props.allowedValues);
|
||||
const prototype = { name, localised_name: props.localisedName, localised_description: props.localisedDescription };
|
||||
let input;
|
||||
if (isBoolean) {
|
||||
input = <Checkbox />;
|
||||
@@ -924,22 +928,22 @@ const SettingsTableField = memo((props: SettingsTableFieldProps) => {
|
||||
const PartialInputColor = InputColor as () => React.JSX.Element;
|
||||
input = <PartialInputColor />;
|
||||
} else if (isSelect) {
|
||||
const options = Object.values(props.allowedValues).map(value => ({
|
||||
const options = Object.values(props.allowedValues as (string | number)[]).map(value => ({
|
||||
value,
|
||||
label: props.locale.get(`string-mod-setting.${name}-${value}`) || value,
|
||||
label: modSettingValueName(props.locale, name, value),
|
||||
}));
|
||||
input = <Select options={options} />;
|
||||
} else {
|
||||
input = <Input />;
|
||||
}
|
||||
|
||||
const label = props.locale.get(`mod-setting-name.${name}`) || name;
|
||||
const label = modSettingName(props.locale, prototype);
|
||||
const change: ModChange = {
|
||||
type: "settings.delete", scope: props.scope, name, value: { value: props.initialValue! },
|
||||
};
|
||||
return <Form.Item
|
||||
label={props.removed ? <Text delete>{label}</Text> : label}
|
||||
tooltip={props.locale.get(`mod-setting-description.${name}`)}
|
||||
tooltip={modSettingDescription(props.locale, prototype)}
|
||||
labelCol={{ span: 8 }}
|
||||
wrapperCol={{ span: 16 }}
|
||||
className={props.changed ? "changed" : ""}
|
||||
@@ -999,6 +1003,8 @@ function SettingsTable(props: SettingsTableProps) {
|
||||
type={setting.type}
|
||||
scope={scope}
|
||||
allowedValues={setting.allowed_values}
|
||||
localisedName={setting.localised_name}
|
||||
localisedDescription={setting.localised_description}
|
||||
locale={props.locale}
|
||||
changed={hasChange(props.changes, { type: "settings.set", scope, name: setting.name })}
|
||||
initialValue={props.savedModPack.settings[scope].get(setting.name)?.value}
|
||||
|
||||
@@ -0,0 +1,160 @@
|
||||
import type { FactorioLocalisedString } from "@clusterio/lib";
|
||||
|
||||
type Locale = Map<string, string>;
|
||||
|
||||
const maxDepth = 20;
|
||||
const prototypeReferences: Record<string, string> = {
|
||||
ENTITY: "entity-name",
|
||||
ITEM: "item-name",
|
||||
TILE: "tile-name",
|
||||
FLUID: "fluid-name",
|
||||
};
|
||||
|
||||
function substitute(locale: Locale, template: string, parameters: string[]) {
|
||||
return template.replace(/__([A-Z_]+)__([^_\s][^\s]*?)__|__(\d+)__/g, (match, kind, name, index) => {
|
||||
if (index !== undefined) {
|
||||
return parameters[Number(index) - 1] ?? match;
|
||||
}
|
||||
const category = prototypeReferences[kind];
|
||||
return category ? locale.get(`${category}.${name}`) ?? name : match;
|
||||
});
|
||||
}
|
||||
|
||||
function resolve(locale: Locale, value: unknown, depth: number): string | undefined {
|
||||
if (typeof value === "string") {
|
||||
return value;
|
||||
}
|
||||
if (typeof value === "number" || typeof value === "boolean") {
|
||||
return String(value);
|
||||
}
|
||||
if (!Array.isArray(value) || typeof value[0] !== "string" || depth > maxDepth) {
|
||||
return undefined;
|
||||
}
|
||||
const [key, ...rest] = value;
|
||||
if (key === "?") {
|
||||
for (const alternative of rest) {
|
||||
const result = resolve(locale, alternative, depth + 1);
|
||||
if (result !== undefined) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
const parameters: string[] = [];
|
||||
for (const parameter of rest) {
|
||||
const result = resolve(locale, parameter, depth + 1);
|
||||
if (result === undefined) {
|
||||
return undefined;
|
||||
}
|
||||
parameters.push(result);
|
||||
}
|
||||
if (key === "") {
|
||||
return parameters.join("");
|
||||
}
|
||||
const template = locale.get(key);
|
||||
return template === undefined ? undefined : substitute(locale, template, parameters);
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve a Factorio LocalisedString against exported locale.
|
||||
*
|
||||
* @param locale - Flattened locale from the mod pack export.
|
||||
* @param value - Localised string to resolve.
|
||||
* @returns the resolved text, or undefined if a key used is missing from the locale.
|
||||
* @see https://lua-api.factorio.com/stable/types/LocalisedString.html
|
||||
*/
|
||||
export function resolveLocalisedString(locale: Locale, value: FactorioLocalisedString) {
|
||||
return resolve(locale, value, 0);
|
||||
}
|
||||
|
||||
const iconTags: Record<string, string> = {
|
||||
"item": "item-name",
|
||||
"entity": "entity-name",
|
||||
"fluid": "fluid-name",
|
||||
"tile": "tile-name",
|
||||
"recipe": "recipe-name",
|
||||
"technology": "technology-name",
|
||||
"item-group": "item-group-name",
|
||||
"virtual-signal": "virtual-signal-name",
|
||||
"space-location": "space-location-name",
|
||||
"planet": "space-location-name",
|
||||
"quality": "quality-name",
|
||||
};
|
||||
const otherTags = [
|
||||
"color", "font", "img", "achievement", "gps", "special-item", "armor", "train", "train-stop", "tooltip",
|
||||
"shortcut", "tip",
|
||||
];
|
||||
const richTextPattern = new RegExp(
|
||||
`\\[(?:/(?:color|font|tooltip)|(${[...Object.keys(iconTags), ...otherTags].join("|")})(?:=([^\\]]*))?)\\]`,
|
||||
"g",
|
||||
);
|
||||
|
||||
/**
|
||||
* Remove Factorio rich text tags from text, replacing icons of prototypes with their name.
|
||||
*
|
||||
* @param locale - Flattened locale from the mod pack export.
|
||||
* @param text - Text that may contain rich text tags.
|
||||
* @returns plain text
|
||||
* @see https://wiki.factorio.com/Rich_text
|
||||
*/
|
||||
export function stripRichText(locale: Locale, text: string) {
|
||||
return text.replace(richTextPattern, (_match, tag: string | undefined, value: string | undefined) => {
|
||||
const category = tag ? iconTags[tag] : undefined;
|
||||
if (!category || !value) {
|
||||
return "";
|
||||
}
|
||||
const name = value.split(",")[0];
|
||||
return locale.get(`${category}.${name}`) ?? name;
|
||||
}).trim();
|
||||
}
|
||||
|
||||
function localisedText(locale: Locale, localised: FactorioLocalisedString | undefined, key: string) {
|
||||
let text: string | undefined;
|
||||
if (localised !== undefined) {
|
||||
text = resolveLocalisedString(locale, localised);
|
||||
}
|
||||
text ??= resolveLocalisedString(locale, [key]);
|
||||
return text === undefined ? undefined : stripRichText(locale, text);
|
||||
}
|
||||
|
||||
type SettingPrototype = {
|
||||
name: string,
|
||||
localised_name?: FactorioLocalisedString,
|
||||
localised_description?: FactorioLocalisedString,
|
||||
};
|
||||
|
||||
/**
|
||||
* Display name of a mod setting, falling back the same way Factorio does.
|
||||
*
|
||||
* @param locale - Flattened locale from the mod pack export.
|
||||
* @param prototype - Mod setting prototype.
|
||||
* @returns localised name, or the internal name if there is none.
|
||||
*/
|
||||
export function modSettingName(locale: Locale, prototype: SettingPrototype) {
|
||||
return localisedText(locale, prototype.localised_name, `mod-setting-name.${prototype.name}`) || prototype.name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Description of a mod setting, falling back the same way Factorio does.
|
||||
*
|
||||
* @param locale - Flattened locale from the mod pack export.
|
||||
* @param prototype - Mod setting prototype.
|
||||
* @returns localised description, or undefined if there is none.
|
||||
*/
|
||||
export function modSettingDescription(locale: Locale, prototype: SettingPrototype) {
|
||||
return localisedText(
|
||||
locale, prototype.localised_description, `mod-setting-description.${prototype.name}`
|
||||
) || undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Display name of an allowed value of a string mod setting.
|
||||
*
|
||||
* @param locale - Flattened locale from the mod pack export.
|
||||
* @param name - Internal name of the mod setting.
|
||||
* @param value - Allowed value.
|
||||
* @returns localised value, or the value itself if there is none.
|
||||
*/
|
||||
export function modSettingValueName(locale: Locale, name: string, value: string | number) {
|
||||
return localisedText(locale, undefined, `string-mod-setting.${name}-${value}`) || String(value);
|
||||
}
|
||||
@@ -8,6 +8,8 @@ export type ModSettingField = {
|
||||
mod?: string;
|
||||
order?: string;
|
||||
allowed_values?: unknown;
|
||||
localised_name?: lib.FactorioLocalisedString;
|
||||
localised_description?: lib.FactorioLocalisedString;
|
||||
};
|
||||
|
||||
const settingTypes = ["bool-setting", "int-setting", "double-setting", "string-setting", "color-setting"];
|
||||
@@ -50,6 +52,8 @@ export function modSettingFields(
|
||||
mod: p.mod as string,
|
||||
order: p.order,
|
||||
allowed_values: p.allowed_values,
|
||||
localised_name: p.localised_name,
|
||||
localised_description: p.localised_description,
|
||||
}))
|
||||
.sort((a, b) => strcmp(a.mod, b.mod) || strcmp(a.order || "", b.order || "") || strcmp(a.name, b.name))
|
||||
;
|
||||
|
||||
Reference in new issue
Block a user