{"id":19927859,"url":"https://github.com/calidion/node-i18n","last_synced_at":"2025-04-28T13:34:10.089Z","repository":{"id":34872381,"uuid":"185935665","full_name":"calidion/node-i18n","owner":"calidion","description":"Simplest I18n Solution for all platforms","archived":false,"fork":false,"pushed_at":"2023-01-05T01:45:37.000Z","size":785,"stargazers_count":3,"open_issues_count":13,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-30T16:01:57.643Z","etag":null,"topics":["i18n","node","node-i18n"],"latest_commit_sha":null,"homepage":"https://t1bao.com","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/calidion.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-05-10T06:59:34.000Z","updated_at":"2022-06-10T01:40:00.000Z","dependencies_parsed_at":"2023-01-15T09:47:07.066Z","dependency_job_id":null,"html_url":"https://github.com/calidion/node-i18n","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/calidion%2Fnode-i18n","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/calidion%2Fnode-i18n/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/calidion%2Fnode-i18n/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/calidion%2Fnode-i18n/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/calidion","download_url":"https://codeload.github.com/calidion/node-i18n/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251320197,"owners_count":21570540,"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","node","node-i18n"],"created_at":"2024-11-12T22:35:30.446Z","updated_at":"2025-04-28T13:34:10.028Z","avatar_url":"https://github.com/calidion.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Build Status](https://travis-ci.org/calidion/node-i18n.svg?branch=master)](https://travis-ci.org/calidion/node-i18n)\n[![Coverage Status](https://coveralls.io/repos/github/calidion/node-i18n/badge.svg?branch=master)](https://coveralls.io/github/calidion/node-i18n?branch=master)\n[![MIT license](http://img.shields.io/badge/license-MIT-brightgreen.svg)](http://opensource.org/licenses/MIT)\n\n# Simplest I18n Solution for all platforms\n\nThis may be the simplest i18n module for nodejs and the web.\n\n# Locale Directory\n\n```\nlocales/\n├── en.json\n├── en-US.json\n├── kr.json\n└── zh-CN.json\n```\n\n\u003e All locale files must be kept within one directory, with file extension `.json` and in json format.\n\n# Install\n\n```\nyarn add node-i18n-core\n# or \nnpm install node-i18n-core\n```\n# Usage\n\n## import\n\n```ts\nimport { I18n, II18nOption, IStorable, IListener } from 'node-i18n-core';\n```\n\n## Options\n\nThere are three options in II18nOption.\n\n1. `current`: the current locale.\n2. `url`: an absolute path of file system or uri.\n3. `loader`: an async function through which locale data is read asynchronously\n\n## Loaders\n\nThere are two Loaders, one is `FileSystemLoader`, the other is `HTTPLoader`.\n\nAs their names imply, one is for file system, the other is for web served json files.\n\n### FileSystemLoader\n\n\u003e `url` should be an absolute path\n\n```ts\n  const options: II18nOption = {\n    current: 'en',\n    loader: FileSystemLoader,\n    url: path.resolve(__dirname, './locales/'),\n  };\n```\n\n### HTTPLoader\n\n\u003e `url` should be an uri path where json files are served\n\n```ts\n  const options: II18nOption = {\n    current: 'en',\n    loader: HTTPLoader,\n    url: 'http://www.yourdomain.com/xxx/locales/'\n  };\n```\n\n## Create I18n Instance\n\nYou can specify your storage or not.\n\n```ts\nconst i18n = new I18n(options);\n\n// Use windows.localStorage as the IStorable interface on the web\nconst i18n = new I18n(options, windows.localStorage);\n```\n\n## Init data\n\nTo use translator, data must be initialized at first:\n\n```ts\nawait i18n.init();\n```\n\nNow you have the json data prepared for access.\n\n## Get Locale\n\n```ts\n// Get Locale loaded from windows.localStorage\ni18n.getLocale();\n```\n\n## Define Customzied IStorable object\n\n\n```ts\n\n// Define an IStorable for yourself\nconst storage: IStorable = {\n    getItem: (key: string): string =\u003e {\n      return data[key];\n    },\n    setItem: (key: string, value: string): void =\u003e {\n      data[key] = value;\n    }\n};\n\n// Get Locale from a customzied IStorable instance\ni18n.getLocale(storage);\n```\n\n## Set Locale (Async)\n\n```ts\n// Set Locale to constructed storage\nawait i18n.setLocale('zh-CN');\n\n// Set Locale to a temporay storage\nawait i18n.setLocale('zh-CN', storage);\n```\n\n\n## Add Change Observer\n\n```ts\n// Listen the locale change infomation\nconst listener: IListener = (from, to) =\u003e {\n  console.log(\"locale has changed from: \" + from + \" to :\" + to );\n};\ni18n.listen(listener);\n```\n\n## Get Translation (Async)\n\n```ts\n// Get translated strings async\nawait i18n._('TITLE');\nawait i18n._('TITLE.SUBTITLE.SUBTITLE');\nawait i18n._('TITLE', 'zh-CN');\nawait i18n._('TITLE.SUBTITLE.SUBTITLE', 'ja');\n\n// Get translated strings, no await need, return '' if not loaded or error.\n i18n._sync('TITLE');\n i18n._sync('TITLE.SUBTITLE.SUBTITLE');\n i18n._sync('TITLE', 'zh-CN');\n i18n._sync('TITLE.SUBTITLE.SUBTITLE', 'ja');\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcalidion%2Fnode-i18n","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcalidion%2Fnode-i18n","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcalidion%2Fnode-i18n/lists"}