This project is moving to Codeberg.org.
If you wish to check out the most recent source code, file an issue or contribute to this project, please head to the new repository on Codeberg.org:
https://codeberg.org/lordgizmo/CultureInfo.js
A node-package for representing locales
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.
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
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`
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
When converting a CultureInfo
to a string it's automatically converted to the language-tag:
console.log(`${new CultureInfo("en-US")}`); // Logs `"en-US"`