{"id":13525591,"url":"https://github.com/DenysVuika/preact-translate","last_synced_at":"2025-04-01T05:31:55.488Z","repository":{"id":39420786,"uuid":"197387997","full_name":"DenysVuika/preact-translate","owner":"DenysVuika","description":"Minimalistic translate (i18n) library for Preact","archived":false,"fork":false,"pushed_at":"2024-01-20T21:44:38.000Z","size":3372,"stargazers_count":64,"open_issues_count":10,"forks_count":6,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-07-19T01:22:16.458Z","etag":null,"topics":["i18n","preact","preact-components","preact-x","translation","typescript"],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","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/DenysVuika.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"DenysVuika"}},"created_at":"2019-07-17T12:46:02.000Z","updated_at":"2024-02-11T17:16:14.000Z","dependencies_parsed_at":"2024-06-18T20:01:05.188Z","dependency_job_id":"d8a790a7-fce9-43b2-a19e-95e5733f70f6","html_url":"https://github.com/DenysVuika/preact-translate","commit_stats":{"total_commits":412,"total_committers":6,"mean_commits":68.66666666666667,"dds":0.5728155339805825,"last_synced_commit":"f6aaf3f61e063eb41ede84cfa4be6aaebba0fed5"},"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DenysVuika%2Fpreact-translate","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DenysVuika%2Fpreact-translate/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DenysVuika%2Fpreact-translate/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DenysVuika%2Fpreact-translate/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/DenysVuika","download_url":"https://codeload.github.com/DenysVuika/preact-translate/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":213474822,"owners_count":15592634,"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","preact","preact-components","preact-x","translation","typescript"],"created_at":"2024-08-01T06:01:20.214Z","updated_at":"2025-04-01T05:31:55.481Z","avatar_url":"https://github.com/DenysVuika.png","language":"TypeScript","funding_links":["https://github.com/sponsors/DenysVuika","https://www.buymeacoffee.com/denys"],"categories":["TypeScript","Uncategorized"],"sub_categories":["Uncategorized"],"readme":"# preact-translate\n\nMinimalistic translate (i18n) library for Preact\n\n[![Build Status](https://dev.azure.com/denysvuika/preact-translate/_apis/build/status/DenysVuika.preact-translate?branchName=master)](https://dev.azure.com/denysvuika/preact-translate/_build/latest?definitionId=3\u0026branchName=master)\n\nBundle size: ~1KB  \nLive example: [Sandbox](https://codesandbox.io/embed/preact-translate-npm-tpe4f)\n\n\u003ca href=\"https://www.buymeacoffee.com/denys\" target=\"_blank\"\u003e\n  \u003cimg src=\"https://cdn.buymeacoffee.com/buttons/default-orange.png\" alt=\"Buy Me A Coffee\" height=\"51\" width=\"217\"\u003e\n\u003c/a\u003e\n\n## Installing\n\nMinimal requirements: **Preact 10.0.0**\n\nUsing Yarn:\n\n```sh\nyarn add @denysvuika/preact-translate\n```\n\nUsing NPM:\n\n```sh\nnpm i @denysvuika/preact-translate\n```\n\n## Basic usage\n\nYou should wrap your application with the `TranslateProvider` component:\n\n```jsx\nimport { TranslateProvider } from '@denysvuika/preact-translate';\n\nexport default function App() {\n  return (\n    \u003cTranslateProvider\u003e\n      \u003cMainComponent /\u003e\n    \u003c/TranslateProvider\u003e\n  );\n}\n```\n\nCreate an `assets/en.json` file with the following content:\n\n```json\n{\n  \"title\": \"Hello\",\n  \"subtitle\": \"World\"\n}\n```\n\nCreate a second file `assets/ua.json` to be able to switch between different resources.\n\n```json\n{\n  \"title\": \"[ua] Hello\",\n  \"subtitle\": \"[ua] World\"\n}\n```\n\nYou can now use the `TranslateContext` in your components to access the translation API and data:\n\n```jsx\nimport { useContext } from 'preact/hooks';\nimport { TranslateContext } from '@denysvuika/preact-translate';\n\nexport default function MainComponent() {\n  const { setLang, t, lang } = useContext(TranslateContext);\n\n  return (\n    \u003cdiv\u003e\n      \u003cdiv\u003eLang: {lang}\u003c/div\u003e\n      \u003cdiv\u003e{t('title')}\u003c/div\u003e\n      \u003cdiv\u003e{t('subtitle')}\u003c/div\u003e\n      \u003cdiv\u003e\n        \u003cbutton onClick={() =\u003e setLang('en')}\u003eEN\u003c/button\u003e\n        \u003cbutton onClick={() =\u003e setLang('ua')}\u003eUA\u003c/button\u003e\n      \u003c/div\u003e\n    \u003c/div\u003e\n  );\n}\n```\n\nFor the `EN` locale you should see:\n\n```text\nLang: en\nHello\nWorld\n```\n\nFor the `UA` locale you should see:\n\n```text\nLang: ua\n[ua] Hello\n[ua] World\n```\n\nThe language loading performs on demand.\nThe `en.json` gets loaded and cached only when first requested by your application.\n\n## Configuring assets root folder\n\nBy default, the `assets` folder is used to fetch locales.\nYou can change it via the `TranslateProvider.root` property:\n\n```jsx\nimport { TranslateProvider } from '@denysvuika/preact-translate';\n\nexport default function App() {\n  return (\n    \u003cTranslateProvider root=\"i18n\"\u003e\n      \u003cMainComponent /\u003e\n    \u003c/TranslateProvider\u003e\n  );\n}\n```\n\n## Formatted translations\n\nYou can use runtime string substitution when translating text\n\n```json\n{\n  \"hello\": \"Hello, {name}\"\n}\n```\n\nThen in the JSX:\n\n```jsx\n\u003cdiv\u003e{t('hello', { name: 'Bob' })}\u003c/div\u003e\n```\n\n## Nested translations\n\nThe library supports complex objects with nested levels.\n\nPut the following in the `en.json` file:\n\n```json\n{\n  \"messages\": {\n    \"errors\": {\n      \"404\": \"Sorry, not found\"\n    }\n  }\n}\n```\n\nThen in the JSX use:\n\n```jsx\n\u003cdiv\u003e{t('messages.errors.404')}\u003c/div\u003e\n```\n\nYou can also use composite strings like the following:\n\n```json\n{\n  \"messages.errors.404\": \"Sorry, not found\"\n}\n```\n\n## Default language\n\nYou can set the default language to use with the application by assigning the `TranslateProvider.lang` property.\n\n```html\n\u003cTranslateProvider lang=\"ua\"\u003e\n  \u003cApplication /\u003e\n\u003c/TranslateProvider\u003e\n```\n\nPlease note that in this case provider is going to load and cache two locales at startup:\n`en.json` (as a fallback) and `ua.json` (as an active lang).\n\n## Custom translation data\n\nYou can use `TranslateProvider.translations` property to provide a custom translation data from the code.\nThat helps with unit testing as well.\n\n```jsx\nconst data = {\n  en: {\n    messages: {\n      404: 'Not found'\n    }\n  }\n};\n\n\u003cTranslateProvider translations={data}\u003e\n  \u003cApplication /\u003e\n\u003c/TranslateProvider\u003e;\n```\n\nNote that the `TranslateProvider` is not going to fetch translation files for the `en` locale,\nand will use your custom data instead.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FDenysVuika%2Fpreact-translate","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FDenysVuika%2Fpreact-translate","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FDenysVuika%2Fpreact-translate/lists"}