This repository has been archived by the owner on Aug 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the language setting page (#1210)
* Add initial language picker prototype * Heap o' improvements and Pirate tongue * Move .visually-hidden to shared utils and add copyright notice * Add a little space before categories names * Simplify search to input focus logic * Remove larger font size and padding from the search field * Some refactors * Braw's descent into madness Thanks web development! In seriousness though, tried to make the list more accessible. Making it fully accessible feels like unbearable task, so at least that. * Litol refactoring * Extract new strings and remove old ones * Update @vintl/nuxt to 1.3.0 This fixes the bug where default locale won't be saved. * A buncha refactorings and cleanup * Scuttle the Pirate lingo 'Twas employed 'ere for testin' purposes, but fear not, for it shall be returnin' in the days to come. Should ye require it fer testin', simply roll back this here commit. * Clean languages source file * Change "US" to "United States" I think it would make distinguishing two languages simpler as now there's more than one letter of difference (US/UK vs United States/ United Kingdom).
- Loading branch information
Showing
17 changed files
with
785 additions
and
10 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains 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 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,13 @@ | ||
export type AutoRef<T> = [T] extends [(...args: any[]) => any] | ||
? Ref<T> | (() => T) | ||
: T | Ref<T> | (() => T) | ||
|
||
/** | ||
* Accepts a value directly, a ref with the value or a getter function and returns a Vue ref. | ||
* @param value The value to use. | ||
* @returns Either the original or newly created ref. | ||
*/ | ||
export function useAutoRef<T>(value: AutoRef<T>): Ref<T> { | ||
if (typeof value === 'function') return computed(() => value()) | ||
return isRef(value) ? value : ref(value as any) | ||
} |
This file contains 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,91 @@ | ||
import { useAutoRef, type AutoRef } from './auto-ref.ts' | ||
|
||
const safeTags = new Map<string, string>() | ||
|
||
function safeTagFor(locale: string) { | ||
let safeTag = safeTags.get(locale) | ||
if (safeTag == null) { | ||
safeTag = new Intl.Locale(locale).baseName | ||
safeTags.set(locale, safeTag) | ||
} | ||
return safeTag | ||
} | ||
|
||
type DisplayNamesWrapper = Intl.DisplayNames & { | ||
of(tag: string): string | undefined | ||
} | ||
|
||
const displayNamesDicts = new Map<string, DisplayNamesWrapper>() | ||
|
||
function getWrapperKey(locale: string, options: Intl.DisplayNamesOptions) { | ||
return JSON.stringify({ ...options, locale }) | ||
} | ||
|
||
export function createDisplayNames( | ||
locale: string, | ||
options: Intl.DisplayNamesOptions = { type: 'language' } | ||
) { | ||
const wrapperKey = getWrapperKey(locale, options) | ||
let wrapper = displayNamesDicts.get(wrapperKey) | ||
|
||
if (wrapper == null) { | ||
const dict = new Intl.DisplayNames(locale, options) | ||
|
||
const badTags: string[] = [] | ||
|
||
wrapper = { | ||
resolvedOptions() { | ||
return dict.resolvedOptions() | ||
}, | ||
of(tag: string) { | ||
let attempt = 0 | ||
|
||
// eslint-disable-next-line no-labels | ||
lookupLoop: do { | ||
let lookup: string | ||
switch (attempt) { | ||
case 0: | ||
lookup = tag | ||
break | ||
case 1: | ||
lookup = safeTagFor(tag) | ||
break | ||
default: | ||
// eslint-disable-next-line no-labels | ||
break lookupLoop | ||
} | ||
|
||
if (badTags.includes(lookup)) continue | ||
|
||
try { | ||
return dict.of(lookup) | ||
} catch (err) { | ||
console.warn( | ||
`Failed to get display name for ${lookup} using dictionary for ${ | ||
this.resolvedOptions().locale | ||
}` | ||
) | ||
badTags.push(lookup) | ||
continue | ||
} | ||
} while (++attempt < 5) | ||
|
||
return undefined | ||
}, | ||
} | ||
|
||
displayNamesDicts.set(wrapperKey, wrapper) | ||
} | ||
|
||
return wrapper | ||
} | ||
|
||
export function useDisplayNames( | ||
locale: AutoRef<string>, | ||
options?: AutoRef<Intl.DisplayNamesOptions | undefined> | ||
) { | ||
const $locale = useAutoRef(locale) | ||
const $options = useAutoRef(options) | ||
|
||
return computed(() => createDisplayNames($locale.value, $options.value)) | ||
} |
This file contains 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,10 @@ | ||
/** | ||
* Checks if any of the modifier keys is down for the event. | ||
* @param e Event that is triggered with the state of modified keys. | ||
* @returns Whether any of the modifier keys is pressed. | ||
*/ | ||
export function isModifierKeyDown( | ||
e: Pick<KeyboardEvent, 'ctrlKey' | 'altKey' | 'metaKey' | 'shiftKey'> | ||
) { | ||
return e.ctrlKey || e.altKey || e.metaKey || e.shiftKey | ||
} |
This file contains 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 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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
{ | ||
"en-US": "American English" | ||
"en-US": "English (United States)" | ||
} |
This file contains 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 |
---|---|---|
@@ -1,6 +1,10 @@ | ||
{ | ||
"displayName": { | ||
"description": "The name of the language in dialect form (e.g. Français canadien for French spoken in Canada, not French (Canada))", | ||
"message": "American English" | ||
"description": "Please enter the name of the language in its specific variant or regional form (e.g., English (US) for American English, not just English). If the language does not have any specific variant, simply enter the name of the language (e.g., Français, Deutsch).", | ||
"message": "English (United States)" | ||
}, | ||
"searchTerms": { | ||
"description": "Please provide additional search terms associated with the language, if needed, to enhance the search functionality (e.g., American English, Deutschland). Each search term should be entered on a separate line. Translate as a hyphen (-) if no additional terms are needed.", | ||
"message": "USA\nAmerican English" | ||
} | ||
} |
This file contains 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 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 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
Oops, something went wrong.