Skip to content

Commit a51f765

Browse files
authored
Merge pull request #2210 from lindapaiste/refactor/lang-menu
Cleanup language menu code in `Nav` component
2 parents 9539b33 + 11d16e5 commit a51f765

File tree

3 files changed

+41
-165
lines changed

3 files changed

+41
-165
lines changed

client/components/Nav.jsx

Lines changed: 17 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
import classNames from 'classnames';
2+
import { sortBy } from 'lodash';
13
import PropTypes from 'prop-types';
24
import React from 'react';
3-
import { connect } from 'react-redux';
4-
import { withRouter } from 'react-router';
5-
import { Link } from 'react-router';
6-
import classNames from 'classnames';
75
import { withTranslation } from 'react-i18next';
8-
import { languageKeyToLabel } from '../i18n';
6+
import { connect } from 'react-redux';
7+
import { Link, withRouter } from 'react-router';
8+
import { availableLanguages, languageKeyToLabel } from '../i18n';
99
import * as IDEActions from '../modules/IDE/actions/ide';
1010
import * as toastActions from '../modules/IDE/actions/toast';
1111
import * as projectActions from '../modules/IDE/actions/project';
@@ -613,7 +613,6 @@ class Nav extends React.PureComponent {
613613
}}
614614
>
615615
<span className="nav__item-header">
616-
{' '}
617616
{languageKeyToLabel(this.props.language)}
618617
</span>
619618
<TriangleIcon
@@ -623,146 +622,18 @@ class Nav extends React.PureComponent {
623622
/>
624623
</button>
625624
<ul className="nav__dropdown">
626-
<li className="nav__dropdown-item">
627-
<button
628-
onFocus={this.handleFocusForLang}
629-
onBlur={this.handleBlur}
630-
value="de"
631-
onClick={(e) => this.handleLangSelection(e)}
632-
>
633-
Deutsch
634-
</button>
635-
</li>
636-
<li className="nav__dropdown-item">
637-
<button
638-
onFocus={this.handleFocusForLang}
639-
onBlur={this.handleBlur}
640-
value="en-US"
641-
onClick={(e) => this.handleLangSelection(e)}
642-
>
643-
English
644-
</button>
645-
</li>
646-
<li className="nav__dropdown-item">
647-
<button
648-
onFocus={this.handleFocusForLang}
649-
onBlur={this.handleBlur}
650-
value="es-419"
651-
onClick={(e) => this.handleLangSelection(e)}
652-
>
653-
Español
654-
</button>
655-
</li>
656-
<li className="nav__dropdown-item">
657-
<button
658-
onFocus={this.handleFocusForLang}
659-
onBlur={this.handleBlur}
660-
value="fr-CA"
661-
onClick={(e) => this.handleLangSelection(e)}
662-
>
663-
Français
664-
</button>
665-
</li>
666-
<li className="nav__dropdown-item">
667-
<button
668-
onFocus={this.handleFocusForLang}
669-
onBlur={this.handleBlur}
670-
value="hi"
671-
onClick={(e) => this.handleLangSelection(e)}
672-
>
673-
हिन्दी
674-
</button>
675-
</li>
676-
<li className="nav__dropdown-item">
677-
<button
678-
onFocus={this.handleFocusForLang}
679-
onBlur={this.handleBlur}
680-
value="ko"
681-
onClick={(e) => this.handleLangSelection(e)}
682-
>
683-
한국어
684-
</button>
685-
</li>
686-
<li className="nav__dropdown-item">
687-
<button
688-
onFocus={this.handleFocusForLang}
689-
onBlur={this.handleBlur}
690-
value="it"
691-
onClick={(e) => this.handleLangSelection(e)}
692-
>
693-
Italiano
694-
</button>
695-
</li>
696-
<li className="nav__dropdown-item">
697-
<button
698-
onFocus={this.handleFocusForLang}
699-
onBlur={this.handleBlur}
700-
value="ja"
701-
onClick={(e) => this.handleLangSelection(e)}
702-
>
703-
日本語
704-
</button>
705-
</li>
706-
<li className="nav__dropdown-item">
707-
<button
708-
onFocus={this.handleFocusForLang}
709-
onBlur={this.handleBlur}
710-
value="pt-BR"
711-
onClick={(e) => this.handleLangSelection(e)}
712-
>
713-
Português
714-
</button>
715-
</li>
716-
<li className="nav__dropdown-item">
717-
<button
718-
onFocus={this.handleFocusForLang}
719-
onBlur={this.handleBlur}
720-
value="sv"
721-
onClick={(e) => this.handleLangSelection(e)}
722-
>
723-
Svenska
724-
</button>
725-
</li>
726-
<li className="nav__dropdown-item">
727-
<button
728-
onFocus={this.handleFocusForLang}
729-
onBlur={this.handleBlur}
730-
value="uk-UA"
731-
onClick={(e) => this.handleLangSelection(e)}
732-
>
733-
Українська
734-
</button>
735-
</li>
736-
<li className="nav__dropdown-item">
737-
<button
738-
onFocus={this.handleFocusForLang}
739-
onBlur={this.handleBlur}
740-
value="zh-CN"
741-
onClick={(e) => this.handleLangSelection(e)}
742-
>
743-
简体中文
744-
</button>
745-
</li>
746-
<li className="nav__dropdown-item">
747-
<button
748-
onFocus={this.handleFocusForLang}
749-
onBlur={this.handleBlur}
750-
value="zh-TW"
751-
onClick={(e) => this.handleLangSelection(e)}
752-
>
753-
正體中文
754-
</button>
755-
</li>
756-
<li className="nav__dropdown-item">
757-
<button
758-
onFocus={this.handleFocusForLang}
759-
onBlur={this.handleBlur}
760-
value="tr"
761-
onClick={(e) => this.handleLangSelection(e)}
762-
>
763-
Türkçe
764-
</button>
765-
</li>
625+
{sortBy(availableLanguages).map((key) => (
626+
<li className="nav__dropdown-item" key={key}>
627+
<button
628+
onFocus={this.handleFocusForLang}
629+
onBlur={this.handleBlur}
630+
value={key}
631+
onClick={this.handleLangSelection}
632+
>
633+
{languageKeyToLabel(key)}
634+
</button>
635+
</li>
636+
))}
766637
</ul>
767638
</li>
768639
</React.Fragment>

client/i18n.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ import {
2020
} from 'date-fns/locale';
2121

2222
const fallbackLng = ['en-US'];
23-
const availableLanguages = [
23+
24+
export const availableLanguages = [
2425
'de',
2526
'en-US',
2627
'es-419',

contributor_docs/translations.md

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,25 @@ In order to simplify the translations process the following rules of thumb were
1111

1212
* There is only one file to translate all the texts in any specific language, which is located under the directory, in the respective locale [subdirectory](https://github.com/processing/p5.js-web-editor/tree/develop/translations/locales)
1313
* The new language code must be added to [client/i18n.js](https://github.com/processing/p5.js-web-editor/blob/edae248eede21d7ad7702945929efbcdfeb4d9ea/client/i18n.js#L22)
14-
* New languages will need to be selected using a dropdown in the Nav component, specifically in function [renderLanguageMenu.](https://github.com/processing/p5.js-web-editor/blob/edae248eede21d7ad7702945929efbcdfeb4d9ea/client/components/Nav.jsx#L599)
1514
* Need to add `TRANSLATIONS_ENABLED=true` to `.env` to activate the dropdown for the languages.
1615

17-
#### Nav.js
18-
Need to add the following code to add a new language as a dropdown menu.
19-
```js
20-
<li className="nav__dropdown-item">
21-
<button
22-
onFocus={this.handleFocusForLang}
23-
onBlur={this.handleBlur}
24-
value="newLanguageValue"
25-
onClick={e => this.handleLangSelection(e)}
26-
>
27-
new language name in the new language ex: 日本語 (Japanese)
28-
</button>
29-
</li>
30-
```
16+
#### Language codes
17+
We use standard [IETF language codes](https://en.wikipedia.org/wiki/IETF_language_tag) to identify languages. In most cases, the code is either two lowercase letters representing a language (`ja` for Japanese) or a language code followed by a hyphen and two uppercase letters for a country (`en-US` for American English).
3118

3219
#### i18n.js
33-
In terms of `i18n.js`, you will need to update 2 things. One is to import a new language from `date-fns/locale`. The other is to add a new language to `languageMap`.
20+
In terms of `i18n.js`, you will need to update 4 things:
3421

22+
1. Add the code for your language to the array of `availableLanguages`.
3523
```js
36-
import { enUS, es, ja, newLanguage } from 'date-fns/locale';
3724
const availableLanguages = ['en-US', 'es-419', 'ja', 'newLanguage'];
3825
```
3926

27+
2. Import the locale for your language from `date-fns/locale`. This is used to translate dates and times.
28+
```js
29+
import { enUS, es, ja, newLanguage } from 'date-fns/locale';
30+
```
31+
32+
3. Associate the locale with the country code by adding it to the map in the `languageKeyToDateLocale` function.
4033
```js
4134
export function languageKeyToDateLocale(lang) {
4235
const languageMap = {
@@ -49,7 +42,18 @@ export function languageKeyToDateLocale(lang) {
4942
}
5043
```
5144

52-
45+
4. Add the name of your language to the map in the `languageKeyToLabel` function. This will determine what is shown in the dropdown menu.
46+
```js
47+
export function languageKeyToLabel(lang) {
48+
const languageMap = {
49+
'en-US': 'English',
50+
'es-419': 'Español',
51+
ja: '日本語',
52+
'newLanguage': 'New Language Name'
53+
};
54+
return languageMap[lang];
55+
}
56+
```
5357

5458
## Translations
5559

0 commit comments

Comments
 (0)