{"id":20969958,"url":"https://github.com/cjy0208/i18nshell","last_synced_at":"2025-05-14T11:32:54.735Z","repository":{"id":36780339,"uuid":"230275604","full_name":"CJY0208/i18nshell","owner":"CJY0208","description":"1.9kb gzipped tiny i18n shell, splitable instances, lazyloadable resources, customizable currency, time or jsx i18n support","archived":false,"fork":false,"pushed_at":"2023-03-04T05:31:30.000Z","size":230,"stargazers_count":4,"open_issues_count":5,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-11-16T01:50:00.641Z","etag":null,"topics":["i18n","lazyload","splitable","tiny","translation"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/i18nshell","language":"JavaScript","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/CJY0208.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":"2019-12-26T14:14:03.000Z","updated_at":"2023-04-18T09:32:38.000Z","dependencies_parsed_at":"2023-01-17T04:44:19.505Z","dependency_job_id":null,"html_url":"https://github.com/CJY0208/i18nshell","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CJY0208%2Fi18nshell","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CJY0208%2Fi18nshell/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CJY0208%2Fi18nshell/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CJY0208%2Fi18nshell/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/CJY0208","download_url":"https://codeload.github.com/CJY0208/i18nshell/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225292089,"owners_count":17451086,"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","lazyload","splitable","tiny","translation"],"created_at":"2024-11-19T03:55:23.881Z","updated_at":"2024-11-19T03:55:33.372Z","avatar_url":"https://github.com/CJY0208.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# I18nshell\n\n[![size](https://img.shields.io/bundlephobia/minzip/i18nshell.svg)](https://github.com/CJY0208/i18nshell)\n[![dm](https://img.shields.io/npm/dm/i18nshell.svg)](https://github.com/CJY0208/i18nshell)\n\nTiny i18n shell\n\nEnglish | [中文说明](./README_CN.md)\n\n---\n\n# Install\n\n```bash\nyarn add i18nshell\n# or\nnpm install i18nshell --save\n```\n\n---\n\n# Basic Usage\n\n```js\nimport I18n from 'i18nshell'\n\nconst i18n = new I18n({\n  types: {\n    default: {\n      resources: {\n        en: {\n          greet: 'Hello, {{name}}'\n        },\n        zh: {\n          greet: '你好，{{name}}'\n        }\n      }\n    }\n  }\n})\n\ntest()\nasync function test() {\n  await I18n.applyLng('en');\n  i18n.t('greet', {\n    name: 'CJY'\n  }) // get string 'Hello, CJY'\n\n  await I18n.applyLng('zh');\n  i18n.t('greet', {\n    name: 'CJY'\n  }) // get string '你好，CJY' \n}\n```\n\n---\n# Add customized i18n types such as currency, time, etc.\n\n`i18nshell` has simple text translation built in\n\nCurrency, time and other nationalization functions need to be implemented by expanding types\n\nUse `@` to select the type of translation you want to use\n\n```jsx\nimport I18n from 'i18nshell'\n\nconst i18n = new I18n({\n  types: {\n    price: {\n      resource: false, // Not using resources\n      format: (value) =\u003e {\n        const unit = {\n          en: '$',\n          zh: '¥'\n        }[I18n.lng]\n\n        return `${unit}${value}`\n      }\n    },\n    jsx: { \n      resources: {\n        en: {\n          clickHere: 'click {{here}}'\n        },\n        zh: {\n          clickHere: '点击 {{here}}'\n        }\n      },\n      format: (value, data) =\u003e (\n        \u003cFragment\u003e\n          {I18n.template(value, data, { split: true }).map((item, idx) =\u003e (\n            \u003cFragment key={idx}\u003e{item}\u003c/Fragment\u003e\n          ))}\n        \u003c/Fragment\u003e\n      )\n    }\n  }\n})\n\ntest()\nasync function test() {\n  await I18n.applyLng('en');\n  i18n.t('1000@price') // get string '$1000'\n\n  await I18n.applyLng('zh');\n  i18n.t('1000@price') // get string '¥1000'\n\n  // get jsx content\n  i18n.t('clickHere@jsx', {\n    here: (\n      \u003ca href=\"https://www.google.com\"\u003egoogle\u003c/a\u003e\n    )\n  })\n}\n```\n\n---\n# Lazyload resources\n\nThe following settings work with the `import (...)` syntax, en language packs are only loaded when needed\n\n```js\n// en.js\nexport default {\n  greet: 'Hello, {{name}}'\n}\n```\n\n```js\n// ./i18n.js\nimport I18n from 'i18nshell'\n\nnew I18n({\n  types: {\n    default: {\n      resources: {\n        en: I18n.load(() =\u003e import('./en.js')),\n        zh: {\n          greet: '你好，{{name}}'\n        }\n      }\n    }\n  }\n})\n```\n\n---\n# Split and inherit other language packs\n\nImplemented using the fallback configuration item\n\n```js\nimport I18n from 'i18nshell'\n\nconst i18n_1 = new I18n({\n  types: {\n    default: {\n      resources: {\n        zh: {\n          test1: '测试1'\n        }\n      }\n    }\n  }\n})\n\nconst i18n_2 = new I18n({\n  fallback: [i18n_1],\n  types: {\n    default: {\n      resources: {        \n        zh: {\n          test2: '测试2'\n        }\n      }\n    }\n  }\n})\n\nconst i18n_3 = new I18n({\n  fallback: [i18n_2],\n  types: {\n    default: {\n      resources: {        \n        zh: {\n          test3: '测试3'\n        }\n      }\n    }\n  }\n})\n\ntest()\nasync function test() {\n  await I18n.applyLng('zh')\n  i18n_3.t('test1') // get string '测试1' fallback i18n_2 -\u003e i18n_1\n  i18n_3.t('test2') // get string '测试2' fallback i18n_2\n  i18n_3.t('test3') // get string '测试3'\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcjy0208%2Fi18nshell","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcjy0208%2Fi18nshell","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcjy0208%2Fi18nshell/lists"}