Merge pull request #4687 from h3poteto/iss-4653/locale

refs #4653 Add language settings
This commit is contained in:
AkiraFukushima 2023-12-05 20:42:15 +09:00 committed by GitHub
commit 90c765d78b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 138 additions and 26 deletions

View File

@ -21,9 +21,11 @@
"accounts": { "accounts": {
"new": { "new": {
"title": "Add account", "title": "Add account",
"domain": "Domain",
"sign_in": "Sign in", "sign_in": "Sign in",
"authorize": "Authorize" "authorize": "Authorize"
} },
"remove": "Remove"
}, },
"notification": { "notification": {
"favourite": { "favourite": {
@ -105,5 +107,9 @@
"timeline": "Timeline", "timeline": "Timeline",
"followers": "Followers", "followers": "Followers",
"followings": "Followings" "followings": "Followings"
},
"settings": {
"title": "Settings",
"language": "Language"
} }
} }

View File

@ -0,0 +1,64 @@
import { localeType } from '@/utils/i18n'
import { Label, Modal, Select } from 'flowbite-react'
import { ChangeEvent, useEffect, useState } from 'react'
import { FormattedMessage } from 'react-intl'
type Props = {
opened: boolean
close: () => void
reloadSettings: () => void
}
const languages = [
{
label: 'English',
value: 'en'
},
{
label: '日本語',
value: 'ja'
}
]
export default function Settings(props: Props) {
const [language, setLanguage] = useState<localeType>('en')
useEffect(() => {
if (typeof localStorage !== 'undefined') {
const lang = localStorage.getItem('language')
setLanguage(lang as localeType)
}
}, [])
const languageChanged = (e: ChangeEvent<HTMLSelectElement>) => {
setLanguage(e.target.value as localeType)
if (typeof localStorage !== 'undefined') {
localStorage.setItem('language', e.target.value)
}
props.reloadSettings()
}
return (
<Modal show={props.opened} onClose={props.close}>
<Modal.Header>
<FormattedMessage id="settings.title" />
</Modal.Header>
<Modal.Body>
<div className="mb-2">
<Label htmlFor="language">
<FormattedMessage id="settings.language" />
</Label>
</div>
<div>
<Select id="language" onChange={languageChanged} defaultValue={language}>
{languages.map(lang => (
<option key={lang.value} value={lang.value}>
{lang.label}
</option>
))}
</Select>
</div>
</Modal.Body>
</Modal>
)
}

View File

@ -72,7 +72,9 @@ export default function New(props: NewProps) {
{sns === null && ( {sns === null && (
<> <>
<div className="block"> <div className="block">
<Label htmlFor="domain" value="Domain" /> <Label htmlFor="domain">
<FormattedMessage id="accounts.new.domain" />
</Label>
</div> </div>
<TextInput id="domain" placeholder="mastodon.social" required type="text" /> <TextInput id="domain" placeholder="mastodon.social" required type="text" />
<Button onClick={checkDomain}> <Button onClick={checkDomain}>

View File

@ -1,12 +1,14 @@
import { useEffect, useRef, useState } from 'react' import { useContext, useEffect, useRef, useState } from 'react'
import { FaPlus } from 'react-icons/fa6' import { FaGear, FaPlus } from 'react-icons/fa6'
import { Account, db } from '@/db' import { Account, db } from '@/db'
import NewAccount from '@/components/accounts/New' import NewAccount from '@/components/accounts/New'
import Settings from '@/components/Settings'
import { Avatar, Dropdown } from 'flowbite-react' import { Avatar, Dropdown } from 'flowbite-react'
import { useRouter } from 'next/router' import { useRouter } from 'next/router'
import { useIntl } from 'react-intl' import { FormattedMessage, useIntl } from 'react-intl'
import generateNotification from '@/utils/notification' import generateNotification from '@/utils/notification'
import generator, { Entity, WebSocketInterface } from 'megalodon' import generator, { Entity, WebSocketInterface } from 'megalodon'
import { Context } from '@/utils/i18n'
type LayoutProps = { type LayoutProps = {
children: React.ReactNode children: React.ReactNode
@ -15,11 +17,14 @@ type LayoutProps = {
export default function Layout({ children }: LayoutProps) { export default function Layout({ children }: LayoutProps) {
const [accounts, setAccounts] = useState<Array<Account>>([]) const [accounts, setAccounts] = useState<Array<Account>>([])
const [openNewModal, setOpenNewModal] = useState(false) const [openNewModal, setOpenNewModal] = useState(false)
const [openSettings, setOpenSettings] = useState(false)
const { switchLang } = useContext(Context)
const router = useRouter() const router = useRouter()
const { formatMessage } = useIntl() const { formatMessage } = useIntl()
const streamings = useRef<Array<WebSocketInterface>>([]) const streamings = useRef<Array<WebSocketInterface>>([])
useEffect(() => { useEffect(() => {
loadSettings()
const fn = async () => { const fn = async () => {
const acct = await db.accounts.toArray() const acct = await db.accounts.toArray()
setAccounts(acct) setAccounts(acct)
@ -95,32 +100,62 @@ export default function Layout({ children }: LayoutProps) {
} }
} }
const loadSettings = () => {
if (typeof localStorage !== 'undefined') {
const lang = localStorage.getItem('language')
switchLang(lang)
}
}
return ( return (
<div className="app flex flex-col min-h-screen"> <div className="app flex flex-col min-h-screen">
<main className="flex w-full box-border my-0 mx-auto min-h-screen"> <main className="flex w-full box-border my-0 mx-auto min-h-screen">
<aside className="w-16 bg-gray-900"> <aside className="w-16 bg-gray-900 flex flex-col justify-between">
{accounts.map(account => ( <div>
<div key={account.id} className={selectedClassName(account.id)}> {accounts.map(account => (
<Avatar <div key={account.id} className={selectedClassName(account.id)}>
alt={account.domain} <Avatar
img={account.avatar} alt={account.domain}
rounded img={account.avatar}
key={account.id} rounded
className="py-2" key={account.id}
onClick={() => openAccount(account.id)} className="py-2"
onContextMenu={() => openContextMenu(account.id)} onClick={() => openAccount(account.id)}
/> onContextMenu={() => openContextMenu(account.id)}
<Dropdown label="" dismissOnClick={true} renderTrigger={() => dropdownTrigger(account.id)}> />
<Dropdown.Item onClick={() => removeAccount(account.id)}>Remove</Dropdown.Item> <Dropdown label="" dismissOnClick={true} renderTrigger={() => dropdownTrigger(account.id)}>
<Dropdown.Item onClick={() => removeAccount(account.id)}>
<FormattedMessage id="accounts.remove" />
</Dropdown.Item>
</Dropdown>
</div>
))}
<button className="py-4 px-6 items-center" onClick={() => setOpenNewModal(true)}>
<FaPlus className="text-gray-400" />
</button>
<NewAccount opened={openNewModal} close={closeNewModal} />
</div>
<div className="settings text-gray-400 py-4 px-6 items-center">
<div className="relative cursor-pointer">
<Dropdown
label=""
dismissOnClick
renderTrigger={() => (
<span>
<FaGear />
</span>
)}
placement="right-start"
>
<Dropdown.Item onClick={() => setOpenSettings(true)}>
<FormattedMessage id="settings.title" />{' '}
</Dropdown.Item>
</Dropdown> </Dropdown>
</div> </div>
))} </div>
<button className="py-4 px-6 items-center" onClick={() => setOpenNewModal(true)}>
<FaPlus className="text-gray-400" />
</button>
<NewAccount opened={openNewModal} close={closeNewModal} />
</aside> </aside>
{children} {children}
<Settings opened={openSettings} close={() => setOpenSettings(false)} reloadSettings={loadSettings} />
</main> </main>
</div> </div>
) )

View File

@ -1,9 +1,10 @@
import en from '../../locales/en/translation.json' import en from '../../locales/en/translation.json'
import ja from '../../locales/ja/translation.json'
import { flattenMessages } from './flattenMessage' import { flattenMessages } from './flattenMessage'
import { createContext, useState } from 'react' import { createContext, useState } from 'react'
import { IntlProvider } from 'react-intl' import { IntlProvider } from 'react-intl'
export type localeType = 'en' | 'ja' | 'it' | 'pt-BR' | 'fr' export type localeType = 'en' | 'ja'
type Props = { type Props = {
children: React.ReactNode children: React.ReactNode
@ -16,11 +17,15 @@ interface Lang {
export const Context = createContext<Lang>({} as Lang) export const Context = createContext<Lang>({} as Lang)
export const IntlProviderWrapper: React.FC<Props> = props => { export const IntlProviderWrapper: React.FC<Props> = props => {
const langs = [{ locale: 'en', messages: flattenMessages(en) }] const langs = [
{ locale: 'en', messages: flattenMessages(en) },
{ locale: 'ja', messages: flattenMessages(ja) }
]
const [lang, setLang] = useState(langs[0]) const [lang, setLang] = useState(langs[0])
const switchLang = (locale: string) => { const switchLang = (locale: string) => {
const changeLang = langs.find(lang => lang.locale === locale) const changeLang = langs.find(lang => lang.locale === locale)
console.log(changeLang)
if (changeLang == null) { if (changeLang == null) {
return return
} }