Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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

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
convention

language
2-letter or 3-letter
lowercase

extlang
3-letter
lowercase

script
4-letter
titlecase

region
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