mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-21 10:50:14 +01:00
feat: auto detect language (#947)
* added auto-detect language * fix(TranslationSelect) now saving the selected language between sessions * fix(LangSelector.spec) * fix(conflict) * fix(Swedish) sv-SE
This commit is contained in:
parent
2419af8748
commit
b48c618f32
12 changed files with 329 additions and 328 deletions
|
|
@ -11,6 +11,21 @@ import Japanese from './languages/Jp';
|
|||
import Swedish from './languages/Sv';
|
||||
// === import additional language files here === //
|
||||
|
||||
const languageMap: { [key: string]: unknown } = {
|
||||
'en-US': English,
|
||||
'zh-CN': Chinese,
|
||||
'de-DE': German,
|
||||
'es-ES': Spanish,
|
||||
'fr-FR': French,
|
||||
'it-IT': Italian,
|
||||
'pl-PL': Polish,
|
||||
'pt-BR': Portuguese,
|
||||
'ru-RU': Russian,
|
||||
'ja-JP': Japanese,
|
||||
'sv-SE': Swedish,
|
||||
// Add additional language mappings here
|
||||
};
|
||||
|
||||
// New method on String allow using "{\d}" placeholder for
|
||||
// loading value dynamically.
|
||||
declare global {
|
||||
|
|
@ -22,7 +37,7 @@ declare global {
|
|||
if (!String.prototype.format) {
|
||||
String.prototype.format = function (...args: string[]) {
|
||||
return this.replace(/{(\d+)}/g, function (match, number) {
|
||||
return typeof args[number] != 'undefined' ? args[number] : match;
|
||||
return typeof args[number] !== 'undefined' ? args[number] : match;
|
||||
});
|
||||
};
|
||||
}
|
||||
|
|
@ -30,57 +45,15 @@ if (!String.prototype.format) {
|
|||
// input: language code in string
|
||||
// returns an object of translated strings in the language
|
||||
export const getTranslations = (langCode: string) => {
|
||||
if (langCode === 'en') {
|
||||
return English;
|
||||
}
|
||||
if (langCode === 'cn') {
|
||||
return Chinese;
|
||||
}
|
||||
if (langCode === 'fr') {
|
||||
return French;
|
||||
}
|
||||
if (langCode === 'de') {
|
||||
return German;
|
||||
}
|
||||
if (langCode === 'it') {
|
||||
return Italian;
|
||||
}
|
||||
if (langCode === 'pl') {
|
||||
return Polish;
|
||||
}
|
||||
if (langCode === 'br') {
|
||||
return Portuguese;
|
||||
}
|
||||
if (langCode === 'es') {
|
||||
return Spanish;
|
||||
}
|
||||
if (langCode === 'ru') {
|
||||
return Russian;
|
||||
}
|
||||
if (langCode === 'jp') {
|
||||
return Japanese;
|
||||
}
|
||||
if (langCode === 'sv') {
|
||||
return Swedish;
|
||||
}
|
||||
|
||||
// === add conditionals here for additional languages here === //
|
||||
return English; // default to English
|
||||
const language = languageMap[langCode] || English;
|
||||
return language;
|
||||
};
|
||||
|
||||
// input: language code in string & phrase key in string
|
||||
// returns an corresponding phrase value in string
|
||||
export const localize = (langCode: string, phraseKey: string, ...values: string[]) => {
|
||||
const lang = getTranslations(langCode);
|
||||
if (phraseKey in lang) {
|
||||
return lang[phraseKey].format(...values);
|
||||
}
|
||||
const phrase = lang[phraseKey] || English[phraseKey] || '';
|
||||
|
||||
if (phraseKey in English) {
|
||||
// Fall back logic to cover untranslated phrases
|
||||
return English[phraseKey].format(...values);
|
||||
}
|
||||
|
||||
// In case the key is not defined, return empty instead of throw errors.
|
||||
return '';
|
||||
return phrase.format(...values);
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue