Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ryanve/subtag
Language tag parser for JavaScript and Node.js
https://github.com/ryanve/subtag
bcp-47 i18n ietf internationalization javascript language languages localisation localization parser subtags
Last synced: 3 months ago
JSON representation
Language tag parser for JavaScript and Node.js
- Host: GitHub
- URL: https://github.com/ryanve/subtag
- Owner: ryanve
- License: isc
- Created: 2017-04-29T18:31:44.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2023-01-11T14:48:16.000Z (about 2 years ago)
- Last Synced: 2024-05-02T00:08:48.291Z (9 months ago)
- Topics: bcp-47, i18n, ietf, internationalization, javascript, language, languages, localisation, localization, parser, subtags
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/subtag
- Size: 38.1 KB
- Stars: 7
- Watchers: 2
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
Awesome Lists containing this project
README
# subtag
[Language tag](https://www.w3.org/International/articles/language-tags/) parser. Parse language tags into subtags.## api
- `subtag(tag)` parse tag into [subtags object](#objects)
- `subtag.split(tag)` split tag into [subtags array](#arrays)
- `subtag.language(tag)` get [primary language subtag](https://www.w3.org/International/articles/language-tags/#language)
- `subtag.extlang(tag)` get [extended language subtag](https://www.w3.org/International/articles/language-tags/#extlang)
- `subtag.script(tag)` get [script subtag](https://www.w3.org/International/articles/language-tags/#script)
- `subtag.region(tag)` get [region subtag](https://www.w3.org/International/articles/language-tags/#region)### notes
- parsing is done via regex
- unpresent subtags will be an empty string
- separator can be dashes (standard) or underscores## setup
### install via npm or yarn
```
npm install subtag --save
``````
yarn add subtag
```## usage
### `require` usage
```js
var subtag = require('subtag')
```### `import` usage
```js
import subtag from 'subtag'
```### examples
#### objects
```js
subtag('ja-JP') // {language: 'ja', extlang: '', script: '', region: 'JP'}
subtag('es-AR') // {language: 'es', extlang: '', script: '', region: 'AR'}
```#### arrays
```js
subtag.split('yue') // ["yue"]
subtag.split('es-419') // ["es", "419"]
subtag.split('zh-Hant-HK') // ["zh", "Hant", "HK"]
subtag.split('en-90210') // ["en"] because 90210 is fake
```#### subtags
```js
subtag.language('en') // 'en'
subtag.extlang('en') // ''
subtag.script('en') // ''
subtag.region('en') // ''subtag.language('en-US') // 'en'
subtag.extlang('en-US') // ''
subtag.script('en-US') // ''
subtag.region('en-US') // 'US'subtag.language('zh-yue') // 'zh'
subtag.extlang('zh-yue') // 'yue'
subtag.script('zh-yue') // ''
subtag.region('zh-yue') // ''subtag.language('zh-Hans') // 'zh'
subtag.extlang('zh-Hans') // ''
subtag.script('zh-Hans') // 'Hans'
subtag.region('zh-Hans') // ''
```## structure
[language-extlang-script-region-variant-extension-privateuse](https://www.w3.org/International/articles/language-tags/#rfc)type
pattern
conventionlanguage
2-letter or 3-letter
lowercaseextlang
3-letter
lowercasescript
4-letter
titlecaseregion
2-letter or 3-number
uppercase### `.pattern`
Regex patterns are exposed for validation```js
subtag.language.pattern.test('en') // true
subtag.language.pattern.test('ast') // true
subtag.language.pattern.test('fake') // false
subtag.extlang.pattern.test('yue') // true
subtag.script.pattern.test('Hans') // true
subtag.region.pattern.test('US') // true
subtag.region.pattern.test('005') // true
subtag.region.pattern.test('90210') // false
```## compatibility
Works in Node.js and ES5+ browsers