@manuth/culture-info
TypeScript icon, indicating that this package has built-in type declarations

3.0.1 • Public • Published

CultureInfo.js

A node-package for representing locales

status-badge

General

This package allows you to easily represent locales and determining the next lesser-specific parent of a locale (en-US => en).

This key feature might be usable for creating a resource-manager or showing web-content based on a language-tag consisting of the language, the script and the region.

Creating a CultureInfo instance

You can create a CultureInfo instance by passing a language-tag to its constructor:

import { CultureInfo } from "@manuth/culture-info";

let culture = new CultureInfo("en-US");

Following kinds of language-tags are supported:

  • Language
    Example: en
  • Language and region
    Example: en-US
  • Language, script and region
    Example: zh-Hans-CN

Features

Automated Casing

The casing is automatically adjusted allowing you to pass the language-tag even with incorrect letter-casing:

console.log(new CultureInfo("EN-US").Name); // Logs `"en-US"`
console.log(new CultureInfo("zH-hANs-cN").Name); // Logs `"zh-Hans-CN"`

Checking whether a culture only consists of a language:

console.log(new CultureInfo("en").IsNeutralCulture); // Logs `true`
console.log(new CultureInfo("en-US").IsNeutralCulture); // Logs `false`

Walking up the Locale-Graph

You might want to find out the next lesser specific locale of a specific language-tag. This can be achieved using the Parent-property:

let culture = new CultureInfo("en-US");
console.log(culture.Name); // Logs `"en-US"`
culture = culture.Parent;
console.log(culture.Name); // Logs `"en"`
culture = culture.Parent;
console.log(culture.Name); // Logs `""` - That's the invariant culture

String Conversion

When converting a CultureInfo to a string it's automatically converted to the language-tag:

console.log(`${new CultureInfo("en-US")}`); // Logs `"en-US"`

Package Sidebar

Install

npm i @manuth/culture-info

Weekly Downloads

42

Version

3.0.1

License

MIT

Unpacked Size

15.5 kB

Total Files

8

Last publish

Collaborators

  • lordgizmo