How To Use 
Register an account

Try For Free Now

Dark

Light

USER DICTIONARY
New user dictionary entries
add user dictionary entries collectively.
User dictionary is only applied to T-4OO engine, Real-time. T-3MT engine.
Endpoint
Request details
POST
https://translate.rozetta-api.io/api/v1/dictionary/bulk
Header
Header
Description
Content-Type
application/json
accessKey, nonce, signature
Please refer to the authentication section.
Body
Key
Required
Description
fromLang
O
The language of text to map from. (The Key of object in array) Except for languages such as zh-CN and zh-TW, please use the ISO 639-1 code. To get the list of supported languages, please refer to Languages section.
fromText
O
The text to map from. (The Key of object in array) Each one is up to 255 charaters. About counting, please refer to "Charater Count method" in Count Method.
toLang
O
The language of the translated text. (The Key of object in array) Except for languages such as zh-CN and zh-TW, please use the ISO 639-1 code. To get the list of supported languages, please refer to Languages section.
toText
O
The text to map to. (The Key of object in array) Each one is up to 255 charaters. About counting, please refer to "Charater Count method" in Count Method.
Request Sample
[{
  "fromLang": "en",
  "fromText": "square",
  "toLang": "ja",
  "toText": "広場"
},
{
  "fromLang": "en",
  "fromText": "rabbit",
  "toLang": "ja",
  "toText": "ラビット"
}]
Response
Key
Description
status
For successfully adding of an entry, this field will be "success". For unsuccessful requests, this field will be "failure".
Response Sample
{
  "status": "success"
}
Limitation
Limitation
Only 3 or less words of User Dictionary can be applied in one sentence when using real-time translation engine. For example: "猫"→"Kitty" "犬"→"Marutaro" "ウサギ"→"Peter" "クマ"→"Teddy" When send request below ["これはです。それはです。ウサギクマも動物です。", "ウサギクマも可愛いです。"] The result will be like: ["This is Kitty. It is Marutaro. Both Peter and bears are animals.", "Both Peter and Teddy are cute."] ※For there are 3 user dictionaries:「猫」、「犬」、「ウサギ」are applied in first sentence, 「クマ」 will not be applied.

api/v1/dictionary/bulk
Sample: Add user dictionary entries with CSV files.
const superagent = require('superagent');
const crypto = require('crypto');
const csvParse = require('csv-parse');
const fs = require('fs');
const path = require('path');

const serverConfig = {
    protocol: 'https:',
    hostname: 'translate.rozetta-api.info',
    port: 443
};

const authConfig = {
    accessKey: 'ACCESS_KEY',
    secretKey: 'SECRET_KEY',
    nonce: Date.now().toString()
};

const fsPromises = fs.promises;

const DATA_FOLDER = path.join(__dirname);

const normalizeText = (text) => {
  const normalized = text.trim();
  if (normalized.startsWith('"') && normalized.endsWith('"')) {
    return normalized.replace(/^"/, '')
      .replace(/"$/, '');
  }
  return normalized;
};

const extractLines = async (content) => new Promise((resolve, reject) => {
  csvParse(
    content,
    {
      delimiter: ',',
      relax: true,
      trim: true,
    },
    (error, lines) => {
      if (error !== undefined) {
        reject(error);
        return;
      }
      resolve(lines);
    },
  );
});

const linesToEntries = (fromLang, toLang, records) => {
  const entries = records.filter((record) => record.length >= 2)
    .map((record) => ({
      fromLang,
      fromText: normalizeText(record[0]),
      toLang,
      toText: record[1],
    }));
  return entries;
};

const sendRequest = (serverConfig, authConfig, payload) => {
  const url = '/api/v1/dictionary/bulk';
  const signature = authUtils.generateSignature(
    url,
    authConfig.secretKey,
    authConfig.nonce
  );

  const headers = {
    accessKey: authConfig.accessKey,
    signature,
    nonce: authConfig.nonce,
  };
  return superagent.post(`${serverConfig.protocol}//${serverConfig.hostname}${url}`)
    .set(headers)
    .send(payload)
    .then((res) => {
      return res.body;
    }).catch((err) => {
      return err.message;
    });
};

const main = async () => {
  try {
    const csvFiles = await fsPromises.readdir(DATA_FOLDER);
    const allEntries = [];
    for (const file of csvFiles) {
      if (!file.endsWith('.csv')) {
        continue;
      }
      const filePath = path.join(DATA_FOLDER, file);

      /*
        csv file like:
        square,広場
        rabbit,ラビット
      */
      const content = await fsPromises.readFile(
        filePath,
        {
          encoding: 'utf8',
        },
      );
      const lines = await extractLines(content);

      // set all lines lang from en to ja 
      const entries = linesToEntries(
        'en',
        'ja',
        lines,
      );
    
      if (entries.length === 0) {
        continue;
      }

      allEntries.push(...entries);
    }

    const response = await sendRequest(
      serverConfig,
      authConfig,
      allEntries
    );
    console.log('Server response:');
    console.log(response);

  } catch (error) {
    console.error(error);
  }
};

main();
About the authentication, please refer to the authentication section.
You can get a full version of sample codes here
©️ 2019 Rozetta API  ・  Powered by Rozetta

Rozetta Corp.

^