Repository navigation
feat(client): add i18n that follows the Devframe hub language #1158
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
677e8fe
chore: set up x18n to check the DevTools client translations
antfubot 22265fe
feat(client): add i18n that follows the Devframe hub language
antfubot 4a3dacd
refactor(client): drop the DevTools language setting, leave it to Dev…
antfubot 57651bf
fix(client): read the host locale from __connection.json, survive blo…
antfubot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| import type { DevToolsRpcClient } from '@vitejs/devtools-kit/client' | ||
| import type { ComputedRef, Ref } from 'vue' | ||
| import type { DevToolsLocale } from '../i18n/locales' | ||
| import { matchLocale } from '@devframes/hub' | ||
| import { useLocalStorage, useNavigatorLanguage } from '@vueuse/core' | ||
| import { computed, ref, shallowRef, watch } from 'vue' | ||
| import { DEFAULT_LOCALE, DEVFRAME_LOCALE_STORAGE_KEY, DEVTOOLS_LOCALES, LOCALE_AUTO } from '../i18n/locales' | ||
| import { rpcClient } from './rpc' | ||
|
|
||
| const available = Object.keys(DEVTOOLS_LOCALES) as DevToolsLocale[] | ||
|
|
||
| /** Closest supported DevTools locale for a BCP 47 tag, using Devframe's matching rules. */ | ||
| export function matchDevToolsLocale(tag: string | undefined | null): DevToolsLocale | undefined { | ||
| return matchLocale(tag ?? undefined, available) as DevToolsLocale | undefined | ||
| } | ||
|
|
||
| /** | ||
| * The visitor's language pick, owned by the Devframe hub UI (Settings → | ||
| * Appearance): `'auto'` or a locale tag. DevTools only reads the hub's own | ||
| * `localStorage` key, so a change in the hub reaches DevTools live via the | ||
| * `storage` event. | ||
| */ | ||
| function useLocalePreference(): Ref<string> { | ||
| try { | ||
| return useLocalStorage<string>(DEVFRAME_LOCALE_STORAGE_KEY, LOCALE_AUTO, { | ||
| // Never write: an absent key already means `auto` to the hub. | ||
| writeDefaults: false, | ||
| listenToStorageChanges: true, | ||
| }) | ||
| } | ||
| catch { | ||
| // Reading `window.localStorage` throws when the browser blocks storage. | ||
| // Fall back to `auto` rather than failing the whole client at startup. | ||
| return ref(LOCALE_AUTO) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * The host's default language, as the server published it in | ||
| * `__connection.json` (`createUi({ locale })` → `configs.ui.locale`). | ||
| * | ||
| * The live `client.connectionMeta` can't be used for this: the running hub UI | ||
| * overwrites `configs.ui.locale` there with the language it resolved, and an | ||
| * embedded frame shares the hub's connection object (devframe reads it from | ||
| * the parent window). Fetching the descriptor again gives the untouched value. | ||
| */ | ||
| async function fetchHostDefault(client: DevToolsRpcClient): Promise<DevToolsLocale | undefined> { | ||
| try { | ||
| const response = await fetch(client.connection.metaBaseUrl) | ||
| if (!response.ok) | ||
| return undefined | ||
| const meta = await response.json() as { configs?: { ui?: { locale?: string } } } | ||
| return matchDevToolsLocale(meta.configs?.ui?.locale) | ||
| } | ||
| catch { | ||
| return undefined | ||
| } | ||
| } | ||
|
|
||
| let resolved: ComputedRef<DevToolsLocale> | undefined | ||
|
|
||
| /** | ||
| * The language DevTools renders in, resolved the same way the Devframe hub UI | ||
| * resolves its own: | ||
| * | ||
| * 1. the visitor's pick in the hub settings (unless `auto`); | ||
| * 2. the host's default, `configs.ui.locale` in `__connection.json`; | ||
| * 3. the browser language; | ||
| * 4. English. | ||
| */ | ||
| export function useDevToolsLocale(): ComputedRef<DevToolsLocale> { | ||
| if (resolved) | ||
| return resolved | ||
|
|
||
| const pick = useLocalePreference() | ||
| const { language } = useNavigatorLanguage() | ||
|
|
||
| const hostDefault = shallowRef<DevToolsLocale>() | ||
| let hostDefaultRequested = false | ||
| watch(rpcClient, async (client) => { | ||
| if (!client || hostDefaultRequested) | ||
| return | ||
| hostDefaultRequested = true | ||
| hostDefault.value = await fetchHostDefault(client) | ||
| }, { immediate: true }) | ||
|
|
||
| resolved = computed<DevToolsLocale>(() => { | ||
| if (pick.value && pick.value !== LOCALE_AUTO) { | ||
| const matched = matchDevToolsLocale(pick.value) | ||
| if (matched) | ||
| return matched | ||
| } | ||
|
|
||
| if (hostDefault.value) | ||
| return hostDefault.value | ||
|
|
||
| // Read `language` unconditionally so the `languagechange` event | ||
| // re-evaluates this, then prefer the full `navigator.languages` list. | ||
| const primary = language.value | ||
| const languages = typeof navigator !== 'undefined' && navigator.languages?.length | ||
| ? navigator.languages | ||
| : [primary] | ||
| for (const tag of languages) { | ||
| const matched = matchDevToolsLocale(tag) | ||
| if (matched) | ||
| return matched | ||
| } | ||
|
|
||
| return DEFAULT_LOCALE | ||
| }) | ||
| return resolved | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| import type { DevToolsLocale } from './locales' | ||
| import { createI18n } from 'vue-i18n' | ||
| import de from '../locales/de.json' | ||
| import en from '../locales/en.json' | ||
| import es from '../locales/es.json' | ||
| import fr from '../locales/fr.json' | ||
| import ja from '../locales/ja.json' | ||
| import ko from '../locales/ko.json' | ||
| import ptBR from '../locales/pt-BR.json' | ||
| import ru from '../locales/ru.json' | ||
| import zhCN from '../locales/zh-CN.json' | ||
| import zhTW from '../locales/zh-TW.json' | ||
| import { DEFAULT_LOCALE } from './locales' | ||
|
|
||
| export type MessageSchema = typeof en | ||
|
|
||
| const messages: Record<DevToolsLocale, MessageSchema> = { | ||
| 'en': en, | ||
| 'zh-CN': zhCN, | ||
| 'zh-TW': zhTW, | ||
| 'ja': ja, | ||
| 'ko': ko, | ||
| 'es': es, | ||
| 'fr': fr, | ||
| 'de': de, | ||
| 'pt-BR': ptBR, | ||
| 'ru': ru, | ||
| } | ||
|
|
||
| /** | ||
| * Russian has three plural forms: `one | few | many` | ||
| * (1 страница, 2 страницы, 5 страниц). | ||
| */ | ||
| function russianPlural(choice: number, choicesLength: number): number { | ||
| if (choicesLength < 3) { | ||
| return choice === 1 | ||
| ? 0 | ||
| : 1 | ||
| } | ||
| const mod10 = choice % 10 | ||
| const mod100 = choice % 100 | ||
| if (mod10 === 1 && mod100 !== 11) | ||
| return 0 | ||
| if (mod10 >= 2 && mod10 <= 4 && (mod100 < 10 || mod100 >= 20)) | ||
| return 1 | ||
| return 2 | ||
| } | ||
|
|
||
| /** | ||
| * The single i18n instance of the DevTools client. Exported so composables | ||
| * running outside a component (tab lists, commands) can translate with | ||
| * `i18n.global.t`, which stays reactive to locale changes. | ||
| */ | ||
| export const i18n = createI18n<[MessageSchema], DevToolsLocale, false>({ | ||
| legacy: false, | ||
| globalInjection: true, | ||
| locale: DEFAULT_LOCALE, | ||
| fallbackLocale: DEFAULT_LOCALE, | ||
| messages, | ||
| pluralRules: { | ||
| ru: russianPlural, | ||
| }, | ||
| missingWarn: false, | ||
| fallbackWarn: false, | ||
| }) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Script executed:
Repository: nuxt/devtools
Length of output: 34732
Script executed:
Repository: nuxt/devtools
Length of output: 33842
Script executed:
Repository: nuxt/devtools
Length of output: 31379
Web query:
github nuxt devtools packages/devtools/client/composables/locale.ts useDevToolsLocale host locale __connection.jsonResult:
Script executed:
Repository: nuxt/devtools
Length of output: 13808
Script executed:
Repository: nuxt/devtools
Length of output: 13696
Retry host-locale resolution after a failed fetch.
hostDefaultRequestedbecomestruebefore the metadata request succeeds. IffetchHostDefaultencounters a transient error or non-OK response, Auto falls back to the browser language and no later RPC reconnect can refreshhostDefault, because the guard remains set. Make the guard represent a successful resolution, and trigger a new request when the RPC connection is replaced or re-established.