bcp47-helper
Version:
A lightweight TypeScript library for parsing, validating, and managing BCP 47 language tags in Node.js and browser environments.
228 lines (162 loc) • 5.63 kB
Markdown
A lightweight TypeScript library for working with [BCP 47 language tags](https://www.rfc-editor.org/info/bcp47) in Node.js and browser environments. This library provides utilities to map, validate, search, and group language codes, making it easier to handle internationalization (i18n) tasks in your applications.
**Note**: The results of all functions depend on the data file located at `src/data/bcp47
-languages.json`. This file currently includes the most common languages and will be enhanced over time with additional languages and details.
[](https://www.npmjs.com/package/bcp47-helper)
[](https://github.com/aumit/BCP-47-Helper/blob/main/LICENSE)
[](https://github.com/aumit/BCP-47-Helper/actions)
- **Map Language Codes**: Convert BCP 47 codes to their full names.
- **Retrieve Language Info**: Get detailed information (code, full name, language, region, description) for a specific language code.
- **Find Language Variants**: List all variants for a base language code.
- **List All Languages**: Retrieve a complete list of supported language codes with their full names.
- **Search by Name**: Search languages by full name, language, or region (case-insensitive).
- **Group by Language**: Organize language codes by their base language.
- **Validate Codes**: Check if a language code exists in the dataset.
- **TypeScript Support**: Fully typed with TypeScript for better developer experience.
- **Lightweight**: Optimized for both Node.js and browser environments.
## Installation
Install the package via npm:
```bash
npm install bcp47-helper
```
Or via Yarn:
```bash
yarn add bcp47-helper
```
## Usage
### Importing the Library
```javascript
import {
mapLanguages,
getLanguageInfo,
getAvailableVariants,
listAllLanguages,
searchByName,
groupByLanguage,
validateCode
} from 'bcp47-helper';
```
```javascript
const codes = ['en-US', 'es-ES', 'fr-CA'];
const mapped = mapLanguages(codes);
console.log(mapped);
// Output: {
// 'en-US': 'English (United States)',
// 'es-ES': 'Spanish (Spain)',
// 'fr-CA': 'French (Canada)'
// }
```
```javascript
const info = getLanguageInfo('en-GB');
console.log(info);
// Output: {
// code: 'en-GB',
// fullName: 'English (United Kingdom)',
// language: 'English',
// region: 'United Kingdom',
// description: 'English as spoken in the United Kingdom'
// }
```
```javascript
const variants = getAvailableVariants('en');
console.log(variants);
// Output: {
// 'en-US': 'English (United States)',
// 'en-GB': 'English (United Kingdom)',
// 'en-AU': 'English (Australia)',
// ...
// }
```
```javascript
const allLanguages = listAllLanguages();
console.log(allLanguages);
// Output: {
// 'en-US': 'English (United States)',
// 'es-ES': 'Spanish (Spain)',
// ...
// }
```
```javascript
const results = searchByName('spanish');
console.log(results);
// Output: {
// 'es-ES': 'Spanish (Spain)',
// 'es-MX': 'Spanish (Mexico)',
// ...
// }
```
```javascript
const grouped = groupByLanguage();
console.log(grouped);
// Output: {
// 'en': ['en-US', 'en-GB', 'en-AU', ...],
// 'es': ['es-ES', 'es-MX', ...],
// ...
// }
```
```javascript
console.log(validateCode('en-US')); // true
console.log(validateCode('xx-XX')); // false
```
Maps an array of BCP 47 language codes to their full names.
Returns detailed information for a given language code or `null` if not found.
Returns all variants for one or more base language codes.
Returns all supported language codes with their full names.
Searches languages by full name, language, or region (case-insensitive).
### `groupByLanguage(): Record<string, string[]>`
Groups language codes by their base language.
### `validateCode(code: string): boolean`
Validates if a language code exists in the dataset.
## Type Definitions
```typescript
export type LangInfo = {
code: string;
fullName: string;
language: string;
region: string;
description: string;
};
```
To contribute or modify the library:
1. Clone the repository:
```bash
git clone https://github.com/aumit/BCP-47-Helper.git
```
2. Install dependencies:
```bash
npm install
```
3. Build the project:
```bash
npm run build
```
4. Run in development mode with watch:
```bash
npm run dev
```
This project is licensed under the [MIT License](LICENSE).
Contributions are welcome! Please open an issue or submit a pull request on [GitHub](https://github.com/aumit/BCP-47-Helper).
If you encounter any issues or have feature requests, please file them on the [GitHub Issues page](https://github.com/aumit/BCP-47-Helper/issues).
- Aumit ([GitHub](https://github.com/aumit))
BCP 47, language tags, internationalization, i18n, TypeScript, JavaScript, language codes, locale, Node.js, browser