Miss any of our Open RFC calls?Watch the recordings here! »

@tradeshift/g11n-langneg

1.2.3 • Public • Published

g11n-langneg

Language negotiation helpers

Language negotiation usage

import { Locale, match } from '@tradeshift/g11n-langneg'
 
const result: Locale = match('pt-Latn-BR', ['da', 'en', 'es', 'pt'])
// result == Locale.parse('pt')`
 
// also accepts `Locale` objects as inputs
const result2: Locale = match(Locale.parse('pt-Latn-BR'), [Locale.parse('da'), Locale.parse('pt')])
// result2 == Locale.parse('pt')`

If there's no match, it will return the first candidate in the list, so the first element should always be your preferred/default locale.

const result: Locale = match('pt', ['en', 'da', 'es'])
// result == Locale.parse('en')`

Locale

Parsing (strict and lenient)

import { Locale } from '@tradeshift/g11n-langneg'
 
// lenient parsing of locales, invalid locales will parse to `und`
const locale: Locale = Locale.parse('pt-BR')
 
// strict parsing of locales will throw errors for invalid locales
try {
    const invalid = Locale.parseStrict('abcdefghijklmnopq')
} catch (e) {
    throw e;
}

Maximization (Likely Subtags)

Note: g11n-langneg uses a small subset of the likely subtags CLDR data

const locale: Locale = Locale.parse('pt-BR')
const maximized: Locale = locale.maximize() // equivalent to 'pt-Latn-BR'

Accessing and modifying subtags

const locale: Locale = Locale.parse('pt-Latn-BR')
 
const language: string = locale.getLanguage() // pt
const script: string = locale.getScript() // Latn
const region: string = locale.getRegion() // BR
 
const ptCyrl: Locale = locale.setScript('Cyrl') // pt-Cyrl-BR
const ptPT: Locale = locale.setRegion('PT') // pt-Latn-PT

Install

npm i @tradeshift/g11n-langneg

DownloadsWeekly Downloads

0

Version

1.2.3

License

MIT

Unpacked Size

97.7 kB

Total Files

40

Last publish

Collaborators

  • avatar
  • avatar
  • avatar
  • avatar
  • avatar
  • avatar
  • avatar