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

attr-i18n

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

attr-i18n

Internationalization for Vanilla JS projects

  • 1.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
106
increased by32.5%
Maintainers
1
Weekly downloads
 
Created
Source

attr-i18n (Internationalization through HTML attributes)

Github Build Codacy Badge Codebeat Badge CodeFactor Badge DeepScan grade Analytics

Translate your vanilla websites or web apps using HTML attributes and a pinch of Javascript

Visitor stats

GitHub stars GitHub forks GitHub watchers GitHub followers

Code stats

GitHub code size in bytes GitHub repo size GitHub language count GitHub top language GitHub last commit

Install

npm i attr-i18n

# or

yarn add attr-i18n

Usage

First import and initialize the module.

import { AttrI18n } from 'attr-i18n';

const languages = ['English', 'हिन्दी', 'français'];

new AttrI18n(languages, {
	attr: 'data-translation',
	path: 'assets/translations',
	debug: true,
	togglerID: 'language-toggle',
	defaultLang: languages[0]
});

Then in your HTML file(s):

  1. Add an HTML select element with the ID corresponding to the togglerID option above. This select element will toggle between languages
  2. Add the translation files in the folder you specified in the path option above. Each of the language files should be named after the language name as it appears in the languages array above.
  3. Use the data-translation attribute as set in the option attr above by adding it to each element you want translated. The data-translation attribute accepts a dot-delimited string which points to the translation you want to use.

Options

NameTypeRequiredDefaultDescription
attrstringfalsedata-attr-i18nHTML attribute which contains the translation key
pathstringfalseassets/attr-i18nPath to the folder which contains the translation JSON file(s)
debugbooleanfalsefalseTurns debug mode on/off
togglerIDstringfalseattr-i18n-togglerID attribute for the language toggle HTML select element
defaultLangstringfalselanguages[0]The default language. If not provided, the first one from the array is used

Example HTML

Language selector HTML select element

<select id="language-toggle">
	<option>English</option>
	<option>हिन्दी</option>
	<option>français</option>
</select>

Page content

<h1 data-translation="form.desc">Fill Your Details!</h1>

<div class="form-row">
	<label data-translation="form.name">Name:</label>

	<input type="text" name="UserName" size="35" maxlength="35" value="" data-translation="form.name" />
</div>

<div class="form-row">
	<label data-translation="form.age">Age:</label>

	<input type="number" name="Age" size="35" maxlength="35" value="" data-translation="form.age" />
</div>

<div class="form-row">
	<p data-translation="form.exe.desc">Do you exercise at home?</p>

	<ul>
		<li>
			<input type="radio" name="exe" value="1" />

			<label data-translation="form.exe.yes">Yes</label>
		</li>

		<li>
			<input type="radio" name="exe" value="2" />

			<label data-translation="form.exe.no">No</label>
		</li>
	</ul>
</div>

LICENSE

MIT


Connect with me:


Support and sponsor my work:

Keywords

FAQs

Package last updated on 25 Jan 2023

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