Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/gabriel-logan/azure-translator-code
Azure Cognitive Services Translator Text API Code for Use with Common Languages
https://github.com/gabriel-logan/azure-translator-code
azure-translate azure-translation-services azure-translator azure-translator-code collaborate communityexchange educative github github-campus-experts github-codespaces github-copilot javascript javascript-library microsoft npm-package student-vscode typescript
Last synced: 17 days ago
JSON representation
Azure Cognitive Services Translator Text API Code for Use with Common Languages
- Host: GitHub
- URL: https://github.com/gabriel-logan/azure-translator-code
- Owner: gabriel-logan
- License: mit
- Created: 2023-09-16T14:44:23.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2024-10-04T00:57:56.000Z (about 1 month ago)
- Last Synced: 2024-10-04T01:00:17.279Z (about 1 month ago)
- Topics: azure-translate, azure-translation-services, azure-translator, azure-translator-code, collaborate, communityexchange, educative, github, github-campus-experts, github-codespaces, github-copilot, javascript, javascript-library, microsoft, npm-package, student-vscode, typescript
- Language: TypeScript
- Homepage: https://azuretranslatorcode.vercel.app
- Size: 3.81 MB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
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
- **[NPM Page](https://www.npmjs.com/package/azure-translator-code)**
- **[GitHub Repository](https://github.com/gabriel-logan/Azure-translator-code)**
- **[Try the Library Here](https://azuretranslatorcode.vercel.app)**---
## Installation
Install the library using npm or yarn:
### As Development Dependency
```bash
npm install -D azure-translator-code
```or
```bash
yarn add -D azure-translator-code
```### As Production Dependency
```bash
npm install azure-translator-code
```or
```bash
yarn add azure-translator-code
```---
## Usage
You can import the JSON file you want to translate in two ways:
### Importing a JSON File
```javascript
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
```javascript
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:
```javascript
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.
```javascript
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 myFoldertranslateToUnicFolder(
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.
```javascript
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.data[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](https://github.com/gabriel-logan/Azure-translator-code).
---