From 418398587cef90151745ba0dbc51cef4762045ca Mon Sep 17 00:00:00 2001 From: boojack Date: Tue, 9 Jun 2026 09:33:17 +0800 Subject: [PATCH] feat(i18n): add searchable locale picker --- web/src/components/LocalePicker.tsx | 111 +++++++++++++++++++++++++++ web/src/components/LocaleSelect.tsx | 24 +----- web/src/components/UserMenu.tsx | 14 +--- web/src/locales/ar.json | 3 + web/src/locales/bg.json | 3 + web/src/locales/ca.json | 3 + web/src/locales/cs.json | 3 + web/src/locales/da.json | 3 + web/src/locales/de.json | 3 + web/src/locales/el.json | 3 + web/src/locales/en-GB.json | 3 + web/src/locales/en.json | 3 + web/src/locales/es.json | 3 + web/src/locales/et.json | 3 + web/src/locales/fa.json | 3 + web/src/locales/fi.json | 3 + web/src/locales/fr.json | 3 + web/src/locales/gl.json | 3 + web/src/locales/hi.json | 3 + web/src/locales/hr.json | 3 + web/src/locales/hu.json | 3 + web/src/locales/id.json | 3 + web/src/locales/it.json | 3 + web/src/locales/ja.json | 3 + web/src/locales/ka-GE.json | 3 + web/src/locales/ko.json | 3 + web/src/locales/lt.json | 3 + web/src/locales/lv.json | 3 + web/src/locales/mr.json | 3 + web/src/locales/nb.json | 3 + web/src/locales/nl.json | 3 + web/src/locales/pl.json | 3 + web/src/locales/pt-BR.json | 3 + web/src/locales/pt-PT.json | 3 + web/src/locales/ro.json | 3 + web/src/locales/ru.json | 3 + web/src/locales/sk.json | 3 + web/src/locales/sl.json | 3 + web/src/locales/sr.json | 3 + web/src/locales/sv.json | 3 + web/src/locales/th.json | 3 + web/src/locales/tr.json | 3 + web/src/locales/uk.json | 3 + web/src/locales/vi.json | 3 + web/src/locales/zh-Hans.json | 3 + web/src/locales/zh-Hant.json | 3 + web/src/utils/i18n.ts | 33 ++++++++ web/tests/i18n-locale-search.test.ts | 24 ++++++ web/tests/locale-picker.test.tsx | 14 ++++ 49 files changed, 318 insertions(+), 31 deletions(-) create mode 100644 web/src/components/LocalePicker.tsx create mode 100644 web/tests/i18n-locale-search.test.ts create mode 100644 web/tests/locale-picker.test.tsx diff --git a/web/src/components/LocalePicker.tsx b/web/src/components/LocalePicker.tsx new file mode 100644 index 00000000..23ef1e32 --- /dev/null +++ b/web/src/components/LocalePicker.tsx @@ -0,0 +1,111 @@ +import { CheckIcon, ChevronDownIcon, GlobeIcon, SearchIcon } from "lucide-react"; +import { useMemo, useState } from "react"; +import { useTranslation } from "react-i18next"; +import { Button } from "@/components/ui/button"; +import { Input } from "@/components/ui/input"; +import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover"; +import { locales } from "@/i18n"; +import { cn } from "@/lib/utils"; +import { getLocaleDisplayName, localeMatchesSearch, useTranslate } from "@/utils/i18n"; + +const MISSING_LANGUAGE_FEEDBACK_URL = + "https://github.com/usememos/memos/issues/new?title=Missing%20language%20support&body=Please%20add%20support%20for%20this%20language%3A%0A%0A-%20Language%3A%20"; + +interface LocaleSearchListProps { + value: Locale; + onChange: (locale: Locale) => void; + className?: string; +} + +export const LocaleSearchList = (props: LocaleSearchListProps) => { + const { value, onChange, className } = props; + const t = useTranslate(); + const { i18n } = useTranslation(); + const [query, setQuery] = useState(""); + const filteredLocales = useMemo( + () => locales.filter((locale) => localeMatchesSearch(locale, query, i18n.language)), + [i18n.language, query], + ); + + return ( +
+
+ + setQuery(event.target.value)} + onKeyDown={(event) => event.stopPropagation()} + className="pl-8" + placeholder={`${t("common.search")} ${t("common.language").toLowerCase()}`} + aria-label={t("common.language")} + /> +
+
+ {filteredLocales.map((locale) => ( + + ))} + {filteredLocales.length === 0 && ( +
+ + {t("locale-picker.no-language-feedback")} + +
+ )} +
+
+ ); +}; + +interface LocalePickerProps { + value: Locale; + onChange: (locale: Locale) => void; + className?: string; +} + +const LocalePicker = (props: LocalePickerProps) => { + const { value, onChange, className } = props; + const [open, setOpen] = useState(false); + + const handleChange = (locale: Locale) => { + onChange(locale); + setOpen(false); + }; + + return ( + + + + + + + + + ); +}; + +export default LocalePicker; diff --git a/web/src/components/LocaleSelect.tsx b/web/src/components/LocaleSelect.tsx index 55b52b21..8c63bf10 100644 --- a/web/src/components/LocaleSelect.tsx +++ b/web/src/components/LocaleSelect.tsx @@ -1,8 +1,6 @@ -import { GlobeIcon } from "lucide-react"; import { FC } from "react"; -import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; -import { locales } from "@/i18n"; -import { getLocaleDisplayName, loadLocale } from "@/utils/i18n"; +import LocalePicker from "@/components/LocalePicker"; +import { loadLocale } from "@/utils/i18n"; interface Props { value: Locale; @@ -19,23 +17,7 @@ const LocaleSelect: FC = (props: Props) => { onChange(locale); }; - return ( - - ); + return ; }; export default LocaleSelect; diff --git a/web/src/components/UserMenu.tsx b/web/src/components/UserMenu.tsx index 985f7d13..8453c130 100644 --- a/web/src/components/UserMenu.tsx +++ b/web/src/components/UserMenu.tsx @@ -14,11 +14,11 @@ import useCurrentUser from "@/hooks/useCurrentUser"; import { useSSEConnectionStatus } from "@/hooks/useLiveMemoRefresh"; import useNavigateTo from "@/hooks/useNavigateTo"; import { useUpdateUserGeneralSetting } from "@/hooks/useUserQueries"; -import { locales } from "@/i18n"; import { cn } from "@/lib/utils"; import { Routes } from "@/router"; -import { getLocaleDisplayName, getLocaleWithFallback, loadLocale, useTranslate } from "@/utils/i18n"; +import { getLocaleWithFallback, loadLocale, useTranslate } from "@/utils/i18n"; import { getThemeWithFallback, loadTheme, THEME_OPTIONS } from "@/utils/theme"; +import { LocaleSearchList } from "./LocalePicker"; import UserAvatar from "./UserAvatar"; import { DropdownMenu, @@ -151,14 +151,8 @@ const UserMenu = (props: Props) => { {t("common.language")} - - {locales.map((locale) => ( - handleLocaleChange(locale)}> - {currentLocale === locale && } - {currentLocale !== locale && } - {getLocaleDisplayName(locale)} - - ))} + + diff --git a/web/src/locales/ar.json b/web/src/locales/ar.json index e34c3c95..bc707c09 100644 --- a/web/src/locales/ar.json +++ b/web/src/locales/ar.json @@ -116,6 +116,9 @@ "visibility": "الرؤية", "yourself": "نفسك" }, + "locale-picker": { + "no-language-feedback": "لم يتم العثور على اللغة؟ إرسال ردود الفعل" + }, "editor": { "add-your-comment-here": "أضف تعليقك هنا...", "any-thoughts": "أية أفكار...", diff --git a/web/src/locales/bg.json b/web/src/locales/bg.json index 33505345..cbc9b6c0 100644 --- a/web/src/locales/bg.json +++ b/web/src/locales/bg.json @@ -156,6 +156,9 @@ "visibility": "Видимост", "yourself": "Себе си" }, + "locale-picker": { + "no-language-feedback": "Езикът не е намерен? Изпратете обратна връзка" + }, "editor": { "add-your-comment-here": "Добавете своя коментар тук...", "any-thoughts": "Някакви мисли...", diff --git a/web/src/locales/ca.json b/web/src/locales/ca.json index 463594c5..e69bdf92 100644 --- a/web/src/locales/ca.json +++ b/web/src/locales/ca.json @@ -119,6 +119,9 @@ "empty-placeholder": "Buit", "unlink": "Desenllaça" }, + "locale-picker": { + "no-language-feedback": "No s'ha trobat l'idioma? Envieu comentaris" + }, "editor": { "add-your-comment-here": "Afegeix el teu comentari aquí...", "any-thoughts": "Alguna idea...", diff --git a/web/src/locales/cs.json b/web/src/locales/cs.json index 10130b9a..925c7f90 100644 --- a/web/src/locales/cs.json +++ b/web/src/locales/cs.json @@ -119,6 +119,9 @@ "empty-placeholder": "Prázdný", "unlink": "Odpojit" }, + "locale-picker": { + "no-language-feedback": "Jazyk nenalezen? Odeslat zpětnou vazbu" + }, "editor": { "add-your-comment-here": "Přidejte sem svůj komentář...", "any-thoughts": "Jakékoli myšlenky...", diff --git a/web/src/locales/da.json b/web/src/locales/da.json index e3033dc2..505bc6bb 100644 --- a/web/src/locales/da.json +++ b/web/src/locales/da.json @@ -156,6 +156,9 @@ "visibility": "Sigtbarhed", "yourself": "Dig selv" }, + "locale-picker": { + "no-language-feedback": "Sprog ikke fundet? Send feedback" + }, "editor": { "add-your-comment-here": "Tilføj din kommentar her...", "any-thoughts": "Nogen tanker...", diff --git a/web/src/locales/de.json b/web/src/locales/de.json index 3097b35d..bca72e3c 100644 --- a/web/src/locales/de.json +++ b/web/src/locales/de.json @@ -119,6 +119,9 @@ "empty-placeholder": "Leer", "unlink": "Verknüpfung aufheben" }, + "locale-picker": { + "no-language-feedback": "Sprache nicht gefunden? Geben Sie Feedback ab" + }, "editor": { "add-your-comment-here": "Füge deinen Kommentar hinzu...", "any-thoughts": "Ein Gedanke...", diff --git a/web/src/locales/el.json b/web/src/locales/el.json index eb353ea0..cca1890f 100644 --- a/web/src/locales/el.json +++ b/web/src/locales/el.json @@ -156,6 +156,9 @@ "visibility": "Ορατότητα", "yourself": "Σύ ο ίδιος" }, + "locale-picker": { + "no-language-feedback": "Δεν βρέθηκε η γλώσσα; Υποβολή σχολίων" + }, "editor": { "add-your-comment-here": "Προσθέστε το σχόλιό σας εδώ...", "any-thoughts": "Οποιαδήποτε σκέψη...", diff --git a/web/src/locales/en-GB.json b/web/src/locales/en-GB.json index c18b85a0..a8f2e498 100644 --- a/web/src/locales/en-GB.json +++ b/web/src/locales/en-GB.json @@ -119,6 +119,9 @@ "empty-placeholder": "Empty", "unlink": "Unlink" }, + "locale-picker": { + "no-language-feedback": "Language not found? Submit feedback" + }, "editor": { "add-your-comment-here": "Add your comment here...", "any-thoughts": "Any thoughts...", diff --git a/web/src/locales/en.json b/web/src/locales/en.json index fa0112bd..9d3ed9e2 100644 --- a/web/src/locales/en.json +++ b/web/src/locales/en.json @@ -156,6 +156,9 @@ "visibility": "Visibility", "yourself": "Yourself" }, + "locale-picker": { + "no-language-feedback": "Language not found? Submit feedback" + }, "editor": { "add-your-comment-here": "Add your comment here...", "any-thoughts": "Any thoughts...", diff --git a/web/src/locales/es.json b/web/src/locales/es.json index 2056de80..4c103117 100644 --- a/web/src/locales/es.json +++ b/web/src/locales/es.json @@ -119,6 +119,9 @@ "empty-placeholder": "Vacía", "unlink": "Desconectar" }, + "locale-picker": { + "no-language-feedback": "¿Idioma no encontrado? Enviar comentarios" + }, "editor": { "add-your-comment-here": "Agrega tu comentario aquí...", "any-thoughts": "Alguna idea...", diff --git a/web/src/locales/et.json b/web/src/locales/et.json index c6bc995b..0b3db07e 100644 --- a/web/src/locales/et.json +++ b/web/src/locales/et.json @@ -156,6 +156,9 @@ "visibility": "Nähtavus", "yourself": "iseennast" }, + "locale-picker": { + "no-language-feedback": "Keelt ei leitud? Esitage tagasisidet" + }, "editor": { "add-your-comment-here": "Lisa oma kommentaar siia...", "any-thoughts": "Mingid mõtted...", diff --git a/web/src/locales/fa.json b/web/src/locales/fa.json index 3b204deb..8bf41f75 100644 --- a/web/src/locales/fa.json +++ b/web/src/locales/fa.json @@ -116,6 +116,9 @@ "visibility": "قابل مشاهده توسط", "yourself": "خودتان" }, + "locale-picker": { + "no-language-feedback": "زبان پیدا نشد؟ ارسال بازخورد" + }, "editor": { "add-your-comment-here": "نظر خود را اینجا اضافه کنید...", "any-thoughts": "فکر شما...", diff --git a/web/src/locales/fi.json b/web/src/locales/fi.json index 329200ed..20d21e58 100644 --- a/web/src/locales/fi.json +++ b/web/src/locales/fi.json @@ -156,6 +156,9 @@ "visibility": "Näkyvyys", "yourself": "itseäsi" }, + "locale-picker": { + "no-language-feedback": "Eikö kieltä löydy? Lähetä palautetta" + }, "editor": { "add-your-comment-here": "Lisää kommenttisi tähän...", "any-thoughts": "Mitään ajatuksia...", diff --git a/web/src/locales/fr.json b/web/src/locales/fr.json index 77a97fea..218c7422 100644 --- a/web/src/locales/fr.json +++ b/web/src/locales/fr.json @@ -119,6 +119,9 @@ "empty-placeholder": "Vide", "unlink": "Dissocier" }, + "locale-picker": { + "no-language-feedback": "Langue introuvable ? Soumettre des commentaires" + }, "editor": { "add-your-comment-here": "Ajoutez votre commentaire ici...", "any-thoughts": "Des idées...", diff --git a/web/src/locales/gl.json b/web/src/locales/gl.json index dfef3f6b..3de258ed 100644 --- a/web/src/locales/gl.json +++ b/web/src/locales/gl.json @@ -119,6 +119,9 @@ "empty-placeholder": "Baleiro", "unlink": "Desvincular" }, + "locale-picker": { + "no-language-feedback": "Non se atopou o idioma? Enviar comentarios" + }, "editor": { "add-your-comment-here": "Engade aquí o comentario...", "any-thoughts": "O que pensas...", diff --git a/web/src/locales/hi.json b/web/src/locales/hi.json index e9327af7..309b4c56 100644 --- a/web/src/locales/hi.json +++ b/web/src/locales/hi.json @@ -116,6 +116,9 @@ "visibility": "दृश्यता", "yourself": "खुद" }, + "locale-picker": { + "no-language-feedback": "भाषा नहीं मिली? प्रतिक्रिया सबमिट करें" + }, "editor": { "add-your-comment-here": "अपनी टिप्पणी यहाँ जोड़ें...", "any-thoughts": "कोई विचार...", diff --git a/web/src/locales/hr.json b/web/src/locales/hr.json index 60d8c885..575487a9 100644 --- a/web/src/locales/hr.json +++ b/web/src/locales/hr.json @@ -119,6 +119,9 @@ "empty-placeholder": "Prazan", "unlink": "Prekini vezu" }, + "locale-picker": { + "no-language-feedback": "Jezik nije pronađen? Pošaljite povratne informacije" + }, "editor": { "add-your-comment-here": "Dodaj svoj komentar ovdje...", "any-thoughts": "Imaš li misli...", diff --git a/web/src/locales/hu.json b/web/src/locales/hu.json index 55bc8932..f0a4b415 100644 --- a/web/src/locales/hu.json +++ b/web/src/locales/hu.json @@ -119,6 +119,9 @@ "empty-placeholder": "Üres", "unlink": "Leválasztás" }, + "locale-picker": { + "no-language-feedback": "Nem található a nyelv? Visszajelzés küldése" + }, "editor": { "add-your-comment-here": "Írd ide a megjegyzésed...", "any-thoughts": "Bármi ami a fejedben jár...", diff --git a/web/src/locales/id.json b/web/src/locales/id.json index 96e45b1a..6f518be4 100644 --- a/web/src/locales/id.json +++ b/web/src/locales/id.json @@ -116,6 +116,9 @@ "visibility": "Visibilitas", "yourself": "Anda sendiri" }, + "locale-picker": { + "no-language-feedback": "Bahasa tidak ditemukan? Kirim masukan" + }, "editor": { "add-your-comment-here": "Tambahkan komentar Anda di sini...", "any-thoughts": "Punya pemikiran...", diff --git a/web/src/locales/it.json b/web/src/locales/it.json index 64cd70d7..034aae94 100644 --- a/web/src/locales/it.json +++ b/web/src/locales/it.json @@ -119,6 +119,9 @@ "empty-placeholder": "Vuota", "unlink": "Scollega" }, + "locale-picker": { + "no-language-feedback": "Lingua non trovata? Invia feedback" + }, "editor": { "add-your-comment-here": "Aggiungi qui il tuo commento...", "any-thoughts": "Qualsiasi cosa pensi...", diff --git a/web/src/locales/ja.json b/web/src/locales/ja.json index 7d23ae42..ee391bd9 100644 --- a/web/src/locales/ja.json +++ b/web/src/locales/ja.json @@ -119,6 +119,9 @@ "empty-placeholder": "空の", "unlink": "リンクを解除する" }, + "locale-picker": { + "no-language-feedback": "言語が見つかりませんか? フィードバックを送信する" + }, "editor": { "add-your-comment-here": "ここにコメントを追加...", "any-thoughts": "今思ったことは...", diff --git a/web/src/locales/ka-GE.json b/web/src/locales/ka-GE.json index 7a28b7ae..9c472aca 100644 --- a/web/src/locales/ka-GE.json +++ b/web/src/locales/ka-GE.json @@ -119,6 +119,9 @@ "empty-placeholder": "ცარიელი", "unlink": "გაუქმება" }, + "locale-picker": { + "no-language-feedback": "ენა ვერ მოიძებნა? გამოხმაურება" + }, "editor": { "add-your-comment-here": "დაამატეთ თქვენი კომენტარი აქ...", "any-thoughts": "რამე იდეები...", diff --git a/web/src/locales/ko.json b/web/src/locales/ko.json index 0af3e72b..5818a94b 100644 --- a/web/src/locales/ko.json +++ b/web/src/locales/ko.json @@ -116,6 +116,9 @@ "visibility": "공개 범위", "yourself": "자기 자신" }, + "locale-picker": { + "no-language-feedback": "언어를 찾을 수 없나요? 피드백 제출" + }, "editor": { "add-your-comment-here": "여기에 댓글을 추가하세요...", "any-thoughts": "떠오르는 게 있나요...", diff --git a/web/src/locales/lt.json b/web/src/locales/lt.json index 6948677b..c0e8ff3a 100644 --- a/web/src/locales/lt.json +++ b/web/src/locales/lt.json @@ -156,6 +156,9 @@ "visibility": "Matomumas", "yourself": "save" }, + "locale-picker": { + "no-language-feedback": "Kalba nerasta? Pateikite atsiliepimą" + }, "editor": { "add-your-comment-here": "Pridėkite savo komentarą čia...", "any-thoughts": "Bet kokios mintys...", diff --git a/web/src/locales/lv.json b/web/src/locales/lv.json index 0e4f49e0..23ac6889 100644 --- a/web/src/locales/lv.json +++ b/web/src/locales/lv.json @@ -156,6 +156,9 @@ "visibility": "Redzamība", "yourself": "sevi" }, + "locale-picker": { + "no-language-feedback": "Valoda nav atrasta? Iesniedziet atsauksmes" + }, "editor": { "add-your-comment-here": "Pievienojiet savu komentāru šeit...", "any-thoughts": "Jebkuras domas...", diff --git a/web/src/locales/mr.json b/web/src/locales/mr.json index d7d5d740..f3507f3a 100644 --- a/web/src/locales/mr.json +++ b/web/src/locales/mr.json @@ -116,6 +116,9 @@ "visibility": "दृश्यमानता", "yourself": "स्वतः" }, + "locale-picker": { + "no-language-feedback": "भाषा सापडली नाही? अभिप्राय सबमिट करा" + }, "editor": { "add-your-comment-here": "तुमची टिप्पणी येथे जो़डा", "any-thoughts": "आपले विचार...", diff --git a/web/src/locales/nb.json b/web/src/locales/nb.json index a7e5646d..c1c70d73 100644 --- a/web/src/locales/nb.json +++ b/web/src/locales/nb.json @@ -119,6 +119,9 @@ "empty-placeholder": "Tømme", "unlink": "Fjern tilknytningen" }, + "locale-picker": { + "no-language-feedback": "Finner du ikke språket? Send tilbakemelding" + }, "editor": { "add-your-comment-here": "Legg til din kommentar her...", "any-thoughts": "Noen tanker...", diff --git a/web/src/locales/nl.json b/web/src/locales/nl.json index 8ddd4760..03f822f6 100644 --- a/web/src/locales/nl.json +++ b/web/src/locales/nl.json @@ -119,6 +119,9 @@ "empty-placeholder": "Leeg", "unlink": "Ontkoppelen" }, + "locale-picker": { + "no-language-feedback": "Taal niet gevonden? Geef feedback" + }, "editor": { "add-your-comment-here": "Voeg hier je opmerking toe…", "any-thoughts": "Enkele gedachten…", diff --git a/web/src/locales/pl.json b/web/src/locales/pl.json index 03902528..b524f80d 100644 --- a/web/src/locales/pl.json +++ b/web/src/locales/pl.json @@ -119,6 +119,9 @@ "empty-placeholder": "Pusty", "unlink": "Odczepić" }, + "locale-picker": { + "no-language-feedback": "Nie znaleziono języka? Prześlij opinię" + }, "editor": { "add-your-comment-here": "Dodaj swój komentarz tutaj...", "any-thoughts": "Jakieś przemyślenia...", diff --git a/web/src/locales/pt-BR.json b/web/src/locales/pt-BR.json index 622486cd..b4a4c247 100644 --- a/web/src/locales/pt-BR.json +++ b/web/src/locales/pt-BR.json @@ -116,6 +116,9 @@ "visibility": "Visibilidade", "yourself": "Você mesmo" }, + "locale-picker": { + "no-language-feedback": "Idioma não encontrado? Enviar comentários" + }, "editor": { "add-your-comment-here": "Adicione seu comentário aqui…", "any-thoughts": "Alguma ideia…", diff --git a/web/src/locales/pt-PT.json b/web/src/locales/pt-PT.json index 9f06a7cb..4a7ba890 100644 --- a/web/src/locales/pt-PT.json +++ b/web/src/locales/pt-PT.json @@ -119,6 +119,9 @@ "empty-placeholder": "Vazia", "unlink": "Desvincular" }, + "locale-picker": { + "no-language-feedback": "Idioma não encontrado? Enviar comentários" + }, "editor": { "add-your-comment-here": "Adicione o seu comentário aqui...", "any-thoughts": "Alguma ideia…", diff --git a/web/src/locales/ro.json b/web/src/locales/ro.json index f307d731..a3ec6086 100644 --- a/web/src/locales/ro.json +++ b/web/src/locales/ro.json @@ -156,6 +156,9 @@ "visibility": "Vizibilitate", "yourself": "Te" }, + "locale-picker": { + "no-language-feedback": "Limba nu a fost găsită? Trimiteți feedback" + }, "editor": { "add-your-comment-here": "Adauga comentariul tau aici...", "any-thoughts": "Orice gânduri...", diff --git a/web/src/locales/ru.json b/web/src/locales/ru.json index 8eea6498..02d9d379 100644 --- a/web/src/locales/ru.json +++ b/web/src/locales/ru.json @@ -119,6 +119,9 @@ "empty-placeholder": "Пустой", "unlink": "Отсоединить" }, + "locale-picker": { + "no-language-feedback": "Язык не найден? Отправить отзыв" + }, "editor": { "add-your-comment-here": "Напишите свой комментарий...", "any-thoughts": "Напишите что-нибудь...", diff --git a/web/src/locales/sk.json b/web/src/locales/sk.json index ea997ba0..72ec9fd6 100644 --- a/web/src/locales/sk.json +++ b/web/src/locales/sk.json @@ -156,6 +156,9 @@ "visibility": "Viditeľnosť", "yourself": "seba" }, + "locale-picker": { + "no-language-feedback": "Jazyk sa nenašiel? Odoslať spätnú väzbu" + }, "editor": { "add-your-comment-here": "Tu pridajte svoj komentár...", "any-thoughts": "Akékoľvek myšlienky...", diff --git a/web/src/locales/sl.json b/web/src/locales/sl.json index b13ec0e5..3caa3845 100644 --- a/web/src/locales/sl.json +++ b/web/src/locales/sl.json @@ -119,6 +119,9 @@ "empty-placeholder": "prazno", "unlink": "Prekini povezavo" }, + "locale-picker": { + "no-language-feedback": "Jezik ni bil najden? Pošlji povratne informacije" + }, "editor": { "add-your-comment-here": "Tu dodaj svoj komentar...", "any-thoughts": "Kakšne misli...", diff --git a/web/src/locales/sr.json b/web/src/locales/sr.json index bbf7ac20..9011322b 100644 --- a/web/src/locales/sr.json +++ b/web/src/locales/sr.json @@ -156,6 +156,9 @@ "visibility": "Видљивост", "yourself": "Себе" }, + "locale-picker": { + "no-language-feedback": "Језик није пронађен? Пошаљите повратне информације" + }, "editor": { "add-your-comment-here": "Додајте свој коментар овде...", "any-thoughts": "Било каква размишљања...", diff --git a/web/src/locales/sv.json b/web/src/locales/sv.json index 6dc10f15..00a7f219 100644 --- a/web/src/locales/sv.json +++ b/web/src/locales/sv.json @@ -119,6 +119,9 @@ "empty-placeholder": "Tömma", "unlink": "Ta bort länken" }, + "locale-picker": { + "no-language-feedback": "Språket hittades inte? Skicka feedback" + }, "editor": { "add-your-comment-here": "Lägg till din kommentar här...", "any-thoughts": "Några tankar...", diff --git a/web/src/locales/th.json b/web/src/locales/th.json index 15dbe5dd..409c11b2 100644 --- a/web/src/locales/th.json +++ b/web/src/locales/th.json @@ -116,6 +116,9 @@ "visibility": "การมองเห็น", "yourself": "ตัวคุณ" }, + "locale-picker": { + "no-language-feedback": "ไม่พบภาษา? ส่งข้อเสนอแนะ" + }, "editor": { "add-your-comment-here": "เพิ่มความคิดเห็นของคุณที่นี่...", "any-thoughts": "ความคิดใดๆ...", diff --git a/web/src/locales/tr.json b/web/src/locales/tr.json index 78e4bd49..95dc3f26 100644 --- a/web/src/locales/tr.json +++ b/web/src/locales/tr.json @@ -119,6 +119,9 @@ "empty-placeholder": "Boş", "unlink": "Bağlantıyı kaldır" }, + "locale-picker": { + "no-language-feedback": "Dil bulunamadı mı? Geri bildirim gönder" + }, "editor": { "add-your-comment-here": "Yorumunuzu buraya ekleyin...", "any-thoughts": "Düşünceleriniz...", diff --git a/web/src/locales/uk.json b/web/src/locales/uk.json index bc427399..44555347 100644 --- a/web/src/locales/uk.json +++ b/web/src/locales/uk.json @@ -119,6 +119,9 @@ "empty-placeholder": "Порожній", "unlink": "Від’єднати" }, + "locale-picker": { + "no-language-feedback": "Мова не знайдена? Надіслати відгук" + }, "editor": { "add-your-comment-here": "Додайте свій коментар тут...", "any-thoughts": "Якісь думки...", diff --git a/web/src/locales/vi.json b/web/src/locales/vi.json index d531c4f8..ced9b3b5 100644 --- a/web/src/locales/vi.json +++ b/web/src/locales/vi.json @@ -116,6 +116,9 @@ "visibility": "Quyền xem", "yourself": "Bản thân" }, + "locale-picker": { + "no-language-feedback": "Không tìm thấy ngôn ngữ? Gửi phản hồi" + }, "editor": { "add-your-comment-here": "Thêm bình luận của bạn tại đây...", "any-thoughts": "Có suy nghĩ gì...", diff --git a/web/src/locales/zh-Hans.json b/web/src/locales/zh-Hans.json index de845650..789b7813 100644 --- a/web/src/locales/zh-Hans.json +++ b/web/src/locales/zh-Hans.json @@ -123,6 +123,9 @@ "visibility": "可见性", "yourself": "您自己" }, + "locale-picker": { + "no-language-feedback": "未找到语言? 提交反馈" + }, "editor": { "add-your-comment-here": "请输入您的评论...", "any-thoughts": "此刻的想法...", diff --git a/web/src/locales/zh-Hant.json b/web/src/locales/zh-Hant.json index 266f3d88..bc3eaaa3 100644 --- a/web/src/locales/zh-Hant.json +++ b/web/src/locales/zh-Hant.json @@ -156,6 +156,9 @@ "visibility": "瀏覽權限", "yourself": "您自己" }, + "locale-picker": { + "no-language-feedback": "未找到語言? 提交回饋" + }, "editor": { "add-your-comment-here": "在這裡添加您的評論...", "any-thoughts": "任何想法...", diff --git a/web/src/utils/i18n.ts b/web/src/utils/i18n.ts index b96421a2..4b1090e3 100644 --- a/web/src/utils/i18n.ts +++ b/web/src/utils/i18n.ts @@ -122,3 +122,36 @@ export const getLocaleDisplayName = (locale: string): string => { } return locale; }; + +export const normalizeLocaleSearchText = (value: string): string => { + return value + .normalize("NFD") + .replace(/\p{Diacritic}/gu, "") + .toLowerCase(); +}; + +const getLocaleDisplayNameForLanguage = (locale: string, displayLanguage: string): string => { + try { + return new Intl.DisplayNames([displayLanguage], { type: "language" }).of(locale) ?? locale; + } catch { + return locale; + } +}; + +export const getLocaleSearchLabels = (locale: string, uiLocale: string): string[] => { + return Array.from( + new Set([ + locale, + getLocaleDisplayNameForLanguage(locale, locale), + getLocaleDisplayNameForLanguage(locale, "en"), + getLocaleDisplayNameForLanguage(locale, uiLocale), + ]), + ); +}; + +export const localeMatchesSearch = (locale: string, query: string, uiLocale: string): boolean => { + const normalizedQuery = normalizeLocaleSearchText(query.trim()); + if (!normalizedQuery) return true; + + return getLocaleSearchLabels(locale, uiLocale).some((label) => normalizeLocaleSearchText(label).includes(normalizedQuery)); +}; diff --git a/web/tests/i18n-locale-search.test.ts b/web/tests/i18n-locale-search.test.ts new file mode 100644 index 00000000..74ba2202 --- /dev/null +++ b/web/tests/i18n-locale-search.test.ts @@ -0,0 +1,24 @@ +import { describe, expect, it } from "vitest"; +import { getLocaleSearchLabels, localeMatchesSearch, normalizeLocaleSearchText } from "@/utils/i18n"; + +describe("locale search helpers", () => { + it("normalizes case and diacritics for locale search", () => { + expect(normalizeLocaleSearchText("Português")).toBe("portugues"); + }); + + it("includes locale code, native name, and English name", () => { + const labels = getLocaleSearchLabels("ja", "en"); + + expect(labels).toContain("ja"); + expect(labels).toContain("日本語"); + expect(labels).toContain("Japanese"); + }); + + it("matches by code, native display name, English display name, and accent-free text", () => { + expect(localeMatchesSearch("pt-PT", "pt", "en")).toBe(true); + expect(localeMatchesSearch("ja", "日本", "en")).toBe(true); + expect(localeMatchesSearch("de", "german", "en")).toBe(true); + expect(localeMatchesSearch("pt-PT", "portugues", "en")).toBe(true); + expect(localeMatchesSearch("ja", "romanian", "en")).toBe(false); + }); +}); diff --git a/web/tests/locale-picker.test.tsx b/web/tests/locale-picker.test.tsx new file mode 100644 index 00000000..75473475 --- /dev/null +++ b/web/tests/locale-picker.test.tsx @@ -0,0 +1,14 @@ +import { fireEvent, render, screen } from "@testing-library/react"; +import { describe, expect, it } from "vitest"; +import { LocaleSearchList } from "@/components/LocalePicker"; + +describe("", () => { + it("links to GitHub feedback when no locale matches the search", async () => { + render( {}} />); + + fireEvent.change(await screen.findByRole("textbox", { name: "Language" }), { target: { value: "klingon" } }); + + const feedbackLink = screen.getByRole("link", { name: "Language not found? Submit feedback" }); + expect(feedbackLink).toHaveAttribute("href", expect.stringContaining("https://github.com/usememos/memos/issues/new")); + }); +});