Skip to content

ryanve/subtag

Repository files navigation

subtag

Language tag parser. Parse language tags into subtags.

api

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

var subtag = require('subtag')

import usage

import subtag from 'subtag'

examples

objects

subtag('ja-JP') // {language: 'ja', extlang: '', script: '', region: 'JP'}
subtag('es-AR') // {language: 'es', extlang: '', script: '', region: 'AR'}

arrays

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

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

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

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