npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

azure-translator-code

v1.1.18

Published

Azure Cognitive Services Translator Text API Code for Use with Common Languages

Downloads

225

Readme

Azure Translator Code

Azure Translator Code is a powerful library for translating JSON files into multiple languages using the Azure Cognitive Translator service. This library supports translating JSON files located in multiple folders or within a single folder, depending on your needs.


Important Links


Installation

Install the library using npm or yarn:

As Development Dependency

npm install -D azure-translator-code

or

yarn add -D azure-translator-code

As Production Dependency

npm install azure-translator-code

or

yarn add azure-translator-code

Usage

You can import the JSON file you want to translate in two ways:

Importing a JSON File

const jsonFile = require("./jsonFileToTranslate/en.json");

// or

const jsonFile = {
	HomePage: {
		welcome: "Welcome",
		hello: "Hello",
		SubText: {
			subText: "This is a subtext",
		},
	},
};

Translating JSON to Multiple Languages

After importing the JSON file, you can use the library to translate it:

Importing the Functions

const {
	translate,
	translateText,
	translateToMultipleFolders,
	translateToUnicFolder,
	updateTranslationsMulti,
	updateTranslationsUnic,
} = require("azure-translator-code");
// or
import {
	translate,
	translateText,
	translateToMultipleFolders,
	translateToUnicFolder,
	updateTranslationsMulti,
	updateTranslationsUnic,
} from "azure-translator-code";

import type { TranslationType } from "azure-translator-code";

Define your Azure API details and languages:

const key = "YOUR_AZURE_KEY"; // Replace with your Azure API key
const endpoint = "https://api.cognitive.microsofttranslator.com/";
const location = "eastus";
const fromLang = "en";
const toLangs = [
	"pt",
	"de",
	"es",
	"fr",
	"it",
	"ja",
	"ko",
	"nl",
	"ru",
	"zh",
	"pt-pt",
	"ar",
	"tlh-Latn",
];

const jsonFile = {
	translation: {
		welcome: "Welcome",
		hello: "Hello",
	},
};

// Translate to multiple folders
translateToMultipleFolders(key, endpoint, location, fromLang, toLangs, jsonFile); 
// This will create a folder called multiFolderGeneratedTranslations

// Translate to a single folder
translateToUnicFolder(key, endpoint, location, fromLang, toLangs, jsonFile); 
// This will create a folder called unicFolderGeneratedTranslations

// Update translations in multiple folders
updateTranslationsMulti(key, endpoint, location, fromLang, toLangs, jsonFile); 
// Only new keys will be translated in the multiFolderGeneratedTranslations folder

// Update translations in a single folder
updateTranslationsUnic(key, endpoint, location, fromLang, toLangs, jsonFile); 
// Only new keys will be translated in the unicFolderGeneratedTranslations folder

Customizing Folder Structure

You can specify the folder name or location where translations will be saved. Saving always starts from the project root folder.

const key = "YOUR_AZURE_KEY";
const endpoint = "https://api.cognitive.microsofttranslator.com/";
const location = "eastus";
const fromLang = "en";
const toLangs = ["pt", "de"];

const jsonFile = {
	translation: {
		welcome: "Welcome",
		hello: "Hello",
	},
};

// Save translations in custom folder
translateToMultipleFolders(
	key,
	endpoint,
	location,
	fromLang,
	toLangs,
	jsonFile,
	"myFolder",
);
// This will create a folder called myFolder

translateToUnicFolder(
	key,
	endpoint,
	location,
	fromLang,
	toLangs,
	jsonFile,
	"./myFolder/OtherFolder/etc",
);
// This will create a folder at ./myFolder/OtherFolder/etc

// If you just want to update the file for new keys, use the update functions to avoid unnecessary requests.

Translating and Logging Results

You can also log the translation results to the console for verification.

const { translate, translateText } = require("azure-translator-code");

const key = "YOUR_AZURE_KEY"; 
const endpoint = "https://api.cognitive.microsofttranslator.com/";
const location = "eastus";
const fromLang = "en";
const toLangs = ["pt"];
const jsonFile = {
	HomePage: {
		Welcome: "Welcome",
		Hello: "Hello",
	},
};

translate(key, endpoint, location, fromLang, toLangs, jsonFile).then((res) => {
	console.log(res);
});

// Output
/**
{
	HomePage: {
		Welcome: "Bem-vindo",
		Hello: "Olá",
	},
};
 */

// or

translateText("Hello World!", fromLang, toLangs, endpoint, key, location).then(
	(res) => {
		console.log(res[0].translations);
	},
);

// Output -> [{ text: "Olá, mundo!", to: "pt" }]

Make sure to replace the key, location and endpoint with your actual Azure access credentials.


Contributing

If you would like to contribute to this project, feel free to open an issue or submit a pull request on our GitHub repository.