mirror of
https://github.com/darkwire/darkwire.io.git
synced 2025-07-18 18:54:52 +00:00
Auto detect language (#141)
* Auto-detect lang * Fix Chat test * Default language to empty string Co-authored-by: Alan Friedman <d.alan.friedman@gmail.com>
This commit is contained in:
parent
3d019ca924
commit
bd1bea20ed
@ -7,7 +7,7 @@ const sendEncryptedMessage = jest.fn()
|
|||||||
|
|
||||||
test('Chat Component', () => {
|
test('Chat Component', () => {
|
||||||
const component = mount(
|
const component = mount(
|
||||||
<Chat scrollToBottom={() => {}} focusChat={false} userId="foo" username="user" showNotice={() => {}} clearActivities={() => {}} sendEncryptedMessage={sendEncryptedMessage} />
|
<Chat scrollToBottom={() => {}} focusChat={false} userId="foo" username="user" showNotice={() => {}} clearActivities={() => {}} sendEncryptedMessage={sendEncryptedMessage} translations={{}}/>
|
||||||
)
|
)
|
||||||
|
|
||||||
const componentJSON = toJson(component)
|
const componentJSON = toJson(component)
|
||||||
|
9
client/src/i18n/i18n.test.js
Normal file
9
client/src/i18n/i18n.test.js
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
import { getTranslations } from './index.js'
|
||||||
|
|
||||||
|
test('Get translation', () => {
|
||||||
|
expect(getTranslations('en').welcomeHeader).toBe("Welcome to Darkwire v2.0");
|
||||||
|
expect(getTranslations('fr').welcomeHeader).toBe("Bienvenue sur Darkwire v2.0");
|
||||||
|
expect(getTranslations('zh-CN').welcomeHeader).toBe("欢迎来到Darkwire v2.0");
|
||||||
|
expect(getTranslations('en-US').welcomeHeader).toBe("Welcome to Darkwire v2.0");
|
||||||
|
expect(getTranslations('ru-CH').welcomeHeader).toBe("Welcome to Darkwire v2.0");
|
||||||
|
})
|
@ -16,6 +16,21 @@ const languagesMap = {
|
|||||||
nl
|
nl
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getTranslations(language) {
|
/**
|
||||||
return languagesMap[language];
|
* Return best match for lang and variant.
|
||||||
|
* @param {string} language string from navigator configuration or cookie.
|
||||||
|
* @returns the translation dict
|
||||||
|
*/
|
||||||
|
export function getTranslations(language = "") {
|
||||||
|
const [lang, variant] = language.split('-');
|
||||||
|
|
||||||
|
if (languagesMap.hasOwnProperty(`${lang}${variant}`)) {
|
||||||
|
return languagesMap[`${lang}${variant}`];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (languagesMap.hasOwnProperty(lang)) {
|
||||||
|
return languagesMap[lang];
|
||||||
|
}
|
||||||
|
|
||||||
|
return languagesMap['en'];
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import Cookie from 'js-cookie';
|
import Cookie from 'js-cookie';
|
||||||
import {getTranslations} from 'i18n';
|
import {getTranslations} from 'i18n';
|
||||||
|
|
||||||
const language = Cookie.get('language') || 'en';
|
const language = Cookie.get('language') || navigator.language || 'en';
|
||||||
|
|
||||||
const initialState = {
|
const initialState = {
|
||||||
modalComponent: null,
|
modalComponent: null,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user