This commit is contained in:
hyojun.choi 2024-08-23 16:39:38 +09:00
commit 9fa384db0d
5 changed files with 53 additions and 0 deletions

View File

@ -3,6 +3,7 @@
import { Button, Table, TableBody, TableCell, TableColumn, TableHeader, TableRow } from '@nextui-org/react' import { Button, Table, TableBody, TableCell, TableColumn, TableHeader, TableRow } from '@nextui-org/react'
import { useAxios } from '@/hooks/useAxios' import { useAxios } from '@/hooks/useAxios'
import { useMessage } from '@/hooks/useMessage'
// import { get } from '@/lib/Axios' // import { get } from '@/lib/Axios'
import QSelect from '@/components/ui/QSelect' import QSelect from '@/components/ui/QSelect'
@ -12,6 +13,7 @@ import styles from './playground.module.css'
export default function Playground() { export default function Playground() {
const { get } = useAxios() const { get } = useAxios()
const testVar = process.env.NEXT_PUBLIC_TEST const testVar = process.env.NEXT_PUBLIC_TEST
const { getMessage } = useMessage()
const handleUsers = async () => { const handleUsers = async () => {
// const users = await get('/api/user/find-all') // const users = await get('/api/user/find-all')
@ -58,6 +60,7 @@ export default function Playground() {
<div className="test"> <div className="test">
<p className="text-white">Sass 테스트입니다.</p> <p className="text-white">Sass 테스트입니다.</p>
</div> </div>
<div>{getMessage('hi')}</div>
</div> </div>
</> </>
) )

38
src/hooks/useMessage.js Normal file
View File

@ -0,0 +1,38 @@
import { useRecoilValue } from 'recoil'
import { globalLocaleState } from '@/store/localeAtom'
import KO from '@/locales/ko.json'
import JA from '@/locales/ja.json'
const SESSION_STORAGE_MESSAGE_KEY = 'QCAST_MESSAGE_STORAGE'
export const useMessage = () => {
const globalLocale = useRecoilValue(globalLocaleState)
const getMessage = (key, args = []) => {
if (sessionStorage.getItem(SESSION_STORAGE_MESSAGE_KEY) === null) {
if (globalLocale === 'ko') {
setSessionMessage(JSON.stringify(KO))
} else {
setSessionMessage(JSON.stringify(JA))
}
}
const sessionMessage = getSessionMessage()
const message = sessionMessage[key] || key
return args.reduce((acc, arg, i) => {
return acc.replaceAll(`{${i}}`, arg)
}, message)
}
const setSessionMessage = (sessionMessage) => {
sessionStorage.setItem(SESSION_STORAGE_MESSAGE_KEY, sessionMessage)
}
const getSessionMessage = () => {
return JSON.parse(sessionStorage.getItem(SESSION_STORAGE_MESSAGE_KEY))
}
return { getMessage }
}

3
src/locales/ja.json Normal file
View File

@ -0,0 +1,3 @@
{
"hi": "こんにちは"
}

3
src/locales/ko.json Normal file
View File

@ -0,0 +1,3 @@
{
"hi": "안녕하세요"
}

6
src/store/localeAtom.js Normal file
View File

@ -0,0 +1,6 @@
import { atom } from 'recoil'
export const globalLocaleState = atom({
key: 'globalLocaleState',
default: 'ko',
})