Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
chenpangpang
open-webui
Commits
df8d1dac
"vscode:/vscode.git/clone" did not exist on "1e2486af23078b1e145f7b3ce324b64d00a56f68"
Commit
df8d1dac
authored
Jun 29, 2024
by
Timothy J. Baek
Browse files
refac: browser language detection
parent
56137acd
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
12 additions
and
5 deletions
+12
-5
src/lib/utils/index.ts
src/lib/utils/index.ts
+7
-0
src/routes/+layout.svelte
src/routes/+layout.svelte
+5
-5
No files found.
src/lib/utils/index.ts
View file @
df8d1dac
...
@@ -746,3 +746,10 @@ export const extractFrontmatter = (content) => {
...
@@ -746,3 +746,10 @@ export const extractFrontmatter = (content) => {
return
frontmatter
;
return
frontmatter
;
};
};
// Function to determine the best matching language
export
const
bestMatchingLanguage
=
(
supportedLanguages
,
preferredLanguages
,
defaultLocale
)
=>
{
const
languages
=
supportedLanguages
.
map
((
lang
)
=>
lang
.
code
);
const
match
=
preferredLanguages
.
find
((
lang
)
=>
languages
.
includes
(
lang
));
return
match
||
defaultLocale
;
};
src/routes/+layout.svelte
View file @
df8d1dac
...
@@ -31,6 +31,7 @@
...
@@ -31,6 +31,7 @@
import { WEBUI_BASE_URL, WEBUI_HOSTNAME } from '$lib/constants';
import { WEBUI_BASE_URL, WEBUI_HOSTNAME } from '$lib/constants';
import i18n, { initI18n, getLanguages } from '$lib/i18n';
import i18n, { initI18n, getLanguages } from '$lib/i18n';
import { bestMatchingLanguage } from '$lib/utils';
setContext('i18n', i18n);
setContext('i18n', i18n);
...
@@ -92,12 +93,11 @@
...
@@ -92,12 +93,11 @@
// so `/error` can show something that's not `undefined`.
// so `/error` can show something that's not `undefined`.
const languages = await getLanguages();
const languages = await getLanguages();
const browserLanguages = navigator.languages
? navigator.languages
: [navigator.language || navigator.userLanguage];
const browserLanguage = navigator.languages
initI18n(bestMatchingLanguage(languages, browserLanguages, backendConfig.default_locale));
? navigator.languages[0]
: navigator.language || navigator.userLanguage;
initI18n(languages.includes(browserLanguage) ? browserLanguage : backendConfig?.default_locale);
if (backendConfig) {
if (backendConfig) {
// Save Backend Status to Store
// Save Backend Status to Store
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment