Crowdin Apps JS

The Crowdin Apps JS is a library built for easier interaction with Crowdin. Since the integration uses the iframes, there are certain limitations on the interaction between windows: environments are encapsulated and do not have access to the page content, code, styles, and any other information related to the page.

The Crowdin Apps JS library is based on cross-window messaging technology and uses postMessage() to interact between multiple windows.

Getting Started

To use this library, connect it on the module page by inserting the following code into the page header:

<script src="https://cdn.crowdin.com/apps/dist/iframe.js"></script>

After adding the code mentioned above, you’ll be able to use the global AP variable.

Note! Don't download the iframe.js file and serve it up from your app server directly. The iframe.js file must be served up by Crowdin for establishing of the cross-domain messaging bridge. The iframe.js file is only intended for use in an iframe inside Crowdin and does not work for standalone web pages.

Global Actions

Global actions are the actions that are available on all module pages.

AP.getContext(callback)

The method retrieves the context information about the current page and puts it into the callback. For example, the project ID where the module is opened or the locale used on the page.

Example:

  AP.getContext(function(contextData) {
    console.log("Object: context", contextData);
  });

Properties:

callback

Type: function

Description: The callback that handles the response object.

Depending on the current page on which this method is used, the payload may contain a different set of the context information.

Payload example:

{
    "project_id": "12",
    "organization_id": "200000000",
    "editor": {
        "mode": "translate",
        "theme": "dark",
        "source_language_id": "en",
        "target_language_id": "fr",
        "file": 12345,
        "workflow_step": {
            "id": 7777,
            "title": "Translation",
            "type": "Translate"
        }
    }
}

Properties:

project_id

Type: string

Description: The identifier of the Crowdin project.

organization_id

Type: string

Description: Crowdin Enterprise only. The identifier of the organization in Crowdin Enterprise.

editor

Type: object

Description: Contains settings and context for the Editor.

editor.mode

Type: string

Description: Active mode of the Editor.

editor.theme

Type: string

Description: Active theme of the Editor.

editor.source_language_id

Type: string

Description: The identifier of the source language.

editor.target_language_id

Type: string

Description: The identifier of the target language.

editor.file

Type: integer

Description: The numeric identifier of a file in the Crowdin project that is open in the Editor.

editor.workflow_step

Type: object

Description: Crowdin Enterprise only. Details of the current workflow step in the Editor.

editor.workflow_step.id

Type: integer

Description: The numeric identifier of the workflow step.

editor.workflow_step.title

Type: string

Description: Title of the workflow step.

editor.workflow_step.type

Type: string

Description: Type of the workflow step.

AP.getJwtToken(callback)

The method retrieves a JWT token string. Used to get a current token. If the previous token has a validity period of less than one minute, the method will generate and return a new token.

Example:

  AP.getJwtToken(function(jwtToken) {
    console.log("String: JWT token", jwtToken);
  });

Properties:

callback

Type: function

Description: The callback that handles the response string.

AP.getTheme()

The method retrieves information about the currently selected theme.

Example:

  AP.getTheme(function(theme) {
    light | dark
  });

Properties:

callback

Type: function

Description: The callback that handles the response string.

Editor Module Actions

Editor module actions provide a possibility to get information from the Editor UI. These actions are available only on pages that are loaded in an iframe in the Editor-panels module.

AP.editor.getString(callback)

The method retrieves information about the currently selected string in the Editor.

Example:

  AP.editor.getString(function(stringData) {
    console.log("Object: source string", stringData);
  });

Properties:

callback

Type: function

Description: The callback that handles the response object.

AP.editor.getTranslations(callback)

The method retrieves a list of translations suggested to the currently selected string in the Editor. Additionally, the response contains information about the translation author, the number of votes, and approval on the current workflow step.

Example:

  AP.editor.getTranslations(function(translationsData) {
    console.log("Array: translation objects", translationsData);
  });

Properties:

callback

Type: function

Description: The callback that handles the response array.

AP.editor.getTopTranslation(callback)

The method retrieves a top translation (the translation with the highest priority) for the currently selected source string in the Editor.

Example:

  AP.editor.getTopTranslation(function(topTranslationData) {
    console.log("Object: top translation", topTranslationData);
  });

Properties:

callback

Type: function

Description: The callback that handles the response object.

AP.editor.setTranslation(text)

The method sets the translation text for the currently selected source string in the Editor.

Example:

AP.editor.setTranslation("Hello,");

Properties:

text

Type: string

Description: The translation text that is being set for the currently selected source string.

AP.editor.appendTranslation(text)

The method appends the translation text next to the cursor for the currently selected source string in the Editor.

Example:

AP.editor.appendTranslation(" {user},");

Properties:

text

Type: string

Description: The translation text that is being appended next to the cursor for the currently selected source string.

AP.editor.clearTranslation()

The method clears the translation text for the currently selected source string in the Editor.

Example:

AP.editor.clearTranslation();

AP.editor.setFocus()

The method sets focus on the translation field in the Editor.

Example:

AP.editor.setFocus();

AP.editor.noticeMessage(message)

The method displays the notice message text in the Editor.

Example:

AP.editor.noticeMessage("Message text");

Properties:

text

Type: string

Description: The notice message text that is being displayed.

AP.editor.successMessage(message)

The method displays the success message text in the Editor.

Example:

AP.editor.successMessage("Message text");

Properties:

text

Type: string

Description: The success message text that is being displayed.

AP.editor.errorMessage(message)

The method displays the error message text in the Editor.

Example:

AP.editor.errorMessage("Message text");

Properties:

text

Type: string

Description: The error message text that is being displayed.

AP.editor.setApplicationNotification(1)

The method sets a notification count next to an app icon in the right panel in the Editor.

Example:

AP.editor.setApplicationNotification(1);

Properties:

count

Type: integer

Description: The notification count number.

AP.editor.clearApplicationNotification()

The method clears a notification count next to an app icon in the right panel in the Editor.

Example:

AP.editor.clearApplicationNotification();

Event Module Actions

Event module actions provide a possibility to work with events, enable the Crowdin app to respond to events that happen in Crowdin UI, and trigger its own events. Read more about supported events.

AP.events.once(event, callback)

The method subscribes a one-time listener to detect events with a specified name. The listener is unsubscribed as soon as the first event is processed.

Example:

  AP.events.once("event", function(eventData) {
    console.log("Event data", eventData);
  });

Properties:

event

Type: string

Description: The name of the event the callback of the function should react to.

callback

Type: function

Description: The callback that handles the event. Depending on the event's type, there could be either one or a few arguments in the callback.

AP.events.on(event, callback)

The method subscribes a listener to all events with a specified name.

Example:

  AP.events.on("event", function(eventData) {
    console.log("Event data", eventData);
  });

Properties:

event

Type: string

Description: The name of the event the callback of the function should react to.

callback

Type: function

Description: The callback that handles the event. Depending on the event's type, there could be either one or a few arguments in the callback.

AP.events.off(event, callback)

The method unsubscribes a listener for an event with a specified name.

Example:

  function callback(eventData) {
    console.log("Event data", eventData);
  };

  AP.events.on("event", callback);
  AP.events.off("event", callback);

Properties:

event

Type: string

Description: The name of the event the callback of the function should be unsubscribed from.

callback

Type: function

Description: The callback that was previously subscribed to the event.

AP.events.ofAll(event)

The method unsubscribes all previously subscribed listeners from the specified event.

Example:

  AP.events.offAll("event");
event

Type: string

Description: The name of the event all listeners should be unsubscribed from.

AP.events.onAny(callback)

The method subscribes to all events from Crowdin UI.

Example:

  AP.events.onAny(function(event, data) {
    console.log("Event name string", event);
    console.log("Event data", data);
  });

Properties:

callback

Type: function

Description: The callback that receives the name of the event and all the data transferred to the event.

AP.events.offAny(callback)

The method unsubscribes a listener from all events.

Example:

  function callback(event, data) {
    console.log("Event name string", event);
    console.log("Event data", data);
  };

  AP.events.onAny(callback);
  AP.events.offAny(callback);

Properties:

callback

Type: function

Description: The callback that was subscribed to all events.

Supported Events

Check out the list of supported events that could be passed to the Crowdin Apps JS library in the following table:

Event Example Description
string.change

      {
        "id": 3939912,
        "text": "source string",
        "context": "string context",
        "max_length": 35,
        "file": {
          "id": 26588,
          "name": "file name.csv"
        }
      }
      
The event emitted when a user switches from one string to another.
textarea.edited

      {
        "id": 3939912,
        "text": "source string",
        "context": "string context",
        "max_length": 0,
        "file": {
          "id": 26588,
          "name": "file name.csv"
        },
        "oldText": "chaîne de caractères source",
        "newText": "chaîne source"
      }
      
The event emitted when a user performs any changes in the translation field.
translation.added

      {
        "id": 422648,
        "string_id": 3939912,
        "text": "chaîne source",
        "target_language_id": "fr",
        "votes_rating": 0,
        "approved": false,
        "author": {
          "id": "1",
          "login": "user.login",
          "name": "User Name",
          "avatar_url": "https://avatar.com/avatar.png"
        },
        "created_at": "2022-12-09T12:17:37-05:00"
      }
      
The event emitted when a user saves a translation for the current string.
translation.deleted

      {
        "id": 422560,
        "string_id": 3939912
      }
      
The event emitted when a user deletes a translation.
translation.restored

      {
        "id": 422648,
        "string_id": 3939912,
        "text": "chaîne source",
        "target_language_id": "fr",
        "“votes_rating”": 0,
        "author": {
          "id": "1",
          "login": "user.login",
          "name": "User Name",
          "avatar_url": "https://avatar.com/avatar.png"
        },
        "created_at": "2022-12-09T12:17:37-05:00"
      }
      
The event emitted when a user restores a deleted translation.
translation.vote

      {
        "id": 422648,
        "string_id": 3939912,
        "text": "chaîne source",
        "target_language_id": "fr",
        "votes_rating": 1,
        "approved": false,
        "author": {
          "id": "1",
          "login": "user.login",
          "name": "User Name",
          "avatar_url": "https://avatar.com/avatar.png"
        },
        "created_at": "2022-12-09T12:17:37-05:00"
      }
      
The event emitted when a user votes for a translation.
translation.approve

      {
        "id": 422648,
        "string_id": 3939912,
        "text": "chaîne source",
        "target_language_id": "fr",
        "votes_rating": 0,
        "approved": true,
        "author": {
          "id": "1",
          "login": "user.login",
          "name": "User Name",
          "avatar_url": "https://avatar.com/avatar.png"
        },
        "created_at": "2022-12-09T12:17:37-05:00",
        "approver": {
          "id": "1",
          "login": "super.user",
          "name": "Super User",
          "avatar_url": "https://avatar.com/avatar.png"
        },
        "approved_at": "2022-12-09T17:20:28.655Z"
      }
      
The event emitted when a user approves a translation.
translation.disapprove

      {
        "id": 422406,
        "string_id": 3939858,
        "text": "chaîne de caractères source",
        "target_language_id": "fr",
        "votes_rating": 0,
        "approved": false,
        "author": {
          "id": "1",
          "login": "user.login",
          "name": "User Name",
          "avatar_url": "https://avatar.com/avatar.png"
        },
        "created_at": "2022-12-08T12:29:07-05:00"
      }
      
The event emitted when a user removes a translation approval.
language.change

      {
        "project_id": "15",
        "organization_id": "200000000",
        "editor": {
          "mode": "translate",
          "theme": "dark;",
          "source_language_id": "en",
          "target_language_id": "fr",
          "file": 26588,
          "workflow_step": {
            "id": 77,
            "title": "Translation",
            "type": "Translate"
          }
        }
      }
      
The event emitted when a user changes a target language in the Editor.
file.change

      {
        "project_id": "15",
        "organization_id": "200000000",
        "editor": {
          "mode": "translate",
          "theme": "dark;",
          "source_language_id": "en",
          "target_language_id": "fr",
          "file": 26574,
          "workflow_step": {
            "id": 77,
            "title": "Translation",
            "type": "Translate"
          }
        }
      }
      
The event emitted when a user changes a file in the Editor.
theme.changed

      "light"
      
The event emitted when a user switches from one theme to another.

Was this article helpful?