{"id":13422507,"url":"https://github.com/fakundo/react-localized","last_synced_at":"2025-04-11T14:50:24.188Z","repository":{"id":57329518,"uuid":"109765283","full_name":"fakundo/react-localized","owner":"fakundo","description":"Internationalization for React and Preact components based on gettext format","archived":false,"fork":false,"pushed_at":"2022-07-24T16:40:21.000Z","size":696,"stargazers_count":10,"open_issues_count":2,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-02T21:39:49.107Z","etag":null,"topics":["gettext","i18n","internationalization","preact","react"],"latest_commit_sha":null,"homepage":"https://fakundo.github.io/react-localized/","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fakundo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-11-07T00:16:17.000Z","updated_at":"2025-02-06T04:53:57.000Z","dependencies_parsed_at":"2022-09-14T18:51:24.922Z","dependency_job_id":null,"html_url":"https://github.com/fakundo/react-localized","commit_stats":null,"previous_names":[],"tags_count":43,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fakundo%2Freact-localized","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fakundo%2Freact-localized/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fakundo%2Freact-localized/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fakundo%2Freact-localized/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fakundo","download_url":"https://codeload.github.com/fakundo/react-localized/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248424060,"owners_count":21101100,"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":["gettext","i18n","internationalization","preact","react"],"created_at":"2024-07-30T23:00:46.465Z","updated_at":"2025-04-11T14:50:24.161Z","avatar_url":"https://github.com/fakundo.png","language":"TypeScript","readme":"# react-localized / preact-localized\n\n[![npm](https://img.shields.io/npm/v/react-localized.svg)](https://www.npmjs.com/package/react-localized)\n[![npm](https://img.shields.io/npm/v/preact-localized.svg)](https://www.npmjs.com/package/preact-localized)\n\nComplete internationalization tool for React and Preact components.\n\nFeatures:\n\n- based on [gettext](https://www.gnu.org/software/gettext/manual/gettext.html) format, uses translation `.po` files\n\n- translation strings for `.po` files can be extracted automatically from project source files\n\n- plural forms are supported\n\n- gettext function aliases are supported\n\n- JavaScript string templates are supported\n\n- locale data is extendable, for example by adding date formats, currencies, etc.\n\n- locale data can be separated from the main bundle by using dynamic import\n\n### Basic component example\n\n```js\nimport { useLocales } from 'react-localized' // or from 'preact-localized'\n\nexport default () =\u003e {\n  const { gettext } = useLocales()\n  return (\n    \u003c\u003e\n      { gettext('Hello, World!') } // Привет, Мир!\n    \u003c/\u003e\n  )\n}\n```\n\n### Complex example\n\n[Live demo](http://fakundo.github.io/react-localized/)\n|\n[Source](https://github.com/fakundo/react-localized/tree/master/examples)\n\n### Installation and usage\n\n#### 1. Use existing `.po` files with translation messages or generate them from your project source files\n\nTo generate `.po` files you can use the extractor CLI tool. Extractor searches your project source files for functions like `gettext`, `ngettext`, etc. Extractor also can update existing `.po` files without erasing existing translations in those files.\n\n```console\nnpm i react-localized-extractor\n```\n\n```console\nreact-localized-extractor [options]\n\nOptions:\n  --version      Show version number                                   [boolean]\n  --help         Show help                                             [boolean]\n  --locales, -l  List of desired locales (comma separated)   [string] [required]\n  --source, -s   Source files pattern\n                               [string] [default: \"./src/**/*.@(js|ts|jsx|tsx)\"]\n  --output, -o   Output .po files directory      [string] [default: \"./locales\"]\n  --alias, -a    Function alias                                         [string]\n  --save-pot     Should create catalog .pot file in output directory\n                                                      [boolean] [default: false]\n```\n\n```console\nreact-localized-extractor -l ru\n```\n\n#### 2. Modify `.po` files to add / edit translation\n\nUse your favourite editor for `.po` files. I recommend you to use `Poedit`.\n\n#### 3. Import `.po` files into your project by using webpack loader (optional)\n\nThe loader transforms the contents of the `.po` files to JSON using `gettext-parser`. Therefore, if you are not using `webpack`, you can generate JSON files using this parser and import them into your project. \n\n```console\nnpm i react-localized-loader\n```\n\n```js\nmodule: {\n  rules: [\n    {\n      test: /\\.po$/,\n      use: 'react-localized-loader'\n    }\n  ]\n}\n```\n\n#### 4. Install `react-localized` or `preact-localized`\n\n```console\n// for React\nnpm i react-localized \n\n// for Preact\nnpm i preact-localized\n```\n\n#### 5. Create data object for each locale\n\nUse the `createLocale` function from the package. The first argument is for translation messages taken from `.po` file. The second argument is for extra data such as date formats, currencies, and so on.\n\nExample of the file `ru.js` for russian locale\n\n```js\nimport { createLocale } from 'react-localized' // or from 'preact-localized'\nimport messages from 'messages/ru.po'\n\nconst extra = { ... }\n\nexport default createLocale(messages, extra)\n```\n\n#### 6. Render provider component\n\n```js\nimport { LocalizedProvider } from 'react-localized' // or from 'preact-localized'\n\nimport fr from 'fr.js'\nimport de from 'de.js'\n\nconst ru = () =\u003e import('ru.js').then(data =\u003e data.default) // separated from the main bundle\n\nconst locales = { fr, de, ru }\n\nexport default () =\u003e (\n  \u003cLocalizedProvider \n    locales={locales} \n    selected=\"fr\"\n  \u003e\n    { ({ localeReady }) =\u003e (\n      localeReady \n        ? 'render children' \n        : 'loading locale'\n    ) }\n  \u003c/LocalizedProvider\u003e\n)\n```\n\n#### 7. Localize components using hook\n\n```js\nimport { useLocales } from 'react-localized' // or from 'preact-localized'\n\nexport default () =\u003e {\n  const { gettext, ngettext, ...extra } = useLocales()\n  return (\n    \u003c\u003e\n      { gettext('Hello, World!') } // Привет, Мир!\n      { ngettext('apple', 'apples', 1) } // яблоко\n      { ngettext('apple', 'apples', 2) } // яблока\n      { ngettext('apple', 'apples', 5) } // яблок\n    \u003c/\u003e\n  )\n}\n```\n\n#### 8. Localize components using HOC (Higher-Order Component)\n\n```js\nimport { withLocales } from 'react-localized' // or from 'preact-localized'\n\nclass LocalizedComponent extends Component {\n  render() {\n    const { pgettext, formatDate, formats } = this.props // 'formatDate' and 'formats' are extra data passed to the 'createLocale'\n    return (\n      \u003c\u003e\n        { pgettext('Context', 'Text with context') } // Текст с контекстом\n        { formatDate(new Date(), formats.date) } // 1 января 2020\n      \u003c/\u003e\n    )\n  }\n}\n\nexport default withLocales()(LocalizedComponent)\n```\n### Using string templates\n\n```js\nimport { useLocales } from 'react-localized' // or from 'preact-localized'\n\nexport default () =\u003e {\n  const { gettext, ngettext, pgettext, npgettext } = useLocales()\n  const name = 'Anna'\n  const i = 2\n  return (\n    \u003c\u003e\n      { gettext`My name is ${name}` } // Мое имя Anna\n      { ngettext`${i} apple``${i} apples`(i) } // 2 яблока\n      { pgettext('Ctx')`My name is ${name}` }\n      { npgettext('Ctx')`${i} apple``${i} apples`(i) }\n    \u003c/\u003e\n  )\n}\n```\n\n### Using function aliases\n\nUse `LocalizedProvider` `alias` prop to define function aliases.\n\nExample of alias for `gettext` only\n\n```js\n\u003cLocalizedProvider alias=\"__\" /\u003e\n```\n\nExample of aliases for `gettext` and `ngettext`\n\n```js\n\u003cLocalizedProvider alias={{ gettext: '__', ngettext: '__n' }} /\u003e\n```\n\nExample of alias usage\n\n```js\nimport { useLocales } from 'react-localized' // or from 'preact-localized'\n\nexport default () =\u003e {\n  const { __, __n } = useLocales()\n  const name = 'Anna'\n  return (\n    \u003c\u003e\n      { __('Hello, World!') }\n      { __`My name is ${name}` }\n      { __n`apple``apples`(5) }\n    \u003c/\u003e\n  )\n}\n```\n\nAlso configure extractor.\n\nExample of alias for `gettext` only\n\n```console\nreact-localized-extractor -l ru -a __\n```\n\nExample of aliases for `gettext` and `ngettext`\n\n```console\nreact-localized-extractor -l ru -a.gettext __ -a.ngettext __n\n```\n\n### API\n\n#### LocalizedProvider props\n\n- `locales` - defined locales\n- `selected` - selected locale (default `en`)\n- `alias` - function aliases (string or object)\n- `children({ localeReady })`\n\n#### Data returned by hook / props passed to the child of the HOC\n\n- `locale`\n- `gettext(text)`\n- `ngettext(text, textPlural, n)`\n- `pgettext(context, text)`\n- `npgettext(context, text, textPlural, n)`\n- `...aliases` - defined function aliases\n- `...extra` - extra data passed to the `createLocale`\n\n### License\n\nMIT\n","funding_links":[],"categories":["Utilities"],"sub_categories":["i18n"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffakundo%2Freact-localized","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffakundo%2Freact-localized","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffakundo%2Freact-localized/lists"}