Giter Club home page Giter Club logo

g11n-langneg's Introduction

g11n-langneg

Language negotiation helpers

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

Release

Semantic release: (Commit conventions)

No new version will be released unless specific commit message is used. See Commit conventions for details. If a release is expected, please fix commit messages to align with appropriate format

g11n-langneg's People

Contributors

adinaiordache2017 avatar celiogafesi avatar dependabot[bot] avatar droinishvili avatar ehats avatar geobos23 avatar hauke-d avatar lacibeloberk avatar mtomescu avatar throup avatar tradeshift-renovate[bot] avatar tradeshift-renovatebot avatar tssecurity avatar tsuesun avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

g11n-langneg's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.