-
Notifications
You must be signed in to change notification settings - Fork 30
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
lib-user: Setup react-i18next for Contributors components #6553
Open
goplayoutside3
wants to merge
3
commits into
master
Choose a base branch
from
translations-contributors
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# react-i18next for Translations | ||
|
||
The `/translations` folder contains json dictionary files for translatable strings in lib-content. There's an i18n instance for each library in FEM. | ||
|
||
|
||
## Adding New Dictionary Keys | ||
|
||
`en.json` contains the primary dictionary for translated strings in this app. Dictionaries in this folder are the only ones that should be edited manually in a local dev environment. An example of how to translate a component can be found below. | ||
|
||
|
||
## Translating a Component | ||
|
||
Import `useTranslation()` hook. Use `.` to indicate nested object keys. Alternatively, [react-i18next docs](https://react.i18next.com/latest/withtranslation-hoc) show how to translate a class component. | ||
|
||
```js | ||
import { useTranslation } from 'react-i18next' | ||
const MyComponent = () => { | ||
const { t } = useTranslation() | ||
return ( | ||
<p>{t('Path.To.Translation.Key')}</p> | ||
) | ||
} | ||
``` | ||
|
||
|
||
## Adding a Language | ||
|
||
FEM is integrated with [Lokalise](https://app.lokalise.com) for translations management. Any dictionary other than `en.json` should be managed through the Lokalise Dashboard. Instructions on how to import and export dictionary files can be found in the how-to-zooniverse [Translations](https://github.com/zooniverse/how-to-zooniverse/tree/master/Translations) folder. | ||
|
||
New languages must be added into the `supportedLngs` array in i18n.js. | ||
|
||
```js | ||
const supportedLngs = ['en', 'fr'] | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"common": { | ||
"avatarAlt": "{{login}} avatar", | ||
"back": "Back", | ||
"classifications": "Classifications", | ||
"hours": "Hours" | ||
}, | ||
"Contributors": { | ||
"error": "There was an error", | ||
"exportLink": "Export all stats", | ||
"generating": "Generating stats export...", | ||
"none": "There are no contributors to this group.", | ||
"noPermission": "You do not have permission to view this group's contributors.", | ||
"title": "Full Group Stats", | ||
"ContributorsList": { | ||
"a11y": "{{name}} member stats", | ||
"privateProject": "Private Project {{index}}" | ||
}, | ||
"ProjectStats": { | ||
"a11y": "{{project}} member stats" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import i18n from 'i18next' | ||
import { | ||
initReactI18next, | ||
Trans as BaseTrans, | ||
useTranslation as useBaseTranslation | ||
} from 'react-i18next' | ||
|
||
const libI18n = i18n.createInstance() | ||
libI18n.use(initReactI18next).init({ | ||
fallbackLng: 'en', | ||
interpolation: { | ||
escapeValue: false // not needed for react as it escapes by default | ||
}, | ||
react: { | ||
useSuspense: false | ||
} | ||
}) | ||
|
||
libI18n.addResourceBundle('en', 'translation', require('./en.json')) | ||
|
||
/* In FEM there's an i18n instance for each library, and each instance has its own functions.*/ | ||
export function useTranslation(ns) { | ||
return useBaseTranslation(ns, { i18n: libI18n }) | ||
} | ||
|
||
export function withTranslation(ns) { | ||
return Component => { | ||
return function TranslatedComponent(props) { | ||
const { t } = useTranslation(ns) | ||
return <Component i18n={libI18n} t={t} {...props} /> | ||
} | ||
} | ||
} | ||
|
||
export function Trans(props) { | ||
return <BaseTrans {...props} i18n={libI18n}/> | ||
} | ||
|
||
export default i18n |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is trivial, but if lib-classifier creates
classifierI18n
and lib-react-components createszrcI18n
, should this be less generic and includeuser
or a related abbreviation?