
Security News
npm ‘is’ Package Hijacked in Expanding Supply Chain Attack
The ongoing npm phishing campaign escalates as attackers hijack the popular 'is' package, embedding malware in multiple versions.
[](https://travis-ci.org/AnandChowdhary/auto-i18n) [](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
[](https://travis-ci.org/AnandChowdhary/auto-i18n) [](https://coveralls.io/github
The npm package auto-i18n receives a total of 75 weekly downloads. As such, auto-i18n popularity was classified as not popular.
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
The ongoing npm phishing campaign escalates as attackers hijack the popular 'is' package, embedding malware in multiple versions.
Security News
A critical flaw in the popular npm form-data package could allow HTTP parameter pollution, affecting millions of projects until patched versions are adopted.
Security News
Bun 1.2.19 introduces isolated installs for smoother monorepo workflows, along with performance boosts, new tooling, and key compatibility fixes.