Security News
Node.js EOL Versions CVE Dubbed the "Worst CVE of the Year" by Security Experts
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
[![Travis CI](https://img.shields.io/travis/AnandChowdhary/auto-i18n.svg)](https://travis-ci.org/AnandChowdhary/auto-i18n) [![Coverage Status](https://coveralls.io/repos/github/AnandChowdhary/auto-i18n/badge.svg?branch=master)](https://coveralls.io/github
Auto I18N is a package which automagically translate your JSON files, objects, or strings using Google Translate. It's automating your internationalization.
Add the dependency from NPM:
npm install auto-i18n
Import the modules you require from the package:
import * from "auto-i18n";
And then configure your Project ID and API key as environment variables (see Configuration).
To translate a single phrase:
import { translate } from "auto-i18n";
const hello = await translate("Hello!", "fr"); // Bonjour!
For every function, can also use promises:
translate("Hello!", "nl")
.then(translation => {
console.log(translation); // Hallo!
})
.catch(error => {
console.log("Got an error", error);
});
To translate an entire object:
import { translateObject } from "auto-i18n";
const translateMe = {
hello: "Hello",
world: ["world", "earth"]
};
const translated = await translateObject(translateMe, "es");
console.log(translated);
/* { hello: "Hola",
world: ["mundo", "tierra"] } */
A single translation file is a JSON file which contains keys for language codes and terms under each key. You can write one for English like this, for example:
File en.json
:
{
"en": {
"greeting": "Hello!",
"question": "How are you?"
}
}
To translate it, use the translateFileSingle
function:
import { translateFileSingle } from "auto-i18n";
import path from "path"; // Node.js path helper
const filePath = path.join(__dirname, "en.json");
const translated = await translateFileSingle(filePath, ["fr", "nl", "es"]);
console.log(translated);
This is what translated
looks like:
{
"en": {
"greeting": "Hello!",
"question": "How are you?"
},
"fr": {
"greeting": "Bonjour!",
"question": "Comment vas-tu?"
},
"nl": {
"greeting": "Hallo!",
"question": "Hoe gaat het met je?"
},
"es": {
"greeting": "Hola!",
"question": "¿Cómo estás?"
}
}
You can also write the file directly by supplying the third parameter as true
:
await translateFileSingle(filePath, ["fr", "nl", "es"], true);
If you have a JSON file which is not a single-translation-file, you can also translate it:
await translateFile(
"en.json", /* File path */
"nl", /* Language code (single) */
false /* Overwrite the file with translation? */
);
If you have a JSON file (e.g., en.json
), you can also generate corresponding language files:
await generate(
"en.json", /* File path */
["nl", "fr", "es"], /* Languages */
);
Auto I18N uses local caching powered by Fraud to decrease API usage and therefore billing. A caching directory, .cache/auto-i18n
is used, which should be added to your .gitignore
. You can overwrite this directory as a parameter in each function.
Auto I18N uses the Google Cloud Translation API, for which an API key and Project ID are required. These should be available as environment variables in your .env
file. For example:
PROJECT_ID = "google-cloud-project-id"
API_KEY = "google-cloud-api-key"
The library will automatically read them from the .env
file, as long as it's in your project root directory.
Install dependencies:
yarn
Compile Typescript to ES6 before publishing to NPM:
yarn build
MIT
FAQs
[![Travis CI](https://img.shields.io/travis/AnandChowdhary/auto-i18n.svg)](https://travis-ci.org/AnandChowdhary/auto-i18n) [![Coverage Status](https://coveralls.io/repos/github/AnandChowdhary/auto-i18n/badge.svg?branch=master)](https://coveralls.io/github
We found that auto-i18n demonstrated a not healthy version release cadence and project activity because the last version was released 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.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.