i18n: Refactor string formatting to use arg() method for translations

This commit is contained in:
月月
2025-07-16 21:11:27 +08:00
parent b98e843a9d
commit 2ad60a40a8
11 changed files with 85 additions and 72 deletions
@@ -377,7 +377,7 @@ Inline w/ backslash and round brackets \\(e^{i\\pi} + 1 = 0\\)
Layout.fillWidth: true
padding: 10
color: activeFocus ? Appearance.m3colors.m3onSurface : Appearance.m3colors.m3onSurfaceVariant
placeholderText: StringUtils.format(Translation.tr('Message the model... "{0}" for commands'), root.commandPrefix)
placeholderText: Translation.tr('Message the model... "%1" for commands').arg(root.commandPrefix)
background: null
@@ -577,8 +577,9 @@ Inline w/ backslash and round brackets \\(e^{i\\pi} + 1 = 0\\)
id: toolTip
extraVisibleCondition: false
alternativeVisibleCondition: mouseArea.containsMouse // Show tooltip when hovered
content: StringUtils.format(Translation.tr("Current model: {0}\nSet it with {1}model MODEL"),
Ai.getModel().name, root.commandPrefix)
content: Translation.tr("Current model: %1\nSet it with %2model MODEL")
.arg(Ai.getModel().name)
.arg(root.commandPrefix)
}
MouseArea {
@@ -242,7 +242,7 @@ Item {
font.pixelSize: Appearance.font.pixelSize.smaller
color: Appearance.m3colors.m3inverseOnSurface
wrapMode: Text.Wrap
text: StringUtils.format(Translation.tr("{0} queries pending"), Booru.runningRequests)
text: Translation.tr("%1 queries pending").arg(Booru.runningRequests)
}
}
}
@@ -354,7 +354,7 @@ Item {
padding: 10
color: activeFocus ? Appearance.m3colors.m3onSurface : Appearance.m3colors.m3onSurfaceVariant
renderType: Text.NativeRendering
placeholderText: StringUtils.format(Translation.tr('Enter tags, or "{0}" for commands'), root.commandPrefix)
placeholderText: Translation.tr('Enter tags, or "%1" for commands').arg(root.commandPrefix)
background: null
@@ -518,8 +518,9 @@ Item {
extraVisibleCondition: false
alternativeVisibleCondition: mouseArea.containsMouse // Show tooltip when hovered
// content: Translation.tr("The current API used. Endpoint: ") + Booru.providers[Booru.currentProvider].url + Translation.tr("\nSet with /mode PROVIDER")
content: StringUtils.format(Translation.tr("Current API endpoint: {0}\nSet it with {1}mode PROVIDER"),
Booru.providers[Booru.currentProvider].url, root.commandPrefix)
content: Translation.tr("Current API endpoint: %1\nSet it with %2mode PROVIDER")
.arg(Booru.providers[Booru.currentProvider].url)
.arg(root.commandPrefix)
}
MouseArea {
@@ -165,7 +165,7 @@ Button {
id: sourceButton
visible: root.imageData.source && root.imageData.source.length > 0
Layout.fillWidth: true
buttonText: StringUtils.format(Translation.tr("Go to source ({0})"), StringUtils.getDomain(root.imageData.source))
buttonText: Translation.tr("Go to source (%1)").arg(StringUtils.getDomain(root.imageData.source))
enabled: root.imageData.source && root.imageData.source.length > 0
onClicked: {
root.showActions = false
@@ -95,7 +95,7 @@ Rectangle {
font.pixelSize: Appearance.font.pixelSize.smaller
color: Appearance.colors.colOnLayer2
// text: `Page ${root.responseData.page}`
text: StringUtils.format(Translation.tr("Page {0}"), root.responseData.page)
text: Translation.tr("Page %1").arg(root.responseData.page)
}
}
}
@@ -124,7 +124,7 @@ Scope {
StyledText {
font.pixelSize: Appearance.font.pixelSize.normal
color: Appearance.colors.colOnLayer0
text: StringUtils.format(Translation.tr("Uptime: {0}"), DateTime.uptime)
text: Translation.tr("Uptime: %1").arg(DateTime.uptime)
textFormat: Text.MarkdownText
}
@@ -29,7 +29,7 @@ QuickToggleButton {
}
}
StyledToolTip {
content: StringUtils.format(Translation.tr("{0} | Right-click to configure"),
content: Translation.tr("%1 | Right-click to configure").arg(
(Bluetooth.bluetoothEnabled && Bluetooth.bluetoothDeviceName.length > 0) ?
Bluetooth.bluetoothDeviceName : Translation.tr("Bluetooth"))
@@ -29,6 +29,6 @@ QuickToggleButton {
}
}
StyledToolTip {
content: StringUtils.format(Translation.tr("{0} | Right-click to configure"), Network.networkName)
content: Translation.tr("%1 | Right-click to configure").arg(Network.networkName)
}
}
+20 -17
View File
@@ -185,7 +185,7 @@ Singleton {
"openrouter-llama4-maverick": {
"name": "Llama 4 Maverick",
"icon": "ollama-symbolic",
"description": StringUtils.format(Translation.tr("Online via {0} | {1}'s model"), "OpenRouter", "Meta"),
"description": Translation.tr("Online via %1 | %2's model").arg("OpenRouter").arg("Meta"),
"homepage": "https://openrouter.ai/meta-llama/llama-4-maverick:free",
"endpoint": "https://openrouter.ai/api/v1/chat/completions",
"model": "meta-llama/llama-4-maverick:free",
@@ -197,7 +197,7 @@ Singleton {
"openrouter-deepseek-r1": {
"name": "DeepSeek R1",
"icon": "deepseek-symbolic",
"description": StringUtils.format(Translation.tr("Online via {0} | {1}'s model"), "OpenRouter", "DeepSeek"),
"description": Translation.tr("Online via %1 | %2's model").arg("OpenRouter").arg("DeepSeek"),
"homepage": "https://openrouter.ai/deepseek/deepseek-r1:free",
"endpoint": "https://openrouter.ai/api/v1/chat/completions",
"model": "deepseek/deepseek-r1:free",
@@ -250,7 +250,7 @@ Singleton {
root.models[safeModelName] = {
"name": guessModelName(model),
"icon": guessModelLogo(model),
"description": StringUtils.format(Translation.tr("Local Ollama model | {0}"), model),
"description": Translation.tr("Local Ollama model | %1").arg(model),
"homepage": `https://ollama.com/library/${model}`,
"endpoint": "http://localhost:11434/v1/chat/completions",
"model": model,
@@ -314,12 +314,12 @@ Singleton {
onLoadedChanged: {
if (!promptLoader.loaded) return;
Config.options.ai.systemPrompt = promptLoader.text();
root.addMessage(StringUtils.format("Loaded the following system prompt\n\n---\n\n{0}", Config.options.ai.systemPrompt), root.interfaceRole);
root.addMessage(Translation.tr("Loaded the following system prompt\n\n---\n\n%1").arg(Config.options.ai.systemPrompt), root.interfaceRole);
}
}
function printPrompt() {
root.addMessage(StringUtils.format("The current system prompt is\n\n---\n\n{0}", Config.options.ai.systemPrompt), root.interfaceRole);
root.addMessage(Translation.tr("The current system prompt is\n\n---\n\n%1").arg(Config.options.ai.systemPrompt), root.interfaceRole);
}
function loadPrompt(filePath) {
@@ -352,8 +352,8 @@ Singleton {
function addApiKeyAdvice(model) {
root.addMessage(
StringUtils.format(Translation.tr('To set an API key, pass it with the command\n\nTo view the key, pass "get" with the command<br/>\n\n### For {0}:\n\n**Link**: {1}\n\n{2}'),
model.name, model.key_get_link, model.key_get_description ?? Translation.tr("<i>No further instruction provided</i>")),
Translation.tr('To set an API key, pass it with the command\n\nTo view the key, pass "get" with the command<br/>\n\n### For %1:\n\n**Link**: %2\n\n%3')
.arg(model.name).arg(model.key_get_link).arg(model.key_get_description ?? Translation.tr("<i>No further instruction provided</i>")),
Ai.interfaceRole
);
}
@@ -371,11 +371,14 @@ Singleton {
if (model?.requires_key) KeyringStorage.fetchKeyringData();
// See if policy prevents online models
if (Config.options.policies.ai === 2 && !model.endpoint.includes("localhost")) {
root.addMessage(StringUtils.format(StringUtils.format("Online models disallowed\n\nControlled by `policies.ai` config option"), model.name), root.interfaceRole);
root.addMessage(
Translation.tr("Online models disallowed for %1\n\nControlled by `policies.ai` config option").arg(model.name),
root.interfaceRole
);
return;
}
if (setPersistentState) Persistent.states.ai.model = modelId;
if (feedback) root.addMessage(StringUtils.format("Model set to {0}", model.name), root.interfaceRole);
if (feedback) root.addMessage(Translation.tr("Model set to %1").arg(model.name), root.interfaceRole);
if (model.requires_key) {
// If key not there show advice
if (root.apiKeysLoaded && (!root.apiKeys[model.key_id] || root.apiKeys[model.key_id].length === 0)) {
@@ -398,13 +401,13 @@ Singleton {
}
Persistent.states.ai.temperature = value;
root.temperature = value;
root.addMessage(StringUtils.format(Translation.tr("Temperature set to {0}"), value), Ai.interfaceRole);
root.addMessage(Translation.tr("Temperature set to %1").arg(value), Ai.interfaceRole);
}
function setApiKey(key) {
const model = models[currentModelId];
if (!model.requires_key) {
root.addMessage(StringUtils.format(Translation.tr("{0} does not require an API key"), model.name), Ai.interfaceRole);
root.addMessage(Translation.tr("%1 does not require an API key").arg(model.name), Ai.interfaceRole);
return;
}
if (!key || key.length === 0) {
@@ -413,7 +416,7 @@ Singleton {
return;
}
KeyringStorage.setNestedField(["apiKeys", model.key_id], key.trim());
root.addMessage(StringUtils.format(Translation.tr("API key set for {0}"), model.name, Ai.interfaceRole));
root.addMessage(Translation.tr("API key set for %1").arg(model.name), Ai.interfaceRole);
}
function printApiKey() {
@@ -421,17 +424,17 @@ Singleton {
if (model.requires_key) {
const key = root.apiKeys[model.key_id];
if (key) {
root.addMessage(StringUtils.format(Translation.tr("API key:\n\n```txt\n{0}\n```"), key), Ai.interfaceRole);
root.addMessage(Translation.tr("API key:\n\n```txt\n%1\n```").arg(key), Ai.interfaceRole);
} else {
root.addMessage(StringUtils.format(Translation.tr("No API key set for {0}"), model.name), Ai.interfaceRole);
root.addMessage(Translation.tr("No API key set for %1").arg(model.name), Ai.interfaceRole);
}
} else {
root.addMessage(StringUtils.format(Translation.tr("{0} does not require an API key"), model.name), Ai.interfaceRole);
root.addMessage(Translation.tr("%1 does not require an API key").arg(model.name), Ai.interfaceRole);
}
}
function printTemperature() {
root.addMessage(StringUtils.format(Translation.tr("Temperature: {0}"), root.temperature), Ai.interfaceRole);
root.addMessage(Translation.tr("Temperature: %1").arg(root.temperature), Ai.interfaceRole);
}
function clearMessages() {
@@ -799,7 +802,7 @@ Singleton {
const value = args.value;
Config.setNestedValue(key, value);
}
else root.addMessage(Translation.tr("Unknown function call: {0}"), "assistant");
else root.addMessage(Translation.tr("Unknown function call: %1").arg(name), "assistant");
}
function chatToJson() {
@@ -27,7 +27,7 @@ Singleton {
return arr.concat([key, root.properties[key]]);
}, []
)
property string keyringLabel: StringUtils.format(Translation.tr("{0} Safe Storage"), "illogical-impulse")
property string keyringLabel: Translation.tr("%1 Safe Storage").arg("illogical-impulse")
function setNestedField(path, value) {
if (!root.keyringData) root.keyringData = {};