Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
language-tags
Advanced tools
The language-tags npm package is a utility for working with IETF language tags (BCP 47). It provides functionality to parse, validate, and manipulate language tags, making it useful for handling language data in applications that need to support internationalization.
Parsing and Validating Language Tags
This feature allows you to parse a language tag string and check if it is valid according to BCP 47 standards. The example demonstrates how to parse the tag 'en-GB' and check its validity.
const tags = require('language-tags');
const tag = tags('en-GB');
console.log(tag.valid()); // true
Subtag Lookup
This feature enables looking up details of specific subtags within a language tag. In the example, it retrieves descriptions for the language subtag of 'en-GB'.
const tags = require('language-tags');
const tag = tags('en-GB');
console.log(tag.language().descriptions()); // ['English']
Data Access
This feature provides access to additional data related to a language tag, such as its scope, type, and more. The example shows how to access the scope of the 'en' language tag.
const tags = require('language-tags');
const data = tags('en').data();
console.log(data.scope); // 'macrolanguage'
The bcp47 package is similar to language-tags as it also provides parsing and validation of BCP 47 language tags. However, it focuses more on the parsing aspect and less on providing metadata or manipulation capabilities.
This package offers regular expression patterns for validating BCP 47 language tags. Unlike language-tags, it does not provide parsing or detailed metadata access, focusing solely on validation through regex matching.
Based on BCP 47 (RFC 5646) and the latest IANA language subtag registry.
This project will be updated as the standards change.
See the language-subtag-registry project for the underlying JSON data.
var tags = require('language-tags')
Note that all lookups and checks for tags and subtags are case insensitive. For formatting according to common conventions, see tag.format
.
Check whether a hyphen-separated tag is valid and well-formed. Always returns a Tag
, which can be checked using the valid
method.
Shortcut for tags(tag).valid()
. Return true
if the tag is valid, false
otherwise. For meaningful error output see tag.errors()
.
Look up one or more subtags. Returns an array of Subtag
objects. Returns an empty array if all of the subtags are non-existent.
Calling tags.subtags('mt')
will return an array with two Subtag
objects: one for Malta (the 'region' type subtag) and one for Maltese (the 'language' type subtag).
> tags.subtags('mt');
[Subtag, Subtag]
> tags.subtags('bumblebee');
[]
To get or check a single subtag by type use tags.language(subtag)
, tags.region(subtag)
or tags.type(subtag, type)
.
The opposite of tags.subtags(subtags)
. Returns an array of codes that are not registered subtags, otherwise returns an empty array.
> tags.filter(['en', 'Aargh']);
['Aargh']
Search for tags and subtags by description. Supports either a RegExp object or a string for description
. Returns an array of Subtag
and Tag
objects or an empty array if no results were found.
Note that Tag
objects in the results represent 'grandfathered' or 'redundant' tags. These are excluded by default. Set the all
parameter to true
to include them.
Search is case-insensitive if description
is a string.
Returns an array of Subtag
objects representing all the 'language' type subtags belonging to the given 'macrolanguage' type subtag.
Throws an error if macrolanguage
is not a macrolanguage.
> tags.languages('zh');
[Subtag, Subtag...]
> tags.languages('en');
Error: 'en' is not a valid macrolanguage.
Convenience method to get a single 'language' type subtag. Can be used to validate an input value as a language subtag. Returns a Subtag
object or null
.
> tags.language('en');
Subtag
> tags.language('us');
null
As above, but with 'region' type subtags.
> tags.region('mt');
Subtag
> tags.region('en');
null
Get a subtag by type. Returns the subtag matching type
as a Subtag
object otherwise returns null
.
A type
consists of one of the following strings: 'language', 'extlang', 'script', 'region' or 'variant'. To get a 'grandfathered' or 'redundant' type tag use tags(tag)
.
> tags.type('zh', 'macrolanguage');
Subtag
> tags.type('zh', 'script');
null
Returns the file date for the underlying data, as a string.
> tags.date();
'2004-06-28'
Get the subtag type (either 'language', 'extlang', 'script', 'region' or 'variant'). See RFC 5646 section 2.2 for type definitions.
Returns an array of description strings (a subtag may have more than one description).
> tags.language('ro').descriptions();
['Romanian', 'Moldavian', 'Moldovan']
Returns a preferred subtag as a Subtag
object if the subtag is deprecated. For example, ro
is preferred over deprecated mo
.
> tags.language('mo').preferred();
Subtag
For subtags of type 'language' or 'extlang', returns a Subtag
object representing the language's default script. See RFC 5646 section 3.1.9 for a definition of 'Suppress-Script'.
Returns the subtag scope as a string, or null
if the subtag has no scope.
Tip: if the subtag represents a macrolanguage, you can use tags.languages(macrolanguage)
to get a list of all the macrolanguage's individual languages.
> tags.language('zh').scope();
'macrolanguage'
> tags.language('nah').scope();
'collection'
Returns a date string reflecting the deprecation date if the subtag is deprecated, otherwise returns null
.
> tags.language('ja').deprecated();
'2008-11-22'
Returns a date string reflecting the date the subtag was added to the registry.
> tags.language('ja').added();
'2005-10-16'
Returns an array of comments, if any, otherwise returns an empty array.
> tags.language('nmf').comments();
['see ntx']
Return the subtag code formatted according to the case conventions defined in RFC 5646 section 2.1.1.
If the tag is listed as 'deprecated' or 'redundant' it might have a preferred value. This method returns a Tag
object if so.
> tags('zh-cmn-Hant').preferred();
Tag
Returns grandfathered
if the tag is grandfathered, redundant
if the tag is redundant, and tag
if neither. For a definition of grandfathered and redundant tags, see RFC 5646 section 2.2.8.
Returns an array of subtags making up the tag, as Subtag
objects.
Shortcuts for tag.find('language')
, tag.find('region')
and tag.find('script')
.
Find a subtag of the given type from those making up the tag.
Returns true
if the tag is valid, false
otherwise.
Returns an array of Error
objects if the tag is invalid. The message
property of each is readable and helpful enough for UI output. The code
property can be checked against the Tag.ERR_*
constants. Each error will also have either a subtag
or tag
property with the code of the offending tag.
Format a tag according to the case conventions defined in RFC 5646 section 2.1.1.
> tags('en-gb').format();
'en-GB'
For grandfathered or redundant tags, returns a date string reflecting the deprecation date if the tag is deprecated.
> tags('zh-cmn-Hant').deprecated();
'2009-07-29'
For grandfathered or redundant tags, returns a date string reflecting the date the tag was added to the registry.
Returns an array of tag descriptions for grandfathered or redundant tags, otherwise returns an empty array.
Copyright (c) 2013, Matthew Caruana Galizia.
The software part of this project is licensed under an MIT licence.
Comments, feedback and suggestions are welcome. Please feel free to raise an issue or pull request. Enjoy.
FAQs
Work with IANA language tags.
The npm package language-tags receives a total of 8,111,108 weekly downloads. As such, language-tags popularity was classified as popular.
We found that language-tags demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.