Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@markuplint/i18n

Package Overview
Dependencies
Maintainers
1
Versions
116
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@markuplint/i18n

HTML parser for markuplint

  • 2.0.0-dev.20211213.1
  • npm
  • Socket score

Version published
Weekly downloads
40K
increased by7.58%
Maintainers
1
Weekly downloads
 
Created
Source

@markuplint/i18n

npm version Build Status Coverage Status

Install

Prerequisites: Node.js (Version 12.4.0 or later)

$ npm install @markuplint/i18n

$ yarn add @markuplint/i18n

API

import { translator } from '@markuplint/i18n';

const t = translator({
	locale: 'ja',
	...require('@markuplint/i18n/locales/ja.json'),
});

The translator function creates the t function. It is an overloading function that accepts kind of arguments below:

Translate sentence

type T = (template?: string, ...values: string[]) => string;
const message = t(
	// Template #1
	'{0} is {1:c}',
	// The {0} value of template #1
	t(
		// Template #2
		'{0} of {1}',
		// The {0} value of template #2
		t(
			// Template #3
			'the {0}',
			// The {0} value of template #3
			'value',
		),
		// The {1} value of template #2
		t(
			// Template #4
			'the "{0*}" {1}',
			// The {0} value of template #4
			'id',
			// The {1} value of template #4
			'attribute',
		),
	),
	// The {1} value of template #1
	'duplicated',
);

console.log(message);
// => 属性「id」の値が重複しています
Placeholder

There is a placeholder that the number is surrounded by {} on template strings. It is replaced argument as a phrase. It translates the phrase if it matches the keyword defined in the dictionary.

Tagged templates syntax

⚠️ It is experimental.

import { taggedTemplateTranslator } from '@markuplint/i18n';
const _ = taggedTemplateTranslator(ja);

const message = _`${
	//
	_`${
		//
		_`the ${'value'}`
	} of ${
		//
		_`the "${'id'}" ${'attribute'}`
	}`
} is ${
	//
	'c:duplicated'
}`;

console.log(message);
// => 属性「id」の値が重複しています

Translate a phrase

type T = (phrase: string) => string;
const phrase = t('element');

console.log(phrase);
// => 要素

Translate listed phrases

type T = (phrases: string[]) => string;
const list = t(['element', 'attribute', 'value']);

console.log(list);
// => 「要素」「属性」「値」

/* If locale is "en" */
console.log(list);
// => "element", "attribute", "value"

It converts the character-separated list specified in each locale.

LocaleSeparaterBefore CharAfter Char
en, (camma + space)" (double quote)" (double quote)
janone (empty string) (left corner bracket) (right corner bracket)

Avoid translation

The autocomplete is defined as オートコンプリート in the JA dictionary. However, It avoids translation if the number placeholder includes * (asterisk). It is an effective means if you want a code or a specific name.

const phrase = t('the "{0}" {1}', 'autocomplete', 'attribute');
console.log(phrase);
// => 属性「オートコンプリート」

const phrase = t('the "{0*}" {1}', 'autocomplete', 'attribute');
console.log(phrase);
// => 属性「autocomplete」

Another means is that it surrounds % (percentage) to a phrase. It is effective when you use listing.

const phrase = t('the "{0}" {1}', '%autocomplete%', 'attribute');
console.log(phrase);
// => 属性「autocomplete」

const list = t(['element', '%attribute%', 'value']);
console.log(list);
// => 「要素」「attribute」「値」

Contributing

$ git clone git@github.com:markuplint/markuplint.git -b main
$ yarn
$ yarn build
$ yarn test

Copyright © 2021 markuplint. Under the MIT License.

FAQs

Package last updated on 13 Dec 2021

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc