Skip to content

Commit 78a0a2a

Browse files
committed
Added languages switch
1 parent 91f41ef commit 78a0a2a

File tree

7 files changed

+55
-5
lines changed

7 files changed

+55
-5
lines changed

components/languages.tsx

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { Locale } from 'i18n';
2+
import styled from 'styled-components';
3+
import { useRouter } from 'next/dist/client/router';
4+
5+
const Container = styled.div`
6+
top: 0;
7+
right: 1rem;
8+
display: flex;
9+
position: fixed;
10+
11+
p {
12+
font-size: 0.75rem;
13+
text-transform: uppercase;
14+
padding: 0.5rem;
15+
border-radius: 0.25rem;
16+
17+
&:hover {
18+
cursor: pointer;
19+
background-color: ${({ theme }) => theme.colors.gray[100]};
20+
}
21+
}
22+
23+
* + * {
24+
margin-left: 0.5rem;
25+
}
26+
`;
27+
28+
export function Languages() {
29+
const { locales, push } = useRouter();
30+
31+
const handleLocaleClick = (locale: Locale) => () => {
32+
push('/', '/', { locale });
33+
};
34+
35+
return (
36+
<Container>
37+
{locales.map((locale: Locale) => (
38+
<p key={locale} onClick={handleLocaleClick(locale)}>
39+
{locale.split('-')[0]}
40+
</p>
41+
))}
42+
</Container>
43+
);
44+
}

components/template.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const Main = styled.main`
1919

2020
const Footer = styled.footer`
2121
margin: 2rem auto;
22+
font-size: 0.75rem;
2223
`;
2324

2425
type PageTemplateProps = {

i18n/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const locales = {
2424
};
2525

2626
type Locales = typeof locales;
27-
type Locale = keyof Locales | string;
27+
export type Locale = keyof Locales;
2828
type LocaleKey = keyof typeof portuguese | keyof typeof english;
2929
export type Translate = (key: LocaleKey) => string;
3030

pages/_app.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { AppProps } from 'next/app';
2-
import { getTranslate, Translate } from 'i18n';
2+
import { getTranslate, Locale, Translate } from 'i18n';
33
import { useEffect } from 'react';
44
import * as gtag from 'lib/gtag';
55

@@ -18,7 +18,7 @@ function MyApp({ Component, pageProps, router }: AppProps<CustomProps>) {
1818
};
1919
}, [router.events]);
2020

21-
return <Component {...pageProps} t={getTranslate(locale)} />;
21+
return <Component {...pageProps} t={getTranslate(locale as Locale)} />;
2222
}
2323

2424
export default MyApp;

pages/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { PageTemplate } from 'components/template';
55
import styled from 'styled-components';
66

77
import * as gtag from 'lib/gtag';
8+
import { Languages } from 'components/languages';
89

910
const Image = styled(NextImage)`
1011
border-radius: 50%;
@@ -67,6 +68,7 @@ const Home: NextPage<HomeProps> = ({ t }) => (
6768
<PageTemplate t={t} title={t('author')}>
6869
<h1>{t('welcome')}</h1>
6970
<p>{t('bio')}</p>
71+
<Languages />
7072
<Image
7173
src="/takah.jpg"
7274
alt="Picture of the author"

styles/globals.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export const GlobalStyles = createGlobalStyle`
77
margin: 0;
88
font-family: -apple-system, BlinkMacSystemFont, Segoe UI, Roboto, Oxygen,
99
Ubuntu, Cantarell, Fira Sans, Droid Sans, Helvetica Neue, sans-serif;
10-
background-color: ${({ theme }) => theme.colors.gray};
10+
background-color: ${({ theme }) => theme.colors.gray[200]};
1111
color: ${({ theme }) => theme.colors.white};
1212
}
1313

styles/theme.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ import 'styled-components';
22

33
export const tokens = {
44
colors: {
5-
gray: '#262626',
5+
gray: {
6+
100: '#4e4d4d',
7+
200: '#262626',
8+
},
69
white: '#ffffff',
710
},
811
};

0 commit comments

Comments
 (0)