{"id":13517827,"url":"https://github.com/amsul/react-translated","last_synced_at":"2025-04-07T12:08:15.586Z","repository":{"id":26889310,"uuid":"111767577","full_name":"amsul/react-translated","owner":"amsul","description":"A dead simple way to add complex translations (i18n) in a React (DOM/Native) project 🌎🌍🌏","archived":false,"fork":false,"pushed_at":"2022-12-07T18:16:23.000Z","size":943,"stargazers_count":178,"open_issues_count":43,"forks_count":12,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-04-01T01:37:51.928Z","etag":null,"topics":["i18n","internationalization","localisation","localization","react","react-component","react-native","react-translate","react-translations","reactjs","translate","translation"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/amsul.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-11-23T05:35:21.000Z","updated_at":"2025-01-29T21:57:43.000Z","dependencies_parsed_at":"2022-07-27T08:52:29.439Z","dependency_job_id":null,"html_url":"https://github.com/amsul/react-translated","commit_stats":null,"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amsul%2Freact-translated","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amsul%2Freact-translated/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amsul%2Freact-translated/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amsul%2Freact-translated/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/amsul","download_url":"https://codeload.github.com/amsul/react-translated/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247648977,"owners_count":20972945,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["i18n","internationalization","localisation","localization","react","react-component","react-native","react-translate","react-translations","reactjs","translate","translation"],"created_at":"2024-08-01T05:01:37.839Z","updated_at":"2025-04-07T12:08:15.567Z","avatar_url":"https://github.com/amsul.png","language":"JavaScript","funding_links":[],"categories":["JavaScript","📦 Libraries"],"sub_categories":["React / React Native"],"readme":"# react-translated\n\nA dead simple way to add complex translations in a React project 🌎🌍🌏\n\n#### Features\n\n* 💥 Data interpolation\n* ☄ Component interpolation\n* Ⓜ Markdown inline-manipulations\n* 🔀 Custom manipulations, pluralizations, and grammar rules based on input-data\n* ⚛ Component-level translation files (enables loading only required translations)\n\n#### Example\n\nWrite this:\n\n```jsx\n\u003cTranslate\n  text=\"{difficulty} *translations* in React \u003cReactLogo\u003e\"\n  data={{ difficulty: 'Simple' }}\n  renderMap={{\n    renderReactLogo: () =\u003e \u003cReactLogo size={14} /\u003e,\n  }}\n/\u003e\n```\n\nTo render this:\n\n![](docs/simple_translations_in_react.png)\n\n#### Support\n\nReact DOM and React Native 🔥\n\n#### Try\n\nPlay around with the library in your browser through the [CodeSandbox](https://codesandbox.io/s/jzq7zwj5k3).\n\n\u003cbr /\u003e\n\n## Table of Contents\n\n* [Installation](#installation)\n* [Setup](#setup)\n* [Usage](#usage)\n* [Contributors](#contributors)\n* [TODOs](#todos)\n* [License](#license)\n\n\u003cbr /\u003e\n\n## Installation\n\nWhatever floats your boat:\n\n* [Yarn](https://yarnpkg.com): `yarn add react-translated`\n* [npm](https://www.npmjs.com): `npm install react-translated`\n\n\u003cbr /\u003e\n\n## Setup\n\n#### Step 1: Create the translations file\n\nCreate a file that will contain a mapping of keys to the string in each language you support.\n\nTo keep things simple, use the strings of your default language as the key:\n\n```js\n// translation.js\n\nexport default {\n  'Hi, World!': {\n    en: 'Hi, World!',\n    fr: 'Bonjour le monde!',\n  },\n  // ...\n}\n```\n\n\u003e NOTE: There is no enforcement on the key used for a language. In these examples, [2-digit country codes](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2#Officially_assigned_code_elements) (`en`, `fr`, etc) are used. Decide on a convention and use that for all translations.\n\n#### Step 2: Connect the `Provider`\n\nWrap your top-level component with the `\u003cProvider\u003e` and set the `translation` and `language` props:\n\n```jsx\n// index.js\n\nimport { Provider } from 'react-translated'\nimport translation from './translation'\nconst App = (\n  \u003cProvider language=\"en\" translation={translation}\u003e\n    \u003cMyApplicationRoot /\u003e\n  \u003c/Provider\u003e\n)\n```\n\n\u003e NOTE: The value of the `language` prop must be one of the keys used for a language defined in Step 1.\n\n#### Step 3: Start translating\n\nThat is all!\n\nContinue reading below to see how to handle the various translation scenarios.\n\n\u003cbr /\u003e\n\n## Usage\n\nThe library can be imported in whatever way you find suitable:\n\n```js\nimport ReactTranslated from 'react-translated'\nimport * as ReactTranslated from 'react-translated'\n\n\u003cReactTranslated.Translate /*...*/ /\u003e\n```\n\nOr:\n\n```js\nimport { Provider, Translate, Translator } from 'react-translated'\n\n\u003cTranslate /*...*/ /\u003e\n```\n\n#### `Translate` vs `Translator`\n\nThe `Translate` component should **always** be used when the translation is rendered as a child component; such as buttons, paragraphs, headings, etc.\n\nThe `Translator` component should **only** be used when the translation is needed as a string; such as placeholders, alternate text, etc.\n\n#### Translation scenarios\n\n* [Static text](#static-text)\n* [Templated text](#templated-text)\n* [Dynamically templated text](#dynamically-templated-text)\n* [Styled text](#styled-text)\n* [Component within text](#component-within-text)\n* [Translated text as string](#translated-text-as-string) (for text input placeholders)\n\n\u003cbr /\u003e\n\n### Static text\n\nThis is pretty self-explanatory:\n\n```jsx\n// translation.js\nexport default {\n  'Hi, World!': {\n    en: 'Hi, World!',\n    fr: 'Bonjour le monde!',\n  },\n}\n\n// any component file\n\u003cTranslate text='Hi, World!' /\u003e\n```\n\nRenders as:\n\n![](docs/hi_world.png)\n\n### Templated text\n\nTo use dynamic text, the text can be templated:\n\n```jsx\n// translation.js\nexport default {\n  'Hi, {firstName}!': {\n    en: 'Hi, {firstName}!',\n    fr: 'Salut {firstName}!',\n  },\n}\n\n// any component file\n\u003cTranslate\n  text='Hi, {firstName}!'\n  data={{ firstName: 'Sergey' }}\n  /\u003e\n```\n\nRenders as:\n\n![](docs/hi_sergey.png)\n\n### Dynamically templated text\n\nSometimes just dynamic text is not enough and the template itself needs to be dynamic (for example pluralization). That can be achieved using a function call:\n\n```jsx\n// translation.js\nexport default {\n  'There are {catsCount} cats in this room.': {\n    en({ catsCount }) {\n      if (catsCount === 1) {\n        return 'There is {catsCount} cat in this room.'\n      }\n      return 'There are {catsCount} cats in this room.'\n    },\n    // ...\n  },\n}\n\n// any component file\n\u003cTranslate\n  text='There are {catsCount} cats in this room.'\n  data={{ catsCount: 2 }}\n  /\u003e\n\u003cTranslate\n  text='There are {catsCount} cats in this room.'\n  data={{ catsCount: 1 }}\n  /\u003e\n```\n\nRenders as:\n\n![](docs/cats_in_room.png)\n\nSince these templates are simple function calls, things more complex than pluralization can be done too:\n\n```jsx\n// translation.js\nexport default {\n  'This is a {fruit}': {\n    en({ fruit }) {\n      if (/^[aeiou]/.test(fruit)) {\n        return 'This is an {fruit}'\n      }\n      return 'This is a {fruit}'\n    },\n    // ...\n  },\n}\n\n// any component file\n\u003cTranslate\n  text='This is a {fruit}'\n  data={{ fruit: 'banana' }}\n  /\u003e\n\u003cTranslate\n  text='This is a {fruit}'\n  data={{ fruit: 'apple' }}\n  /\u003e\n```\n\nRenders as:\n\n![](docs/this_is_a_fruit.png)\n\n### Styled text\n\nThe translated text can also have some basic styling applied:\n\n```jsx\n// translation.js\nexport default {\n  'Hi, *World*!': {\n    en: 'Hi, *World*!',\n    fr: 'Bonjour *le monde*!',\n  },\n}\n\n// any component file\n\u003cTranslate text='Hi, *World*!' /\u003e\n```\n\nRenders as:\n\n![](docs/styled_hi_world.png)\n\nAnd of course the same can be done with dynamic templates:\n\n```jsx\n// translation.js\nexport default {\n  'Hi, *{firstName}*!': {\n    en: 'Hi, *{firstName}*!',\n    fr: 'Salut *{firstName}*!',\n  },\n}\n\n// any component file\n\u003cTranslate\n  text='Hi, *{firstName}*!'\n  data={{ firstName: 'Sergey' }}\n  /\u003e\n```\n\nRenders as:\n\n![](docs/styled_hi_sergey.png)\n\n### Component within text\n\nFor more advanced uses where Markdown and Emojis don’t suffice, components can be rendered within the text:\n\n```jsx\n// translation.js\nexport default {\n  'Tap the \u003cStarIcon\u003e to add': {\n    en: 'Tap the \u003cStarIcon\u003e to add',\n    fr: 'Appuyez sur la \u003cStarIcon\u003e pour ajouter',\n  },\n}\n\n// any component file\n\u003cTranslate\n  text='Tap the \u003cStarIcon\u003e to add!'\n  renderMap={{\n    renderStarIcon: () =\u003e \u003cStarIcon size={14} /\u003e\n  }}\n  /\u003e\n```\n\nRenders as:\n\n![](docs/tap_the_star.png)\n\nAnother practical application of this is nested translations - text that requires data that also needs to be translated:\n\n```jsx\n// translation.js\nexport default {\n  'I was born in \u003cMonthName\u003e': {\n    en: 'I was born in \u003cMonthName\u003e',\n    fr: 'Je suis né en \u003cMonthName\u003e',\n  },\n  'August': {\n    en: 'August',\n    fr: 'août',\n  },\n}\n\n// any component file\nconst monthName = 'August'\n\u003cTranslate\n  text='I was born in \u003cMonthName\u003e'\n  renderMap={{\n    renderMonthName: () =\u003e \u003cTranslate text={monthName} /\u003e\n  }}\n  /\u003e\n```\n\nRenders as:\n\n![](docs/born_in_august.png)\n\n\u003cbr /\u003e\n\n### Translated text as string\n\n\u003e _Added v2.2.0_\n\nIn scenarios where the translated text is required as a string, such as with text inputs placeholders or accessibility labels, the `Translator` can be used:\n\n```jsx\n// translation.js\nexport default {\n  'Enter your age {firstName}': {\n    en: 'Enter your age {firstName}',\n    fr: 'entrez votre âge {firstName}',\n  },\n}\n\n// any component file\n\u003cTranslator\u003e\n  {({ translate }) =\u003e (\n    \u003cinput\n      placeholder={translate({\n        text: 'Enter your age {firstName}',\n        data: { firstName: 'Sergey' },\n      })}\n      /\u003e\n  )}\n\u003c/Translator\u003e\n```\n\nRenders as:\n\n![](docs/age_sergey.png)\n\n\u003cbr /\u003e\n\n## Contributors\n\n\u003c!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section --\u003e\n\u003c!-- prettier-ignore --\u003e\n| [\u003cimg src=\"https://avatars3.githubusercontent.com/u/685051?v=4\" width=\"100px;\"/\u003e\u003cbr /\u003e\u003csub\u003e\u003cb\u003eamsul\u003c/b\u003e\u003c/sub\u003e](http://amsul.ca)\u003cbr /\u003e[💻](https://github.com/Amsul/react-translated/commits?author=amsul \"Code\") [🎨](#design-amsul \"Design\") [📖](https://github.com/Amsul/react-translated/commits?author=amsul \"Documentation\") [💡](#example-amsul \"Examples\") [🔧](#tool-amsul \"Tools\") | [\u003cimg src=\"https://avatars2.githubusercontent.com/u/4886073?v=4\" width=\"100px;\"/\u003e\u003cbr /\u003e\u003csub\u003e\u003cb\u003eJohnson Su\u003c/b\u003e\u003c/sub\u003e](https://johnsonsu.com)\u003cbr /\u003e[🐛](https://github.com/Amsul/react-translated/issues?q=author%3Ajohnsonsu \"Bug reports\") [💻](https://github.com/Amsul/react-translated/commits?author=johnsonsu \"Code\") |\n| :---: | :---: |\n\u003c!-- ALL-CONTRIBUTORS-LIST:END --\u003e\n\n👋 Interested becoming a contributor too?\n\nAwesome! This project follows the [all-contributors](https://github.com/kentcdodds/all-contributors)\nspecification. Contributions of any kind are welcome!\n\nYou may also want to take a look at our [TODOs](#todos) below and make sure to give our [Contributing](https://github.com/amsul/react-translated/blob/master/CONTRIBUTING.md) guide a read.\n\n\u003cbr /\u003e\n\n## TODOs\n\n* Add tests using [Jest](https://facebook.github.io/jest/)\n\n\u003cbr /\u003e\n\n## License\n\nLicensed under [MIT](https://github.com/amsul/react-translated/blob/master/LICENSE).\n\n© 2019 [Amsul](http://twitter.com/amsul_)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Famsul%2Freact-translated","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Famsul%2Freact-translated","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Famsul%2Freact-translated/lists"}