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

DON'T MERGE (yet): Added I18n support: German and Dutch languages #6

Open
wants to merge 3 commits into
base: master
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
268 changes: 207 additions & 61 deletions package-lock.json

Large diffs are not rendered by default.

11 changes: 10 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"lint": "eslint src",
"test": "jest ./tests",
"test:e2e": "cypress run",
"deploy": "npm run build && firebase deploy"
"deploy": "npm run build && firebase deploy",
"i18n": "i18nline synch"
},
"eslintConfig": {
"extends": "eslint-config-synacor"
Expand All @@ -31,8 +32,10 @@
"preact-render-spy": "^1.2.1"
},
"dependencies": {
"i18nline": "^2.0.1",
"preact": "^8.2.6",
"preact-compat": "^3.17.0",
"preact-i18nline": "^2.0.0",
"preact-router": "^2.5.7"
},
"jest": {
Expand All @@ -58,5 +61,11 @@
"^create-react-class$": "preact-compat/lib/create-react-class",
"^react-addons-css-transition-group$": "preact-css-transition-group"
}
},
"i18n": {
"autoTranslateTags": [
"h1",
"Link"
]
}
}
16 changes: 16 additions & 0 deletions preact.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export default (config, env, helpers) => {
// Use Preact CLI's helpers object to get the babel-loader
let babel = helpers.getLoadersByName(config, 'babel-loader')[0].rule;
// Update the loader config to include preact-i18nline
babel.loader = [
{ // create an entry for the old loader
loader: babel.loader,
options: babel.options
},
{ // add the preact-i18nline webpack loader
loader: 'preact-i18nline/webpack-loader'
}
];
// remove the old loader options
delete babel.options;
};
29 changes: 29 additions & 0 deletions src/components/LanguagePicker/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { h, Component } from 'preact';
import I18n from 'i18nline';
import style from './style';

const locales = () => [
{ k: 'en', v: I18n.t('English') },
{ k: 'de', v: I18n.t('German') },
{ k: 'nl', v: I18n.t('Dutch') }
];

const alphabetic = (x,y) => (x.v < y.v ? -1 : x.v > y.v ? 1 : 0);

export default class LanguagePicker extends Component {
changeListener = (e) => {
const lang = e.target.options[e.target.selectedIndex].value;
this.props.changeLocale && this.props.changeLocale(lang, e);
e.preventDefault();
}

render (props) {
return (
<select class={style.languagePicker} onChange={this.changeListener}>
{locales().sort(alphabetic).map(x => (
<option selected={x.k === (props.locale || props.defaultLocale)} value={x.k}>{x.v}</option>
))}
</select>
);
}
}
4 changes: 4 additions & 0 deletions src/components/LanguagePicker/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.languagePicker {
margin: auto 20px auto auto;
width: 100px;
}
19 changes: 18 additions & 1 deletion src/components/app.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,37 @@
import { h, Component } from 'preact';
import { Router } from 'preact-router';
import I18n from '../i18n';
import Header from './header';
import LanguagePicker from './LanguagePicker';
import Home from '../routes/home';
import New from '../routes/new';
import Best from '../routes/best';
import Item from '../routes/item';

if (module.hot) {
require('preact/debug');
((typeof window === 'object') && window || global).I18n = I18n;
}

export default class App extends Component {
localeChanged = (locale) => {
this.setState({ locale });
}

componentDidMount() {
I18n.on('change', this.localeChanged);
}

componentWillUnmount() {
I18n.removeListener('change', this.localeChanged);
}

render() {
return (
<div id="app">
<Header />
<Header>
<LanguagePicker {...I18n} />
</Header>
<Router>
<Home path="/:page?" />
<New path="/new/:page?" />
Expand Down
8 changes: 5 additions & 3 deletions src/components/header/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { h, Component } from 'preact';
import { Link } from 'preact-router/match';
import I18n from 'i18nline/lib/i18n';
import style from './style';

export default class Header extends Component {
Expand All @@ -8,10 +9,11 @@ export default class Header extends Component {
<header class={style.header}>
<h1>Preact HNPWA</h1>
<nav>
<Link activeClassName={style.active} href="/">Top</Link>
<Link activeClassName={style.active} href="/new">New</Link>
<Link activeClassName={style.active} href="/best">Best</Link>
<Link translate="yes" activeClassName={style.active} href="/">Top</Link>
<Link translate="yes" activeClassName={style.active} href="/new">New</Link>
<Link translate="yes" activeClassName={style.active} href="/best">Best</Link>
</nav>
{this.props.children}
</header>
);
}
Expand Down
14 changes: 14 additions & 0 deletions src/i18n/de.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"de": {
"best_e9c34252": "Beste",
"comments_count_0_no_comments_comments_count_1_1_co_56eb6f20": "%{comments_count_0_no_comments} %{comments_count_1_1_comment} %{comments_count_1_comments_count_comments}",
"dutch_6d05cee5": "Niederländisch",
"english_c60612e2": "Englisch",
"german_3ec99bbb": "Deutsch",
"item_title_item_domain_2ba8bfc4": "* %{item_title} **(%{item_domain})** *",
"more_cf9cc661": "Mehr",
"new_13daf639": "Neu",
"preact_hnpwa_f9c867d6": "Preact HNPWA",
"top_66e0adb6": "Top"
}
}
14 changes: 14 additions & 0 deletions src/i18n/default.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"en": {
"best_e9c34252": "Best",
"comments_count_0_no_comments_comments_count_1_1_co_56eb6f20": "%{comments_count_0_no_comments} %{comments_count_1_1_comment} %{comments_count_1_comments_count_comments}",
"dutch_6d05cee5": "Dutch",
"english_c60612e2": "English",
"german_3ec99bbb": "German",
"item_title_item_domain_2ba8bfc4": "* %{item_title} **(%{item_domain})** *",
"more_cf9cc661": "More",
"new_13daf639": "New",
"preact_hnpwa_f9c867d6": "Preact HNPWA",
"top_66e0adb6": "Top"
}
}
14 changes: 14 additions & 0 deletions src/i18n/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"en": {
"best_e9c34252": "Best",
"comments_count_0_no_comments_comments_count_1_1_co_56eb6f20": "%{comments_count_0_no_comments} %{comments_count_1_1_comment} %{comments_count_1_comments_count_comments}",
"dutch_6d05cee5": "Dutch",
"english_c60612e2": "English",
"german_3ec99bbb": "German",
"item_title_item_domain_2ba8bfc4": "* %{item_title} **(%{item_domain})** *",
"more_cf9cc661": "More",
"new_13daf639": "New",
"preact_hnpwa_f9c867d6": "Preact HNPWA",
"top_66e0adb6": "Top"
}
}
26 changes: 26 additions & 0 deletions src/i18n/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions src/i18n/nl.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"nl": {
"best_e9c34252": "Beste",
"comments_count_0_no_comments_comments_count_1_1_co_56eb6f20": "%{comments_count_0_no_comments} %{comments_count_1_1_comment} %{comments_count_1_comments_count_comments}",
"dutch_6d05cee5": "Nederlands",
"english_c60612e2": "Engels",
"german_3ec99bbb": "Duits",
"item_title_item_domain_2ba8bfc4": "* %{item_title} **(%{item_domain})** *",
"more_cf9cc661": "Meer",
"new_13daf639": "Nieuw",
"preact_hnpwa_f9c867d6": "Preact HNPWA",
"top_66e0adb6": "Top"
}
}