{"id":21184400,"url":"https://github.com/yankouskia/localize-react","last_synced_at":"2025-07-10T00:33:22.197Z","repository":{"id":35032559,"uuid":"198681941","full_name":"yankouskia/localize-react","owner":"yankouskia","description":"✈️ Lightweight React Localization Library 🇺🇸","archived":false,"fork":false,"pushed_at":"2023-01-06T02:02:52.000Z","size":1282,"stargazers_count":53,"open_issues_count":13,"forks_count":4,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-11-08T11:42:16.805Z","etag":null,"topics":["l10n","lightweight","localization","react"],"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/yankouskia.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-07-24T17:34:56.000Z","updated_at":"2024-06-07T07:50:27.000Z","dependencies_parsed_at":"2023-01-15T12:25:22.112Z","dependency_job_id":null,"html_url":"https://github.com/yankouskia/localize-react","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yankouskia%2Flocalize-react","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yankouskia%2Flocalize-react/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yankouskia%2Flocalize-react/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yankouskia%2Flocalize-react/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yankouskia","download_url":"https://codeload.github.com/yankouskia/localize-react/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225606600,"owners_count":17495551,"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":["l10n","lightweight","localization","react"],"created_at":"2024-11-20T18:01:38.215Z","updated_at":"2024-11-20T18:03:03.457Z","avatar_url":"https://github.com/yankouskia.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![CircleCI](https://circleci.com/gh/yankouskia/localize-react.svg?style=shield)](https://circleci.com/gh/yankouskia/localize-react) [![Codecov Coverage](https://img.shields.io/codecov/c/github/yankouskia/localize-react/master.svg?style=flat-square)](https://codecov.io/gh/yankouskia/localize-react/) [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/yankouskia/localize-react/pulls) [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/yankouskia/localize-react/blob/master/LICENSE) ![GitHub stars](https://img.shields.io/github/stars/yankouskia/localize-react.svg?style=social)\n\n[![NPM](https://nodei.co/npm/localize-react.png?downloads=true)](https://www.npmjs.com/package/localize-react)\n\n# localize-react\n\n✈️ Lightweight React Localization Library 🇺🇸\n\n## Motivation\n\nCreating really simple lightweight library for localization in React applications without any dependencies, which is built on top of new [React Context Api](https://reactjs.org/docs/context.html)\n\nLibrary has just **737 Bytes** gzipped size\n\n\n## Installation\n\nnpm:\n\n```sh\nnpm install localize-react --save\n```\n\nyarn:\n\n```sh\nyarn add localize-react\n```\n\n## API\n\n### Provider \u0026 Consumer\n\n`LocalizationProvider` is used to provide data for translations into React context. The root application component should be wrapped into `LocalizationProvider`. Component has the next props:\n- `children` - children to render\n- `locale` - [OPTIONAL] locale to be used for translations. If locale is not specified regular translations object will be used as map of `{ key: translations }`\n- `translations` - object with translations\n- `disableCache` - boolean variable to disable cache on runtime (`false` by default). Setting this to `true` could affect runtime performance, but could be useful for development.\n\nExample:\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport { LocalizationConsumer, LocalizationProvider } from 'localize-react';\n\nconst TRANSLATIONS = {\n  en: {\n    name: 'Alex',\n  },\n};\n\nconst App = () =\u003e (\n  \u003cLocalizationProvider\n    disableCache\n    locale=\"en\"\n    translations={TRANSLATIONS}\n  \u003e\n    \u003cLocalizationConsumer\u003e\n      {({ translate }) =\u003e translate('name')}\n    \u003c/LocalizationConsumer\u003e\n  \u003c/LocalizationProvider\u003e\n);\n\nReactDOM.render(\u003cApp /\u003e, node); // \"Alex\" will be rendered\n```\n\n### Message\n\n`Message` component is used to provide translated message by specified key, which should be passed via props. Component has the next props:\n- `descriptor` - translation key (descriptor)\n- `defaultMessage` - message to be used in case translation is not provided (values object are applied to default message as well)\n- `values` - possible values to use with template string (Template should be passed in next format: `Hello {{name}}`)\n\nExample:\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport { LocalizationProvider, Message } from 'localize-react';\n\nconst TRANSLATIONS = {\n  en: {\n    name: 'Alex',\n  },\n};\n\nconst App = () =\u003e (\n  \u003cLocalizationProvider\n    locale=\"en\"\n    translations={TRANSLATIONS}\n  \u003e\n    \u003cMessage descriptor=\"name\" /\u003e\n  \u003c/LocalizationProvider\u003e\n);\n\nReactDOM.render(\u003cApp /\u003e, node); // \"Alex\" will be rendered\n```\n\nTo use with templates:\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport { LocalizationProvider, Message } from 'localize-react';\n\nconst TRANSLATIONS = {\n  en: {\n    name: 'Hello, {{name}}!',\n  },\n};\n\nconst App = () =\u003e (\n  \u003cLocalizationProvider\n    locale=\"en\"\n    translations={TRANSLATIONS}\n  \u003e\n    \u003cMessage descriptor=\"name\" values={{ name: 'Alex' }} /\u003e\n  \u003c/LocalizationProvider\u003e\n);\n\nReactDOM.render(\u003cApp /\u003e, node); // \"Alex\" will be rendered\n```\n\nTo use with default message:\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport { LocalizationProvider, Message } from 'localize-react';\n\nconst TRANSLATIONS = {\n  en: {},\n};\n\nconst App = () =\u003e (\n  \u003cLocalizationProvider\n    locale=\"en\"\n    translations={TRANSLATIONS}\n  \u003e\n    \u003cMessage\n      descriptor=\"name\"\n      defaultMessage=\"Hello, {{name}}!\"\n      values={{ name: 'Alex' }}\n    /\u003e\n  \u003c/LocalizationProvider\u003e\n);\n\nReactDOM.render(\u003cApp /\u003e, node); // \"Alex\" will be rendered\n```\n\n### useLocalize\n\n`useLocalize` hook is used to provide localization context, which can be used for translation.\n\n**NOTE**\n\nKeep in mind, that hooks are not supported in class components!\n\nExample:\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport { LocalizationProvider, useLocalize } from 'localize-react';\n\nconst TRANSLATIONS = {\n  en: {\n    name: 'Alex',\n  },\n};\n\nfunction Test() {\n  const { translate } = useLocalize();\n\n  return translate('name');\n}\n\nconst App = () =\u003e {\n\n  return (\n    \u003cLocalizationProvider\n      locale=\"en\"\n      translations={TRANSLATIONS}\n    \u003e\n      \u003cTest /\u003e\n    \u003c/LocalizationProvider\u003e\n  );\n}\n\nReactDOM.render(\u003cApp /\u003e, node); // \"Alex\" will be rendered\n```\n\n### Templates\n\nIt's possible to use templates inside translation strings with highlighting templates using double curly braces. To pass correpospondent values:\n\n```js\n  const translation = translate('My name is {{name}}. I am {{age}}', { name: 'Alex', age: 25 });\n```\n\nOr with React component:\n\n```js\n  \u003cMessage descriptor=\"My name is {{name}}. I am {{age}}\" values={{ name: 'Alex', age: 25 }} /\u003e\n```\n\n### contextType\n\nAlternative way of usage inside class components:\n\n```js\nimport React from 'react';\nimport { LocalizationContext, LocalizationProvider } from 'localize-react';\n\nconst TRANSLATIONS = {\n  en: {\n    name: 'Alex',\n  },\n};\n\n\nclass Translation extends React.PureComponent {\n  render() {\n    return (\n      \u003cspan\u003e\n        {this.context.translate('name')}\n      \u003c/span\u003e\n    )\n  }\n}\n\nTranslation.contextType = LocalizationContext;\n\nconst App = () =\u003e {\n  return (\n    \u003cLocalizationProvider\n      locale=\"en\"\n      translations={TRANSLATIONS}\n    \u003e\n      \u003cTranslation /\u003e\n    \u003c/LocalizationProvider\u003e\n  );\n}\n\nReactDOM.render(\u003cApp /\u003e, node); // \"Alex\" will be rendered\n```\n\n### locale\nLocale could be passed in short or long option.\n\n\nValid examples:\n\n```\nen-us\nEN_US\nen\neN-uS\n```\n\n### translations\nTranslations could be passed in any object form (plain or with deep properties)\n\nValid examples:\n\n```js\nconst translations = {\n  n: {\n    a: {\n      m: {\n        e: 'Alex',\n      },\n    },\n  },\n},\n```\n\nYou could use key with dot delimiter to access that property:\n\n```js\n\u003cMessage descriptor=\"n.a.m.e\" /\u003e // will print \"Alex\"\n```\n\nIf there is no exact match in translations, then the value of locale will be sanitized and formatted to **lower_case_separate_by_underscore**. Make sure you provide translations object with keys in this format. If translations for long locale will not be found, and translations will be found for shorten alternative - that version will be used\n\n## Restriction\n\nAt least `React 16.8.0` is required to use this library, because new React Context API \u0026 React Hooks\n\n## Contributing\n\n`localize-react` is open-source library, opened for contributions\n\n### Tests\n\n**Current test coverage is 100%**\n\n`jest` is used for tests. To run tests:\n\n```sh\nyarn test\n```\n\n### License\n\nlocalize-react is [MIT licensed](https://github.com/yankouskia/localize-react/blob/master/LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyankouskia%2Flocalize-react","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyankouskia%2Flocalize-react","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyankouskia%2Flocalize-react/lists"}