• Stars
    star
    121
  • Rank 293,924 (Top 6 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 5 years ago
  • Updated 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

JavaScript client library for Crowdin API

Crowdin JavaScript client Tweet GitHub Repo stars

The Crowdin JavaScript client is a lightweight interface to the Crowdin API that works in any JavaScript environment, including web browsers, workers in web browsers, extensions in web browsers or desktop applications, Node.js etc. It provides common services for making API requests.

Our API is a full-featured RESTful API that helps you to integrate localization into your development process. The endpoints that we use allow you to easily make calls to retrieve information and to execute actions needed.

Live Demo  |  Docs  |  Examples  |  Crowdin API  |  Crowdin Enterprise API

npm npm Tests codecov GitHub Release Date GitHub contributors License

Table of Contents

Installation

npm

npm i @crowdin/crowdin-api-client

yarn

yarn add @crowdin/crowdin-api-client

Quick Start

Typescript
import crowdin, { Credentials, SourceFilesModel } from '@crowdin/crowdin-api-client';

// credentials
const credentials: Credentials = {
  token: 'personalAccessToken',
  organization: 'organizationName' // optional
};

// initialization of crowdin client
const {
  projectsGroupsApi,
  uploadStorageApi,
  sourceFilesApi,
  translationsApi
} = new crowdin(credentials);

// get project list
projectsGroupsApi.listProjects()
  .then(projects => console.log(projects))
  .catch(error => console.error(error));

// You can also use async/wait. Add `async` keyword to your outer function/method
async function getProjects() {
  try {
    const projects = await projectsGroupsApi.listProjects();
    console.log(projects);
  } catch (error) {
    console.error(error);
  }
}

// Create file with json content to translate
async function createFile() {
  const projectId = 123;
  const fileData = {
    title: 'Example',
    description: 'Some Text'
  };
  const storage = await uploadStorageApi.addStorage('file1.json', fileData);
  const file = await sourceFilesApi.createFile(projectId, {
    name: 'file1.json',
    title: 'Sample file',
    storageId: storage.data.id,
    type: 'json'
  });
  console.log(file);
}

// Download translations
async function downloadTranslations() {
  const projectId = 123;
  const fileId = 456;
  const language = 'de';
  const downloadLink = await translationsApi.buildProjectFileTranslation(
    projectId,
    fileId,
    {
      targetLanguageId: language
    }
  );
  const response = await fetch(downloadLink.data.url);
  const translations = await response.json();
  console.log(translations);
}

Or specific API instances:

import { Credentials, ProjectsGroups } from '@crowdin/crowdin-api-client';

// credentials
const credentials: Credentials = {
  token: 'personalAccessToken',
  organization: 'organizationName' // optional
};

// initialization of ProjectsGroups
const projectsGroupsApi = new ProjectsGroups(credentials);

// get project list
projectsGroupsApi.listProjects()
  .then(projects => console.log(projects))
  .catch(error => console.error(error));
Javascript ES6 modules
import crowdin, { SourceFilesModel } from '@crowdin/crowdin-api-client';

// initialization of crowdin client
const {
  projectsGroupsApi,
  uploadStorageApi,
  sourceFilesApi,
  translationsApi
} = new crowdin({
  token: 'personalAccessToken',
  organization: 'organizationName' // optional
});

// get project list
projectsGroupsApi.listProjects()
  .then(projects => console.log(projects))
  .catch(error => console.error(error));

// You can also use async/wait. Add `async` keyword to your outer function/method
async function getProjects() {
  try {
    const projects = await projectsGroupsApi.listProjects();
    console.log(projects);
  } catch (error) {
    console.error(error);
  }
}

// Create file with json content to translate
async function createFile() {
  const projectId = 123;
  const fileData = {
    title: 'Example',
    description: 'Some Text'
  };
  const storage = await uploadStorageApi.addStorage('file1.json', fileData);
  const file = await sourceFilesApi.createFile(projectId, {
    name: 'file1.json',
    title: 'Sample file',
    storageId: storage.data.id,
    type: 'json'
  });
  console.log(file);
}

// Download translations
async function downloadTranslations() {
  const projectId = 123;
  const fileId = 456;
  const language = 'de';
  const downloadLink = await translationsApi.buildProjectFileTranslation(
    projectId,
    fileId,
    {
      targetLanguageId: language
    }
  );
  const response = await fetch(downloadLink.data.url);
  const translations = await response.json();
  console.log(translations);
}

Or specific API instances:

import { ProjectsGroups } from '@crowdin/crowdin-api-client';

// initialization of ProjectsGroups
const projectsGroupsApi = new ProjectsGroups({
  token: 'personalAccessToken',
  organization: 'organizationName' // optional
});

// get project list
projectsGroupsApi.listProjects()
  .then(projects => console.log(projects))
  .catch(error => console.error(error));
Javascript CommonJS
const crowdin = require('@crowdin/crowdin-api-client');

// initialization of crowdin client
const {
  projectsGroupsApi,
  uploadStorageApi,
  sourceFilesApi,
  translationsApi
} = new crowdin.default({
  token: 'personalAccessToken',
  organization: 'organizationName' // optional
});

// get project list
projectsGroupsApi.listProjects()
  .then(projects => console.log(projects))
  .catch(error => console.error(error));

// You can also use async/wait. Add `async` keyword to your outer function/method
async function getProjects() {
  try {
    const projects = await projectsGroupsApi.listProjects();
    console.log(projects);
  } catch (error) {
    console.error(error);
  }
}

// Create file with json content to translate
async function createFile() {
  const projectId = 123;
  const fileData = {
    title: 'Example',
    description: 'Some Text'
  };
  const storage = await uploadStorageApi.addStorage('file1.json', fileData);
  const file = await sourceFilesApi.createFile(projectId, {
    name: 'file1.json',
    title: 'Sample file',
    storageId: storage.data.id,
    type: 'json'
  });
  console.log(file);
}

// Download translations
async function downloadTranslations() {
  const projectId = 123;
  const fileId = 456;
  const language = 'de';
  const downloadLink = await translationsApi.buildProjectFileTranslation(
    projectId,
    fileId,
    {
      targetLanguageId: language
    }
  );
  const response = await fetch(downloadLink.data.url);
  const translations = await response.json();
  console.log(translations);
}

Or specific API instances:

const ProjectsGroups = require('@crowdin/crowdin-api-client').ProjectsGroups;

// initialization of ProjectsGroups
const projectsGroupsApi = new ProjectsGroups({
  token: 'personalAccessToken',
  organization: 'organizationName' // optional
});

// get project list
projectsGroupsApi.listProjects()
  .then(projects => console.log(projects))
  .catch(error => console.error(error));

You can generate Personal Access Token in your Crowdin Account Settings.

For more examples please check Examples

List of projects with Fetch API

In addition if you use client in non-Node.js environment you might have a troubles with http calls. This client uses axios which internally uses http and https Node modules. So there is an option to use http client based on Fetch API (keep in mind that fetch should be available in global scope).

import { ProjectsGroups } from '@crowdin/crowdin-api-client';

const projectsGroupsApi = new ProjectsGroups(credentials, {
  httpClientType: 'fetch'
});

Or even pass your own http client as httpClient property which should implement HttpClient interface.

Fetch all records

It is possible to fetch all records from paginatable methods (where we have limit and offset in arguments).

import { ProjectsGroups } from '@crowdin/crowdin-api-client';

// initialization of ProjectsGroups
const projectsGroupsApi = new ProjectsGroups({
  token: 'personalAccessToken',
  organization: 'organizationName' // optional
});

// get all projects
projectsGroupsApi
  .withFetchAll()
  .listProjects()
  .then(projects => console.log(projects))
  .catch(error => console.error(error));

// get projects but not more than 1000
projectsGroupsApi
  .withFetchAll(1000)
  .listProjects()
  .then(projects => console.log(projects))
  .catch(error => console.error(error));

Retry configuration

There is a possibility to configure client invoke http calls with retry mechanism.

import { ProjectsGroups } from '@crowdin/crowdin-api-client';

const projectsGroupsApi = new ProjectsGroups(credentials, {
  retryConfig: {
    retries: 2, // amount of retries (gte 0)
    waitInterval: 100, // wait interval in ms between retries
    conditions: [ // array of conditions which will check if retry should not be applied
      {
        test(error) {
          return error.code === 40
        }
      }
    ]
  }
});

Exception handling

In case of error library will throw an Error based exception. This can either be a generic error with an error message and a code, or a validation error that additionally contains validation error codes.

const crowdin = require('@crowdin/crowdin-api-client');

const token = '';

const { translationsApi } = new crowdin.default({ token });

async function test() {
  const project = 123;
  const dir = 456;
  try {
    const res = await translationsApi.buildProjectDirectoryTranslation(project, dir);
    console.log(JSON.stringify(res));
  } catch (e) {
    if (e instanceof crowdin.CrowdinValidationError) {
      console.log('Validation error');
    } else if (e instanceof crowdin.CrowdinError) {
      console.log('Generic error');
    }
    console.error(e);
  }
}

test();

Http request timeout

By default request timeout will vary on http client implementation and/or environment (e.g. fetch uses timeout configured by the browser).
But there is an option to set constant value:

const crowdin = require('@crowdin/crowdin-api-client');

const credentials = { token: 'token' };

const httpRequestTimeout = 60 * 1000; // 60 seconds

const client = new crowdin.default(credentials, { httpRequestTimeout });

Over-The-Air Content Delivery

💫 Recommended for translations delivery to your website or mobile application.

You can also use the Crowdin OTA Client JS library to send the translated content to your web apps via content delivery. Crowdin Content Delivery uses a CDN vault that mirrors your project’s translated content. The updated translations will become available to users much faster.

GraphQL API

This library also provides possibility to use GraphQL API (only for Crowdin Enterprise).

const crowdin = require('@crowdin/crowdin-api-client');

const client = new crowdin.default({
  token: '{token}',
  organization: '{organization}'
});

const query = `
query {
  viewer {
    projects(first: 50) {
      edges {
        node {
          name
  
          files(first: 10) {
            totalCount
            edges {
              node {
                name
                type
              }
            }
          }
        }
      }
    }
  }
}
`;

client
  .graphql({ query })
  .then(res => console.log(JSON.stringify(res, null, 2)));

Seeking Assistance

If you find any problems or would like to suggest a feature, please read the How can I contribute section in our contributing guidelines.

Need help working with Crowdin JavaScript client or have any questions? Contact Customer Success Service.

Contributing

If you want to contribute please read the Contributing guidelines.

License

The Crowdin JavaScript client is licensed under the MIT License.
See the LICENSE.md file distributed with this work for additional
information regarding copyright ownership.

Except as contained in the LICENSE file, the name(s) of the above copyright
holders shall not be used in advertising or otherwise to promote the sale,
use or other dealings in this Software without prior written authorization.

More Repositories

1

crowdin-cli

A command-line client for the Crowdin API
Java
241
star
2

github-action

A GitHub action to manage and synchronize localization resources with your Crowdin project
Shell
151
star
3

mobile-sdk-ios

Crowdin iOS SDK delivers all new translations from Crowdin project to the application immediately
Swift
116
star
4

mobile-sdk-android

Crowdin Android SDK delivers all new translations from Crowdin project to the application immediately
Kotlin
104
star
5

crowdin-cli-v1

A command-line client for the Crowdin API v1
Ruby
84
star
6

ota-client-js

JavaScript client library for Crowdin Over-The-Air Content Delivery
TypeScript
67
star
7

crowdin-api-client-java

Java client library for Crowdin API
Java
65
star
8

crowdin-api-client-ruby

Ruby client library for Crowdin API
Ruby
56
star
9

android-studio-plugin

Integrate your Android project with Crowdin
Java
56
star
10

crowdin-api-client-php

PHP client library for Crowdin API
PHP
55
star
11

crowdin-api-client-python

Python client library for Crowdin API
Python
54
star
12

crowdin-api-client-dotnet

.NET client library for Crowdin API
C#
53
star
13

vscode-crowdin

Crowdin Explorer for Visual Studio Code
TypeScript
51
star
14

sketch-crowdin

Connect your Sketch and Crowdin projects together
HTML
37
star
15

flutter-sdk

Crowdin Flutter SDK for instant translation delivery Over-The-Air directly to your application
Dart
25
star
16

crowdin-hybrid-sso-examples

Examples using Crowdin hybrid SSO
C#
15
star
17

zapier-integration

Connect Crowdin to hundreds of other apps with Zapier
JavaScript
13
star
18

react-native-sdk

Crowdin React Native SDK delivers all new translations from Crowdin project to the application immediately
Java
10
star
19

homebrew-crowdin

Homebrew Tap for Crowdin CLI
Ruby
9
star
20

editor-app

Crowdin for Mac, Windows, and Linux
JavaScript
8
star
21

react-crowdin-login

React component for a simple OAuth login with Crowdin
TypeScript
7
star
22

translate-readme

A GitHub Action to automate the translation of your README.md files via Crowdin ✨
TypeScript
7
star
23

xamarin-sdk

Crowdin Xamarin SDK delivers all new translations from Crowdin project to the application immediately
C#
7
star
24

crowdin-apps-functions

Common functions used by Crowdin Apps
TypeScript
6
star
25

create-crowdin-app

Crowdin App skeleton
TypeScript
5
star
26

context-harvester

JavaScript
5
star
27

crowdin-script-editor

Crowdin Script Editor is an open source development and debugging tool for Crowdin custom QA checks
JavaScript
5
star
28

crowdin-api-client-go

Go client library for Crowdin API
Go
5
star
29

editor-anywhere

Plugin to any website that allows to search selected text in the Crowdin project and translate it in-place
JavaScript
3
star
30

.github

2
star
31

apps-quick-start

EJS
2
star