Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DEMO CODE] implements i18next support #9

Open
wants to merge 2 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions cypress/support/navigatorStubs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const stubLanguages = language => {
return {
onBeforeLoad(win) {
Object.defineProperty(
win.navigator,
'language',
{
value: language[0]
}
);
Object.defineProperty(
win.navigator,
"languages",
{
value: [language[0], language[1]]
}
);
}
}
}

const stubLanguage = language => {
return {
onBeforeLoad(win) {
Object.defineProperty(
win.navigator,
'language',
{
get: cy.stub().returns(language).as('language')
}
)
}
}
}
export { stubLanguage, stubLanguages }
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@
"@testing-library/user-event": "^7.1.2",
"axios": "^0.19.2",
"grommet": "^2.11.2",
"i18next": "^19.3.3",
"i18next-browser-languagedetector": "^4.0.2",
"i18next-xhr-backend": "^3.2.2",
"polished": "^3.4.4",
"react": "^16.13.0",
"react-dom": "^16.13.0",
"react-i18next": "^11.3.4",
"react-redux": "^7.2.0",
"react-scripts": "3.4.0",
"redux": "^4.0.5",
Expand Down
10 changes: 10 additions & 0 deletions public/locales/en/translation.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"header": {
"headline": "Urban Living {{key}}",
"tagline": "A source of work / life inspiration for young professionals."
},
"languages": {
"english": "English",
"swedish": "Swedish"
}
}
10 changes: 10 additions & 0 deletions public/locales/sv/translation.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"header": {
"headline": "Urban Livsstil",
"tagline": "En härlig tagline på svenska bör läggas till"
},
"languages": {
"english": "Engelska",
"swedish": "Svenska"
}
}
38 changes: 24 additions & 14 deletions src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,33 @@
import React, { Component } from "react";
import React from "react";
import { connect } from "react-redux";
import ArticleList from "./components/ArticleList";
import { Grommet, Main, Heading } from "grommet";
import { grommet } from "grommet/themes";
import SpecificArticle from "./components/SpecificArticle";
import { useTranslation } from 'react-i18next'
import i18n from './i18n'

class App extends Component {
render() {
return (
<Grommet full theme={grommet}>
<Main fill align="center" justify="center">
<Heading>Urban Living</Heading>A source of work / life inspiration for
young professionals.
{this.props.state.showArticleList && <ArticleList />}
{this.props.state.readArticle && <SpecificArticle />}
</Main>
</Grommet>
);
}

const App = props => {
const { t } = useTranslation()

return (
<Grommet full theme={grommet}>
<Main fill align="center" justify="center">
<Heading>{t('header.headline', { key: 'value' })}</Heading>{t('header.tagline')}
<small
style={{ cursor: 'pointer' }}
onClick={() => i18n.changeLanguage('sv')}
>{t('languages.swedish')}</small>
<small
style={{ cursor: 'pointer' }}
onClick={() => i18n.changeLanguage('en')}
>{t('languages.english')}</small>
{props.state.showArticleList && <ArticleList />}
{props.state.readArticle && <SpecificArticle />}
</Main>
</Grommet>
)
}

const mapStateToProps = state => {
Expand Down
2 changes: 1 addition & 1 deletion src/components/ArticleList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class ArticleList extends Component {
});
}

async articleFetcher (event) {
async articleFetcher(event) {
let id = event.target.dataset.id
let response = await axios.get(`/articles/${id}`)
this.props.dispatch({
Expand Down
21 changes: 21 additions & 0 deletions src/i18n.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import i18n from 'i18next'
import Backend from 'i18next-xhr-backend'
import LanguageDetector from 'i18next-browser-languagedetector'
import { initReactI18next } from 'react-i18next'

const fallbackLng = ['en']
const availableLanguages = ['en', 'sv']

i18n
.use(Backend)
.use(LanguageDetector)
.use(initReactI18next)
.init(
{
fallbackLng,
whitelist: availableLanguages,
interpolation: { escapeValue: true }
}
)

export default i18n
17 changes: 7 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { Suspense } from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
Expand All @@ -14,14 +14,11 @@ axios.defaults.baseURL = "http://localhost:3000/api/";
const store = configureStore();
window.store = store;


ReactDOM.render(
<Provider store={store}>
<App />
</Provider>, document.getElementById('root'));

serviceWorker.unregister();
<Provider store={store}>
<Suspense fallback={(<div>Loading...</div>)}>
<App />
</Suspense>
</Provider>, document.getElementById('root'));

if (window.Cypress) {
window.store = store
}
serviceWorker.unregister();
Loading