mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-29 11:42:17 +00:00
fix(i18n): localize browser and count grammar
This commit is contained in:
parent
aa4157a54a
commit
590a02fd2c
10 changed files with 85 additions and 29 deletions
|
|
@ -299,6 +299,15 @@ export const dict = {
|
|||
"common.loading.ellipsis": "…",
|
||||
"common.cancel": "Cancel",
|
||||
"common.clear": "Clear",
|
||||
"common.color.orange": "orange",
|
||||
"common.color.yellow": "yellow",
|
||||
"common.color.cyan": "cyan",
|
||||
"common.color.green": "green",
|
||||
"common.color.red": "red",
|
||||
"common.color.pink": "pink",
|
||||
"common.color.blue": "blue",
|
||||
"common.color.purple": "purple",
|
||||
"common.color.gray": "gray",
|
||||
"common.open": "Open",
|
||||
"common.connect": "Connect",
|
||||
"common.disconnect": "Disconnect",
|
||||
|
|
@ -648,6 +657,11 @@ export const dict = {
|
|||
"toast.migration.failed.title": "Data migration failed",
|
||||
"toast.migration.progress.clearingOldEvents": "Clearing old events",
|
||||
"toast.migration.progress.migratingSessions": "Migrating sessions",
|
||||
"toast.migration.progress.migratingSessions.current": "Migrating sessions {{current}}",
|
||||
"toast.migration.progress.migratingSessions.progress": "Migrating sessions {{current}}/{{total}}",
|
||||
"toast.migration.progress.working": "Migrating data",
|
||||
"toast.migration.progress.working.current": "Migrating data {{current}}",
|
||||
"toast.migration.progress.working.progress": "Migrating data {{current}}/{{total}}",
|
||||
|
||||
"toast.update.title": "Update available",
|
||||
"toast.update.description": "A new version of OpenCode ({{version}}) is now available to install.",
|
||||
|
|
@ -909,6 +923,7 @@ export const dict = {
|
|||
|
||||
"session.browser.address": "Browser address",
|
||||
"session.browser.replaced": "Browser control moved to another desktop window.",
|
||||
"session.browser.unsupported": "This desktop app does not support the browser pane.",
|
||||
"session.browser.suspended": "Browser suspended. Interact with this session to reconnect.",
|
||||
"session.browser.address.placeholder": "Enter URL",
|
||||
|
||||
|
|
|
|||
|
|
@ -238,11 +238,6 @@ export const { use: useLanguage, provider: LanguageProvider } = createSimpleCont
|
|||
params?: Record<string, string | number | boolean>,
|
||||
) => string
|
||||
|
||||
const rich = (key: Parameters<typeof t>[0], params: Record<string, JSX.Element>) =>
|
||||
t(key)
|
||||
.split(/(\{\{\w+\}\})/g)
|
||||
.map((part, index) => (index % 2 ? (params[part.slice(2, -2)] ?? part) : part))
|
||||
|
||||
const pluralForm = (
|
||||
key: PluralKey,
|
||||
category: UiPluralCategory,
|
||||
|
|
@ -293,7 +288,6 @@ export const { use: useLanguage, provider: LanguageProvider } = createSimpleCont
|
|||
locales: LOCALES,
|
||||
label,
|
||||
t,
|
||||
rich,
|
||||
plural,
|
||||
pluralForm,
|
||||
rich,
|
||||
|
|
|
|||
|
|
@ -36,6 +36,12 @@ export function SessionBrowserPane(props: { browser: ReturnType<typeof createSes
|
|||
canvas.width = canvas.height = 1
|
||||
const paint = canvas.getContext("2d", { willReadFrequently: true })
|
||||
const scheme = () => store.address.match(/^https?:\/\//i)?.[0] ?? ""
|
||||
const error = () => {
|
||||
const value = props.browser.error()
|
||||
if (value === "browser.pane.replaced") return language.t("session.browser.replaced")
|
||||
if (value === "browser.pane.unsupported") return language.t("session.browser.unsupported")
|
||||
return value
|
||||
}
|
||||
|
||||
command.register("browser.navigation", () => [
|
||||
{
|
||||
|
|
@ -237,9 +243,13 @@ export function SessionBrowserPane(props: { browser: ReturnType<typeof createSes
|
|||
</div>
|
||||
</form>
|
||||
</div>
|
||||
<Show when={props.browser.error()}>
|
||||
<div class="shrink-0 px-3 py-1.5 text-12-regular text-text-danger-base border-b border-v2-border-border-muted">
|
||||
{props.browser.error()}
|
||||
<Show when={error()}>
|
||||
<div
|
||||
class="shrink-0 px-3 py-1.5 text-12-regular text-text-danger-base border-b border-v2-border-border-muted"
|
||||
role="alert"
|
||||
aria-live="assertive"
|
||||
>
|
||||
{error()}
|
||||
</div>
|
||||
</Show>
|
||||
<div ref={surface} class="min-h-0 flex-1 bg-v2-background-bg-base flex items-center justify-center">
|
||||
|
|
|
|||
|
|
@ -112,7 +112,9 @@ export const SettingsProjectGeneral: Component<{
|
|||
return (
|
||||
<button
|
||||
type="button"
|
||||
aria-label={language.t("dialog.project.edit.color.select", { color })}
|
||||
aria-label={language.t("dialog.project.edit.color.select", {
|
||||
color: language.t(`common.color.${color}`),
|
||||
})}
|
||||
aria-pressed={selected()}
|
||||
class="project-settings-color"
|
||||
classList={{ "project-settings-color--selected": selected() }}
|
||||
|
|
|
|||
|
|
@ -16,15 +16,23 @@ export function MigrationStatus(props: { server: ServerReadyData }) {
|
|||
|
||||
const format = (progress: Progress | undefined) => {
|
||||
if (!progress) return ""
|
||||
const label =
|
||||
progress.label === "Clearing old events"
|
||||
? language.t("toast.migration.progress.clearingOldEvents")
|
||||
: progress.label === "Migrating sessions"
|
||||
? language.t("toast.migration.progress.migratingSessions")
|
||||
: progress.label
|
||||
if (progress.numerator === undefined) return label
|
||||
if (progress.denominator === undefined) return `${label} ${progress.numerator}`
|
||||
return `${label} ${progress.numerator}/${progress.denominator}`
|
||||
if (progress.label === "Clearing old events") return language.t("toast.migration.progress.clearingOldEvents")
|
||||
if (progress.label === "Migrating sessions") {
|
||||
if (progress.numerator === undefined) return language.t("toast.migration.progress.migratingSessions")
|
||||
if (progress.denominator === undefined)
|
||||
return language.t("toast.migration.progress.migratingSessions.current", { current: progress.numerator })
|
||||
return language.t("toast.migration.progress.migratingSessions.progress", {
|
||||
current: progress.numerator,
|
||||
total: progress.denominator,
|
||||
})
|
||||
}
|
||||
if (progress.numerator === undefined) return language.t("toast.migration.progress.working")
|
||||
if (progress.denominator === undefined)
|
||||
return language.t("toast.migration.progress.working.current", { current: progress.numerator })
|
||||
return language.t("toast.migration.progress.working.progress", {
|
||||
current: progress.numerator,
|
||||
total: progress.denominator,
|
||||
})
|
||||
}
|
||||
|
||||
const hide = () => {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import { Index, createMemo } from "solid-js"
|
||||
import type { UiI18nPluralKey } from "@opencode/ui/context"
|
||||
import { useI18n, type UiI18nPluralKey } from "@opencode/ui/context"
|
||||
import { AnimatedCountLabel } from "./tool-count-label"
|
||||
|
||||
export type CountItem = {
|
||||
|
|
@ -8,7 +8,9 @@ export type CountItem = {
|
|||
}
|
||||
|
||||
export function AnimatedCountList(props: { items: CountItem[]; fallback?: string; class?: string }) {
|
||||
const i18n = useI18n()
|
||||
const firstPositive = createMemo(() => props.items.findIndex((item) => item.count > 0))
|
||||
const active = createMemo(() => props.items.flatMap((item, index) => (item.count > 0 ? [index] : [])))
|
||||
const fallback = createMemo(() => props.fallback ?? "")
|
||||
const showEmpty = createMemo(() => firstPositive() === -1 && fallback().length > 0)
|
||||
|
||||
|
|
@ -20,16 +22,14 @@ export function AnimatedCountList(props: { items: CountItem[]; fallback?: string
|
|||
|
||||
<Index each={props.items}>
|
||||
{(item, index) => {
|
||||
const active = createMemo(() => item().count > 0)
|
||||
const visible = createMemo(() => item().count > 0)
|
||||
const position = createMemo(() => active().indexOf(index))
|
||||
return (
|
||||
<>
|
||||
<span
|
||||
data-slot="tool-count-summary-prefix"
|
||||
data-active={active() && firstPositive() !== index ? "true" : "false"}
|
||||
>
|
||||
,
|
||||
<span data-slot="tool-count-summary-prefix" data-active={visible() && position() > 0 ? "true" : "false"}>
|
||||
{i18n.listSeparator(position(), active().length)}
|
||||
</span>
|
||||
<span data-slot="tool-count-summary-item" data-active={active() ? "true" : "false"}>
|
||||
<span data-slot="tool-count-summary-item" data-active={visible() ? "true" : "false"}>
|
||||
<span data-slot="tool-count-summary-item-inner">
|
||||
<AnimatedCountLabel plural={item().key} count={Math.max(0, Math.round(item().count))} />
|
||||
</span>
|
||||
|
|
|
|||
|
|
@ -379,7 +379,7 @@ export function getToolInfo(
|
|||
title: i18n.t("ui.tool.patch"),
|
||||
subtitle:
|
||||
Array.isArray(input.files) && input.files.length
|
||||
? `${input.files.length} ${i18n.plural("ui.common.file", input.files.length)}`
|
||||
? i18n.plural("ui.common.fileCount", input.files.length)
|
||||
: undefined,
|
||||
}
|
||||
case "todowrite":
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import { describe, expect, test } from "bun:test"
|
||||
import { createUiI18n, pluralCategory, type UiI18nSource } from "./i18n"
|
||||
import { createUiI18n, localizedListSeparator, pluralCategory, type UiI18nSource } from "./i18n"
|
||||
|
||||
describe("pluralCategory", () => {
|
||||
test.each([
|
||||
|
|
@ -47,3 +47,11 @@ describe("dynamic source copy", () => {
|
|||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("localizedListSeparator", () => {
|
||||
test("uses locale list punctuation and conjunctions", () => {
|
||||
expect(localizedListSeparator("en", 1, 3)).toBe(", ")
|
||||
expect(localizedListSeparator("en", 2, 3)).toBe(", and ")
|
||||
expect(localizedListSeparator("de", 1, 2)).toBe(" und ")
|
||||
})
|
||||
})
|
||||
|
|
|
|||
|
|
@ -25,6 +25,7 @@ export type UiI18nSource = {
|
|||
export type UiI18n = UiI18nSource & {
|
||||
/** Preserve runtime-generated English copy while using the keyed dictionary for every other locale. */
|
||||
tDynamic: (key: UiI18nOrdinaryKey, source: string, params?: UiI18nParams) => string
|
||||
listSeparator: (index: number, count: number) => string
|
||||
}
|
||||
|
||||
const rules = new Map<string, Intl.PluralRules>()
|
||||
|
|
@ -42,6 +43,21 @@ export function pluralKey(key: UiI18nPluralKey, category: UiPluralCategory) {
|
|||
return `${key}.${category}` as UiI18nPluralLookupKey
|
||||
}
|
||||
|
||||
export function localizedListSeparator(locale: string, index: number, count: number) {
|
||||
if (index <= 0 || index >= count) return ""
|
||||
const parts = new Intl.ListFormat(locale, { style: "long", type: "conjunction" }).formatToParts(
|
||||
Array.from({ length: count }, (_, item) => String(item)),
|
||||
)
|
||||
const current = parts.findIndex((part) => part.type === "element" && part.value === String(index))
|
||||
const previous = parts.findLastIndex(
|
||||
(part, partIndex) => partIndex < current && part.type === "element" && part.value === String(index - 1),
|
||||
)
|
||||
return parts
|
||||
.slice(previous + 1, current)
|
||||
.map((part) => part.value)
|
||||
.join("")
|
||||
}
|
||||
|
||||
function resolveTemplate(text: string, params?: UiI18nParams) {
|
||||
if (!params) return text
|
||||
return text.replace(/{{\s*([^}]+?)\s*}}/g, (_, rawKey) => {
|
||||
|
|
@ -56,6 +72,7 @@ export function createUiI18n(source: UiI18nSource): UiI18n {
|
|||
...source,
|
||||
tDynamic: (key, value, params) =>
|
||||
source.locale().toLowerCase().split("-")[0] === "en" ? resolveTemplate(value, params) : source.t(key, params),
|
||||
listSeparator: (index, count) => localizedListSeparator(source.locale(), index, count),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -211,6 +211,8 @@ const source = {
|
|||
|
||||
"ui.common.file.one": "file",
|
||||
"ui.common.file.other": "files",
|
||||
"ui.common.fileCount.one": "{{count}} file",
|
||||
"ui.common.fileCount.other": "{{count}} files",
|
||||
"ui.common.question.one": "question",
|
||||
"ui.common.question.other": "questions",
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue