Skip to content
This repository has been archived by the owner on Feb 27, 2021. It is now read-only.

Commit

Permalink
fix: add i18n language dropdown
Browse files Browse the repository at this point in the history
  • Loading branch information
shanejonas committed Oct 1, 2019
1 parent 7ae9132 commit f5d29fd
Show file tree
Hide file tree
Showing 12 changed files with 233 additions and 16 deletions.
2 changes: 2 additions & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import useMultiGethStore from "./stores/useMultiGethStore";
import EthereumJSONRPC from "@etclabscore/ethereum-json-rpc";
import ETHJSONSpec from "@etclabscore/ethereum-json-rpc-specification/openrpc.json";
import { useTranslation } from "react-i18next";
import LanguageMenu from "./containers/LanguageMenu"

import { createBrowserHistory } from "history";
const history = createBrowserHistory();
Expand Down Expand Up @@ -173,6 +174,7 @@ function App(props: any) {
</Grid>
</Hidden>
<Grid item>
<LanguageMenu />
<Tooltip title={t("JSON-RPC API Documentation")}>
<IconButton
onClick={() =>
Expand Down
2 changes: 1 addition & 1 deletion src/components/BlockList/BlockList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function BlockList({ blocks }: any) {
</Link>
</TableCell>
<TableCell style={rightPaddingFix}>
<Typography>{hexToDate(b.timestamp)}</Typography>
<Typography>{t("Timestamp Date", { date: hexToDate(b.timestamp)})}</Typography>
</TableCell>
<TableCell style={rightPaddingFix}>
<Typography>{b.transactions.length}</Typography>
Expand Down
2 changes: 1 addition & 1 deletion src/components/BlockView/BlockView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function BlockView(props: any) {

<TableRow>
<TableCell>{t("Timestamp")}</TableCell>
<TableCell>{hexToDate(timestamp)}</TableCell>
<TableCell>{t("Timestamp Date", { date: hexToDate(timestamp)})}</TableCell>
</TableRow>

<TableRow>
Expand Down
2 changes: 1 addition & 1 deletion src/containers/Dashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const useState = React.useState;

const config = {
blockTime: 15, // seconds
blockHistoryLength: 100,
blockHistoryLength: 1,
chartHeight: 200,
chartWidth: 400,
};
Expand Down
44 changes: 44 additions & 0 deletions src/containers/LanguageMenu/LanguageMenu.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import * as React from "react";
import { IconButton, Menu, MenuItem, Tooltip, Button } from "@material-ui/core"; //tslint:disable-line
import { useTranslation } from "react-i18next";
import { supportedLanguages, reverseSupportedLanguages } from "../../i18n";

const LanguageMenu: React.FC = (props) => {
const { t, i18n } = useTranslation();
const [anchorEl, setAnchorEl] = React.useState<null | HTMLElement>(null);

const handleClick = (event: React.MouseEvent<HTMLButtonElement>) => {
setAnchorEl(event.currentTarget);
};

const handleMenuItemClick = (lang: string) => {
setAnchorEl(null);
// this forces language change for react + i18n react
i18n.changeLanguage(reverseSupportedLanguages[lang]);
};

const handleClose = () => {
setAnchorEl(null);
};

return (
<>
<Tooltip title={t("Change Language")}>
<Button onClick={handleClick}>{supportedLanguages[i18n.language]}</Button>
</Tooltip>
<Menu
id="simple-menu"
anchorEl={anchorEl}
keepMounted
open={Boolean(anchorEl)}
onClose={handleClose}
>
{Object.values(supportedLanguages).map((lang, i) => (
<MenuItem onClick={(event) => handleMenuItemClick(lang)}>{lang}</MenuItem>
))}
</Menu>
</>
)
};

export default LanguageMenu;
2 changes: 2 additions & 0 deletions src/containers/LanguageMenu/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import LanguageMenu from "./LanguageMenu";
export default LanguageMenu;
4 changes: 1 addition & 3 deletions src/helpers/hexToDate.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import moment from "moment";

const hexToDate = (hexTimestamp: string) => {
return moment(
new Date(parseInt(hexTimestamp, 16) * 1000).toISOString(),
).format("MMMM Do YYYY, h:mm:ss a");
return new Date(parseInt(hexTimestamp, 16) * 1000).toISOString();
};

export default hexToDate;
50 changes: 50 additions & 0 deletions src/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,62 @@ import i18n from "i18next";
import { initReactI18next } from "react-i18next";
import LanguageDetector from "i18next-browser-languagedetector";
import enJSON from "./translations/en";
import krJSON from "./translations/kr";
import cnJSON from "./translations/cn";
import moment from "moment";
import "moment/locale/ko";
import "moment/locale/zh-cn";
import "moment/locale/en-ca";
import i18next from "i18next";

interface IMap {
[key: string]: string;
}


const momentMap: IMap = {
"kr": "ko",
"cn": "zh-cn",
"en-US": "en-ca",
};

i18n
.use(LanguageDetector)
.use(initReactI18next)
.init({
resources: {
en: { translation: enJSON },
kr: { translation: krJSON },
cn: { translation: cnJSON },
},
interpolation: {
escapeValue: false,
format: (value, format, lng) => {
if (!lng) {
return;
}
if (format !== "date") {
return;
}
const ln = momentMap[lng];
const result = moment(value).locale(ln || "en").format("MMMM Do YYYY, h:mm:ss a");
return result as any;
},
},
});

export const reverseSupportedLanguages: IMap = {
"EN": "en-US", //tslint:disable-line
"ไธญๆ–‡": "cn", //tslint:disable-line
"ํ•œ๊ตญ์–ด": "kr", //tslint:disable-line
};

export const supportedLanguages: IMap = {
"en-US": "EN",
"cn": "ไธญๆ–‡",
"kr": "ํ•œ๊ตญ์–ด",
};

export const changeLanguage = (l: string) => {
return i18next.changeLanguage(l);
};
10 changes: 7 additions & 3 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import * as React from "react";
import * as ReactDOM from "react-dom";
import App from "./App";
import { I18nextProvider } from "react-i18next";
import i18n from "i18next";
import { ReusableProvider } from "reusable";
import "./i18n";

ReactDOM.render(
<ReusableProvider>
<App />
</ReusableProvider>,
<I18nextProvider i18n={i18n}>
<ReusableProvider>
<App />
</ReusableProvider>
</I18nextProvider>,
document.getElementById("root") as HTMLElement,
);
61 changes: 61 additions & 0 deletions src/translations/cn.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
export default {
// Moment integration
"Timestamp Date": "{{date, date}}",
// App Bar
"Explorer": "ๆต่งˆๅ™จ",
"Enter an Address, Transaction Hash or Block Number": "่พ“ๅ…ฅๅœฐๅ€ ไบคๆ˜“ๅ“ˆๅธŒๆˆ–ๅŒบๅ—ๅท",
"JSON-RPC API Documentation": "JSON-RPC API ๆ–‡ๆกฃ",
"Jade Explorer Github": "Jade Explorer Github",
"Toggle Dark Mode": "ๅˆ‡ๆข้ป‘ๆš—ๆจกๅผ",
// Dashboard
"Block Height": "ๅŒบๅ—้ซ˜ๅบฆ",
"Chain ID": "Chain ID",
"Syncing": "ๅŒๆญฅไธญ",
"Gas Price": "Gas ่ดน็”จ",
"Network Hash Rate": "็ฎ—ๅŠ›",
"Pending Transactions": "็ญ‰ๅพ…ไธญ็š„ไบคๆ˜“",
"Peers": "็ซฏ็‚น",
"Hash Rate last blocks": "ไธŠไธชๅŒบๅ—็ฎ—ๅŠ›",
"Hash Rate last blocks_plural": "ๅ‰{{count}}ไธชๅŒบๅ—็ฎ—ๅŠ›",
"Transaction count last blocks": "ไธŠไธชๅŒบๅ—ไบคๆ˜“ๆ•ฐ้‡",
"Transaction count last blocks_plural": "ๅ‰ {{count}}ไธชๅŒบๅ—ไบคๆ˜“ๆ•ฐ้‡",
"Gas Used last blocks": "ไธŠไธชๅŒบๅ—Gasๆถˆ่€—",
"Gas Used last blocks_plural": "ๅ‰{{count}}ไธชๅŒบๅ—Gasๆถˆ่€—",
"Uncles last blocks": "ไธŠไธชๅŒบๅ—ๅ”ๅ—",
"Uncles last blocks_plural": "ๅ‰{{count}}ไธชๅŒบๅ—ๅ”ๅ—",
// block list
"Block Number": "ๅŒบๅ—็ผ–ๅท",
"Hash": "ๅ“ˆๅธŒ",
"Timestamp": "ๆ—ถ้—ดๆˆณ",
"Transactions": "ไบคๆ˜“",
// block view
"Number": "ๆ•ฐ้‡",
"ParentHash": "็ˆถๅ“ˆๅธŒ",
"Miner": "็Ÿฟๅทฅ",
"Nonce": "้šๆœบๆ•ฐ",
"Difficulty": "้šพๅบฆ",
"Extra Data": "้ขๅค–ๆ•ฐๆฎ",
"State Root": "็Šถๆ€ๆ น",
"Transaction Root": "ไบคๆ˜“ๆ น",
"Receipts Root": "ๆŽฅๆ”ถๆ น",
// transaction view
"Block": "ๅŒบๅ—",
"Gas Used": "Gas ๆถˆ่€—",
"Cumulative Gas Used": "็ดฏ่ฎกGasๆถˆ่€—",
"Value in Ether": "Etherๅ€ผ",
"From": "ไปŽ",
"To": "ๅˆฐ",
"Contract Address": "ๅˆ็บฆๅœฐๅ€",
"Transaction Index": "ไบคๆ˜“็ดขๅผ•",
"Receipt Status": "ๆŽฅๆ”ถ็Šถๆ€",
"Receipt Logs": "ๆŽฅๆ”ถๆ—ฅๅฟ—",
// address view
"Address": "ๅœฐๅ€",
"Balance": "ไฝ™้ข",
"Code": "ไปฃ็ ",
// configuration menu
"Configuration": "้…็ฝฎ",
"Back": "่ฟ”ๅ›ž",
"Ethereum RPC": "Ethereum RPC",
"Service Runner RPC": "Service Runner RPC",
};
9 changes: 2 additions & 7 deletions src/translations/en.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
export default {
// App Bar
// Moment integration
"Timestamp Date": "{{date, date}}",
"Explorer": "Explorer",
"Enter an Address, Transaction Hash or Block Number": "Enter an Address, Transaction Hash or Block Number",
"JSON-RPC API Documentation": "JSON-RPC API Documentation",
"Jade Explorer Github": "Jade Explorer Github",
"Toggle Dark Mode": "Toggle Dark Mode",
// Dashboard
"Block Height": "Block Height",
"Chain ID": "Chain ID",
"Syncing": "Syncing",
Expand All @@ -21,12 +21,10 @@ export default {
"Gas Used last blocks_plural": "Gas Used last {{count}} blocks",
"Uncles last blocks": "Uncles last block",
"Uncles last blocks_plural": "Uncles last {{count}} blocks",
// block list
"Block Number": "Block Number",
"Hash": "Hash",
"Timestamp": "Timestamp",
"Transactions": "Transactions",
// block view
"Number": "Number",
"ParentHash": "ParentHash",
"Miner": "Miner",
Expand All @@ -36,7 +34,6 @@ export default {
"State Root": "State Root",
"Transaction Root": "Transaction Root",
"Receipts Root": "Receipts Root",
// transaction view
"Block": "Block",
"Gas Used": "Gas Used",
"Cumulative Gas Used": "Cumulative Gas Used",
Expand All @@ -47,11 +44,9 @@ export default {
"Transaction Index": "Transaction Index",
"Receipt Status": "Receipt Status",
"Receipt Logs": "Receipt Logs",
// address view
"Address": "Address",
"Balance": "Balance",
"Code": "Code",
// configuration menu
"Configuration": "Configuration",
"Back": "Back",
"Ethereum RPC": "Ethereum RPC",
Expand Down
61 changes: 61 additions & 0 deletions src/translations/kr.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
export default {
// Moment integration
"Timestamp Date": "{{date, date}}",
// App Bar
"Explorer": "๋ธŒ๋ผ์šฐ์ €",
"Enter an Address, Transaction Hash or Block Number": "๊ฑฐ๋ž˜์ฃผ์†Œ ํ•ด์‹œ๊ฐ’ ํ˜น์€ ๋ธ”๋ก๋ฒˆํ˜ธ ์ž…๋ ฅ",
"JSON-RPC API Documentation": "JSON-RPC API ๋ฌธ์„œ",
"Jade Explorer Github": "Jade Explorer Github",
"Toggle Dark Mode": "๋ธ”๋ž™๋ชจ๋“œ ๋ณ€๊ฒฝ",
// Dashboard
"Block Height": "๋ธ”๋ก๋†’์ด",
"Chain ID": "Chain ID",
"Syncing": "๋™๊ธฐํ™”",
"Gas Price": "Gas ๋น„์šฉ",
"Network Hash Rate": "ํ•ด์‹œํŒŒ์›Œ",
"Pending Transactions": "๋Œ€๊ธฐ์ธ ๊ฑฐ๋ž˜",
"Peers": "๊ฑฐ์ ",
"Hash Rate last blocks": "์ด์ „ ๋ธ”๋ก ํ•ด์‹œํŒŒ์›Œ",
"Hash Rate last blocks_plural": "{{count}}๊ฐœ ์ด์ „ ๋ธ”๋ก ํ•ด์‹œํŒŒ์›Œ",
"Transaction count last blocks": "์ด์ „ ๋ธ”๋ก ๊ฑฐ๋ž˜์ˆ˜๋Ÿ‰",
"Transaction count last blocks_plural": "{{count}}๊ฐœ ์ด์ „ ๋ธ”๋ก ํ•ด์‹œํŒŒ์›Œ",
"Gas Used last blocks": "์ด์ „ ๋ธ”๋ก Gas์†Œ๋ชจ",
"Gas Used last blocks_plural": "{{count}}๊ฐœ ์ด์ „ ๋ธ”๋ก Gas์†Œ๋ชจ",
"Uncles last blocks": "์ด๋ฒˆ ๋ธ”๋ก์˜ ์—‰ํด๋ธ”๋ก",
"Uncles last blocks_plural": "{{count}}๊ฐœ ์ด์ „ ๋ธ”๋ก์˜ ์—‰ํด๋ธ”๋ก",
// block list
"Block Number": "๋ธ”๋ก๋ฒˆํ˜ธ",
"Hash": "ํ•ด์‹œ",
"Timestamp": "ํƒ€์ž„ ์Šคํƒฌํ”„",
"Transactions": "๊ฑฐ๋ž˜",
// block view
"Number": "์ˆ˜๋Ÿ‰",
"ParentHash": "ParentHash",
"Miner": "๋งˆ์ด๋„ˆ",
"Nonce": "๋ชฉํ•˜",
"Difficulty": "๋‚œ์ด๋„",
"Extra Data": "์ถ”๊ฐ€ ๋ฐ์ดํ„ฐ",
"State Root": "์ฃผ ๋ฃจํŠธ",
"Transaction Root": "๊ฑฐ๋ž˜ ๋ฃจํŠธ",
"Receipts Root": "์˜์ˆ˜ ๋ฃจํŠธ",
// transaction view
"Block": "๋ธ”๋ก",
"Gas Used": "Gas ์†Œ๋ชจ",
"Cumulative Gas Used": "๋ˆ„์  Gas์†Œ๋ชจ",
"Value in Ether": "Ether๊ฐ’",
"From": "๋ถ€ํ„ฐ",
"To": "์œผ๋กœ",
"Contract Address": "๊ณ„์•ฝ์ฃผ์†Œ",
"Transaction Index": "๊ฑฐ๋ž˜ ์ธ๋ฑ์Šค",
"Receipt Status": "์ˆ˜์‹ ์ƒํƒœ",
"Receipt Logs": "์ˆ˜์‹ ์ผ์ง€",
// address view
"Address": "์ฃผ์†Œ",
"Balance": "์ž”์•ก",
"Code": "์ฝ”๋“œ",
// configuration menu
"Configuration": "๊ตฌ์„ฑ",
"Back": "๋’ค๋กœ",
"Ethereum RPC": "Ethereum RPC",
"Service Runner RPC": "Service Runner RPC",
};

0 comments on commit f5d29fd

Please sign in to comment.