Last active
May 3, 2021 21:12
-
-
Save DenisIzmaylov/dbd7e85f384016e3e07a to your computer and use it in GitHub Desktop.
Easy i18n translation in your ES6 apps
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
{ | |
"ru": { | |
"Your original english text": "Твой оригинальный русский текст" | |
} | |
} |
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
const i18n = require('./i18n-data.json'); | |
let currentLang = 'en'; | |
export function setLanguage(id) { | |
currentLang = id; | |
} | |
export function t(text) { | |
return (i18n[currentLang] && i18n[currentLang][text]) || text; | |
} |
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
import React, { Component } from 'react'; | |
import t from './i18n'; | |
export default MyComponent extends Component { | |
render() { | |
<div> | |
{t('Your original english text')} | |
</div> | |
} | |
} |
Thanks a lot! 👍
Thanks for sharing:-)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Missing
import { t } from './i18n'
inmy-component.js
:)