|
| 1 | +# @intilfy/utils |
| 2 | + |
| 3 | +[![npm version][npm-version-src]][npm-version-href] |
| 4 | +[![npm downloads][npm-downloads-src]][npm-downloads-href] |
| 5 | +[![CI][ci-src]][ci-href] |
| 6 | + |
| 7 | +Collection of i18n utilities |
| 8 | + |
| 9 | +## 🌟 Features |
| 10 | + |
| 11 | +✅️ **Modern:** ES Modules first and respect Web Standard and ECMAScript |
| 12 | +Internationalization APIs |
| 13 | + |
| 14 | +✅️ **Compatible:** support CommonJS and various JS environments |
| 15 | + |
| 16 | +✅️️ **Minimal:** Small and fully tree-shakable |
| 17 | + |
| 18 | +✅️️ **Type Strong:** Written in TypeScript, with full JSdoc |
| 19 | + |
| 20 | +## 💿 Installation |
| 21 | + |
| 22 | +### 🐢 Node.js |
| 23 | + |
| 24 | +```sh |
| 25 | +# Using npm |
| 26 | +npm install @intlify/utils |
| 27 | + |
| 28 | +# Using yarn |
| 29 | +yarn add @intlify/utils |
| 30 | + |
| 31 | +# Using pnpm |
| 32 | +pnpm add @intlify/utils |
| 33 | +``` |
| 34 | + |
| 35 | +<details> |
| 36 | + <summary>Using Edge Releases</summary> |
| 37 | + |
| 38 | +If you are directly using `@intlify/utils` as a dependency: |
| 39 | + |
| 40 | +```json |
| 41 | +{ |
| 42 | + "dependencies": { |
| 43 | + "@intlify/utils": "npm:@intlify/utils-edge@latest" |
| 44 | + } |
| 45 | +} |
| 46 | +``` |
| 47 | + |
| 48 | +**Note:** Make sure to recreate lockfile and `node_modules` after reinstall to avoid hoisting issues. |
| 49 | + |
| 50 | +</details> |
| 51 | + |
| 52 | +### 🦕 Deno |
| 53 | + |
| 54 | +You can install via `import`. |
| 55 | + |
| 56 | +in your code: |
| 57 | + |
| 58 | +```ts |
| 59 | +/** |
| 60 | + * you can install via other CDN URL such as skypack, |
| 61 | + * or, you can also use import maps |
| 62 | + * https://docs.deno.com/runtime/manual/basics/import_maps |
| 63 | + */ |
| 64 | +import { ... } from 'https://esm.sh/@intlify/utils' |
| 65 | + |
| 66 | +// something todo |
| 67 | +// ... |
| 68 | +``` |
| 69 | + |
| 70 | +<details> |
| 71 | + <summary>Using Edge Releases</summary> |
| 72 | + |
| 73 | +```ts |
| 74 | +import { ... } from 'https://esm.sh/@intlify/utils-edge' |
| 75 | + |
| 76 | +// something todo |
| 77 | +// ... |
| 78 | +``` |
| 79 | + |
| 80 | +</details> |
| 81 | + |
| 82 | +### 🥟 Bun |
| 83 | + |
| 84 | +```sh |
| 85 | +bun install @intlify/utils |
| 86 | +``` |
| 87 | + |
| 88 | +### 🌍 Browser |
| 89 | + |
| 90 | +in your HTML: |
| 91 | + |
| 92 | +```html |
| 93 | +<script type="module"> |
| 94 | +/** |
| 95 | + * you can install via other CDN URL such as skypack, |
| 96 | + * or, you can also use import maps |
| 97 | + */ |
| 98 | +import { isLocale } from 'https://esm.sh/@intlify/utils' |
| 99 | +
|
| 100 | +// something todo |
| 101 | +// ... |
| 102 | +</script> |
| 103 | +``` |
| 104 | + |
| 105 | +## 🍭 Playground |
| 106 | + |
| 107 | +You can play the below examples: |
| 108 | + |
| 109 | +- 🐢 [Node.js](https://github.com/intlify/utils/tree/main/examples/node): |
| 110 | + `npm run play:node` |
| 111 | +- 🦕 [Deno](https://github.com/intlify/utils/tree/main/examples/deno): |
| 112 | + `npm run play:deno` |
| 113 | +- 🥟 [Bun](https://github.com/intlify/utils/tree/main/examples/bun): |
| 114 | + `npm run play:bun` |
| 115 | +- 🌍 [Browser](https://github.com/intlify/utils/tree/main/examples/browser): |
| 116 | + `npm run play:browser` |
| 117 | + |
| 118 | +## 🔨 Utilities |
| 119 | + |
| 120 | +### Common |
| 121 | + |
| 122 | +- `isLocale` |
| 123 | +- `toLocale` |
| 124 | +- `parseAcceptLanguage` |
| 125 | +- `validateLangTag` |
| 126 | +- `normalizeLanguageName` |
| 127 | + |
| 128 | +You can do `import { ... } from '@intlify/utils'` the above utilities |
| 129 | + |
| 130 | +### Navigator |
| 131 | + |
| 132 | +- `getNavigatorLocales` |
| 133 | +- `getNavigatorLocale` |
| 134 | + |
| 135 | +You can do `import { ... } from '@intlify/utils'` the above utilities |
| 136 | + |
| 137 | +> ⚠NOTE: for Node.js You need to do `import { ... } from '@intlify/utils/node'` |
| 138 | +
|
| 139 | +### HTTP |
| 140 | + |
| 141 | +- `getHeaderLanguages` |
| 142 | +- `getHeaderLanguage` |
| 143 | +- `getHeaderLocales` |
| 144 | +- `getHeaderLocale` |
| 145 | +- `getCookieLocale` |
| 146 | +- `setCookieLocale` |
| 147 | +- `getPathLocale` |
| 148 | +- `getQueryLocale` |
| 149 | + |
| 150 | +The about utilies functions accpet Web APIs such as [Request](https://developer.mozilla.org/en-US/docs/Web/API/Request) and [Response](https://developer.mozilla.org/en-US/docs/Web/API/Response) that is supported by JS environments (such as Deno, Bun, and Browser) |
| 151 | + |
| 152 | +#### Specialized environments |
| 153 | + |
| 154 | +If you will use Node.js and H3, You can do `import { ... } from '@intlify/utils/{ENV}'` the above utilities. |
| 155 | + |
| 156 | +The namespace `{ENV}` is one of the following: |
| 157 | + |
| 158 | +- `node`: accpet `IncomingMessage` and `Outgoing` by Node.js [http](https://nodejs.org/api/http.html) module |
| 159 | +- `h3`: accept `H3Event` by HTTP framework [h3](https://github.com/unjs/h3) |
| 160 | +- `hono`: accept `Context` by edge-side web framework [hono](https://github.com/honojs/hono) |
| 161 | + |
| 162 | +## 🙌 Contributing guidelines |
| 163 | + |
| 164 | +If you are interested in contributing to `@intlify/utils`, I highly recommend checking out [the contributing guidelines](/CONTRIBUTING.md) here. You'll find all the relevant information such as [how to make a PR](/CONTRIBUTING.md#pull-request-guidelines), [how to setup development](/CONTRIBUTING.md#development-setup)) etc., there. |
| 165 | + |
| 166 | +## ©️ License |
| 167 | + |
| 168 | +[MIT](http://opensource.org/licenses/MIT) |
| 169 | + |
| 170 | +<!-- Badges --> |
| 171 | + |
| 172 | +[npm-version-src]: https://img.shields.io/npm/v/@intlify/utils?style=flat&colorA=18181B&colorB=FFAD33 |
| 173 | +[npm-version-href]: https://npmjs.com/package/@intlify/utils |
| 174 | +[npm-downloads-src]: https://img.shields.io/npm/dm/@intlify/utils?style=flat&colorA=18181B&colorB=FFAD33 |
| 175 | +[npm-downloads-href]: https://npmjs.com/package/@intlify/utils |
| 176 | +[ci-src]: https://github.com/intlify/utils/actions/workflows/ci.yml/badge.svg |
| 177 | +[ci-href]: https://github.com/intlify/utils/actions/workflows/ci.yml |
0 commit comments