Quick Start
In this article, you’ll find information on the fundamental principles of building a Crowdin Application. As a result, you’ll be able to build your own app along the way. To accomplish that, you’ll be using Node.js and the Heroku platform.
Prerequisites:
- Installed Node.js and NPM.
- Registered free account on the Heroku platform.
Setup
In this step, you need to download the sample app code on your local machine to check it out in detail.
Download the app code from GitHub and install all the needed dependencies using the following commands:
At this point, you have a working app with the following structure:
public
– The directory with public assets (e.g., images, styles, fonts, etc.).resources/views
– The directory with HTML page templates.app.json
– The descriptor file for the Heroku platform that describes the Heroku app and how to run it.manifest.js
– The descriptor of your app that describes the Crowdin app itself and its basic structure.index.js
– The main file and entry point of the app.router.js
– The file responsible for router initialization and the primary endpoints of the app.package.json
– The file that contains information about the app and its libraries.
Now the app is ready for deployment on Heroku. As you may notice in the manifest.json, the app in its current state contains only the project-menu module and doesn’t require authorization, i.e., the app won’t have access to the API.
Deploying Crowdin App
In this step, you’ll deploy the app and install it in your Crowdin account.
First, create an app on the Heroku platform. You can do it with the following command:
The app name is optional. If you don’t specify it, Heroku will do it for you automatically.
Once the app is created, Heroku CLI with respond with the following output:
As you can see in the response above, Heroku has successfully created a new app and provided the URL to the app itself and the repository where it will be stored. Also, Heroku automatically connects the created repository to your local Git.
Copy the app URL and add it to your Heroku environment variables using the following command (ensure to replace <crowdin-app-name>
with your app’s name):
All preparations are now complete, and you can deploy the app and run it with the following commands:
This app is now globally available and can be installed in the Crowdin account. After executing the heroku open
command, it will automatically open the deployed app in your default browser. On the opened page, you will see a form with the manifest URL. Copy this URL and install the app in your Crowdin account using the manual installation. After the installation, go to one of your existing projects or create a new one. Among the project tabs, you will see a new tab called “Getting Started”. If you see a welcome message when opening the app, it has been installed successfully.
At this stage, the app doesn’t provide a lot of features. It can only get contextual information about the project it’s installed in. Click on the AP.getContext()
button to get information about the project. To develop more complex apps, you will need access to the API. Now you can go to your Crowdin Account Settings > Crowdin Applications and uninstall the app.
Adding API Access to Crowdin App
In this step, you can provide the app with access to the API. To work with the Crowdin and Crowdin Enterprise API, you need an OAuth application.
To create an OAuth app, follow these steps:
- Open your Account Settings and go to the OAuth Applications tab.
- Click New application.
- In the appeared dialog, specify Getting Started in the Name field.
- Specify
http://localhost
in the Authorization callback URLs field. - Select All scopes.
- Click Create.
Now you have a new entry in the OAuth application table. Open it with a double-click. In the appeared dialog, you’ll see the Client ID and Client secret required for the app to work. In addition to the OAuth app, you need a database to store the API token received from Crowdin. To accomplish that, execute the following command in the console:
Also, add the following environment variables to your app by executing the following commands (ensure to replace the placeholders <OAuth App Client ID>
and <OAuth App Client Secret>
with data from the OAuth app you created):
Now go back to your local project and open the manifest.js
file. In this file, replace the authorization
and events
nodes with the following code:
After these changes, during the app installation, Crowdin will try to transfer the authorization code to the app for the events specified in the manifest. So you need to add these routes and connect the database to save the access token for further use.
Now for connecting to the database, create a db.js
file and insert the following code listing:
In the code listing, you create a connection to the database, as well as an Organization model to store the received data in it.
Open the index.js
file and connect the database so it can be initialized with the app. For this, wrap the app.listen(…)
function with the following code:
Now you can start creating the routes that will read events from Crowdin. Open the router.js
file and add the following routes to it before the variable export:
After receiving a request for the installed event, you can obtain an authorization code for the app using the OAuth Authorization Code flow. So you need to create a payload for extracting the access token according to this flow and make a request using the Axios library and save the received data to the Organization model or update the data in this model. For the uninstall event, the saved entry in the model will be deleted.
Since the app will now work with the API and will have access to information in the project, it must be protected from third-party access so that unauthorized users of the app won’t access Crowdin account data. You can do it by adding the middleware to the module’s project menu in the route.js
file.
Open the route.js
file and add the middleware:
When opening the module page, Crowdin sends a JWT token in the request, signed by the OAuth Client Secret with information about the user that currently opens the app. Based on this, the middleware will verify access to the app.
Modify the project menu module route to the following:
Also, add a route for extracting information about the current user using the stored access token if the user is authorized through the API. First, create the apiClient.js
file and add the following code listing:
The code above allows you to extract the current access token for accessing the API.
If the token has expired, then based on the refresh token, a new current token is extracted and saved in the Organization model. Also, a function with an API client with an authorization header is exported for the current organization.
Now open the route.js
file and add the action for retrieving information about the user:
The app is now ready to work with the API. So you can deploy the changes and install the new version of the app in Crowdin. To apply the changes, execute the following commands:
As previously, copy the manifest URL and install the app in your Crowdin account using the manual installation.
Once installed, open the app in the project tab. Since the user is now authorized, we have more possibilities for the app. Click Show user details to get information about the user.
In the next step, you will add the ability to process custom file formats. For now, you can go to your Crowdin Account Settings > Crowdin Applications and uninstall the app.
Adding Custom File Format Module to Crowdin App
In this step, you will add to your app the possibility of custom processing a JSON file and building a preview for it.
Open the manifest.js
file and add a new module type by adding the following code to the modules
node:
With the code above, you can add a module with the custom-file-format key that will process the file in the JSON format containing “hello_world”
inside. If you upload such a file to the Crowdin project, it will be sent to the route process for custom parsing.
Create the fileHelper.js
file in the project root and add the following code listing to it:
The code listing above contains two main methods for working with a file: parseFile
and buildFile
.
parseFile
– Allows processing the source file’s content, extracting strings for translation from the source file, and building an HTML preview for the source file, so that translators can conveniently review the content in the Crowdin editor and translate it.buildFile
– Allows building a file for export in the original custom format using the strings imported to Crowdin.
Now open the route.js
file and create a new route that will be receiving content from Crowdin:
Besides the /process
route, you also need to add a route for downloading the file if its size exceeds 5MB. Now the app can process a custom JSON file.
Execute the following commands to deploy the changes:
As previously, copy the manifest URL and once again install the app in your Crowdin account using the manual installation. Then create a JSON file on your local machine and insert the following content:
This is the source file you will be uploading to your Crowdin project for translation. Now open your test project in Crowdin and upload just created JSON source file to it. Once the file is uploaded and imported, Crowdin will display that it contains two strings for translation. Go to the Dashboard tab, select one of the target languages and click on the file. You will see a custom preview for this JSON file in the editor’s left panel.