Cloudwords CLI for Application Developers

The Cloudwords CLI (command line interface) allows developers to easily create Cloudwords projects and get content translated. It is also designed to be scriptable and can be integrated with your SCM (source control management) system or your continuous integration process. This tutorial will illustrate just how simple it is to get your source content into Cloudwords and retrieve the translations once delivered by the vendor.

We welcome your feedback on the CLI. If you have questions, suggestions, or need support using the CLI, please contact us at support@cloudwords.com.

For this tutorial, you will need:

1. A Cloudwords sandbox account. Don't have one? Create one now.
2. The API Access Key for your account. Learn how to get it here.
3. Java 8 or higher
4. The CLI Distributable makes it super simple to integrate with Cloudwords from the command line.

It is recommended you place the CLI executable (cw-cli) somewhere in your PATH. Suggested installation instructions are provided below.

$ cd /opt
$ sudo tar -xzf cw-cli-version.tar.gz

Add the following line to the end of your ~/.bashrc file.

export PATH=/opt/cloudwords-cli:$PATH

Initial Configuration

Before you start using the CLI, you'll need to log in with your API key.

$ cw-cli login prod api-key

The Cloudwords CLI stores your credentials and project details in $HOME/.cloudwords/

General Usage

Running cw-cli without arguments will print a list of "tools". A tool is invoked by running cw-cli tool. A tool will print usage information with a command in the pattern cw-cli tool --help.

Tool Help

usage: /opt/cloudwords-cli/cw-cli info options... Retrieve information about your Cloudwords account -d,--department show available departments -s,--source show available source languages -t,--target show available target languages --vendor <department> show preferred vendors. <department> is optional, unless you do not have a default department. If your account has only one department, the argument is not required. -i,--intended-use show available intended uses -v,--verbose increase message verbosity -h,--help print this message
usage: /opt/cloudwords-cli/cw-cli login [-v] [environment] token Login to Cloudwords -v,--verbose increase message verbosity -h,--help print this message Default environment: sandbox Available environments: prod, sandbox
usage: /opt/cloudwords-cli/cw-cli project [options] [files...] Create projects or update project properties --info print project details -c,--create <name> create project with specified <name> -n,--name <name> set project name to <name> -d,--department <id> set project department ID to <id> -s,--source <code> set project source language by <code> -t,--target <code> set project target languages to <code>, repeat option for multiple languages --intended-use <id> set project intended use ID to <id> -i,--project <id> update project <id> instead of the default project -p,--pattern <patt> update target file pattern to <patt>, this controls where translated files are saved --lang-pattern <code=patt> update target file pattern for target language <code> to <patt>, this is a per-language version of pattern. Repeat parameter for multiple languages. -D,--no-defaults do not update defaults (otherwise project settings are saved as defaults for the next project) -R,--remove <id> removed project <id> from local CLI metadata -u,--updated only upload files that have been updated; the project will not be created if no files need to uploaded -v,--verbose increase message verbosity -h,--help print this message Pattern (--pattern, --lang-pattern) variables: %D absolute path of the directory that contains the source file %d name of the directory that contains the source file %N full name of the source file (not including path) %n name of the source file without extension %E extension of the source file, including '.' %e extension of the source file, excluding '.' %L name of the target language (e.g. French or French (France)) %l target language code (e.g. fr or fr-fr)
usage: /opt/cloudwords-cli/cw-cli pull [options] [directory] Download translations from Cloudwords -p,--project <id> pull files for project ID <id> instead of the default project -a,--approved only download files that have been approved -l,--language <code> only download files for language(s) <codes...> (repeat option for multiple) --pattern <patt> use a the pattern <patt> instead of the project's configured pattern -x,--cross-project download files for all CLI projects -f,--force download files even if the are already up to date -v,--verbose increase message verbosity -h,--help print this message By default, target paths are determined using the project's configured path. Specifying a the <directory> argument will instead download files to that directory.
usage: /opt/cloudwords-cli/cw-cli push [options] files... Add source files to a project -p,--project <id> upload files to project ID <id> instead of the default project -v,--verbose increase message verbosity -h,--help print this message
usage: /opt/cloudwords-cli/cw-cli submit options... Submit project to vendor(s) -b,--bid-due-date <date> set due date for vendors to submit bids -t,--translation-due-date <date> set due date for vendor to deliver translations --vendor <id> set the vendor to submit the project to use (optional) -a,--auto-select-bid <bool> automatically accept the vendor's bid (requires --vendor) if <bool> is true -m,--vendor-marketplace <bool> use the Cloudwords vendor marketplace to add up to 5 vendors for competitive bids (if <bool> is true) -p,--project <id> submit project <id> rather than the default project -v,--verbose increase message verbosity -h,--help print this message Date syntax is based on your system locale. There are several acceptable date formats. For instance the date December 20, 2015 may be entered as: 12/20/15 Date only, the time will always be set to 5PM in the configured time zone. 12/20/15 5:00 PM Date and Time, the time will be interpreted as the configured time zone. 12/20/15 5:00:00 PM EST Date, Time, and Time Zone. Dates can also be relative to the current date using one of the following formats (not case sensitive): +3D 3 days from now at 5PM in configured time zone. +3D+4H 3 days and 4 hours from now, rounded to the nearest hour (4:30 becomes 5:00, 4:29 becomes 4:00). +3D18H 3 days from now at 6:00 PM in configured time zone. +3D18HZAmerica/New_York 3 days from now at 6:00 PM in the time zone "America/New_York". Time Zone names for relative dates can be listed with the tz tool (see /opt/cloudwords-cli/cw-cli tz --help).
usage: /opt/cloudwords-cli/cw-cli tz [-v] [option] [zone] Configure Time Zone -r,--region <name> If <name> is passed, then show time zones in region <name>. Else list all regions -a,--all List ALL available time zones. -v,--verbose increase message verbosity -h,--help print this message

Setting up defaults

Before creating projects, it generally helps to set default values for properties like the Department and Source Language. Projects cannot be created without a Department, but source language is optional. Note: If info --department lists no departments, or only a single department, then you do not need to set a default department.

$ cw-cli info --department
Available Departments:
ID  Department Name
1   Unassigned
99  Product
101 Development
263 Customer Success
513 Channel
515 Legal
611 Sales
955 Marketing
$ cw-cli defaults --department 955
Defaults updated
Default Project: Not set
Default Department for new projects: Marketing (#955)
Default source language for new projects: Not set
Default target language(s) from new projects: Not set
Root directory: None

Creating a project

A simple command will create a project.

$ cw-cli project --create "test project"
Successfully created project 15315

To add a source file use the push tool

$ cw-cli push test.txt
Uploading /Users/cloudwords/test.txt
Uploaded 1 files

Note: Creating a project (or updating one) sets that project to the default project. This default can be controlled with the defaults tool. Operations which do not specify a project ID execute on the default project. See the usage information for each tool to see how to specify the project ID.

For convenience, the project create and push commands can be combined into a single command.

$ cw-cli project --create "test project" test.txt
Successfully created project 15316
Uploading /Users/cloudwords/test.txt
Uploaded 1 files

Additionally, the parameter --updated can be added to the above command. This will suppress upload of a file, if that file has not been changed since it was last sent out for translation, or if it is currently in translation in an active project. No project will be created at all unless at least one file is eligible for upload.

Patterns

In order to download translations, it is necessary to set the project pattern. This pattern allows the CLI to determine where a translated file should be saved, based on the location of the source. A simple pattern would be %D/%l/%N. This would store the translation in a sub-directory of the directory the source file was in named with the target language code. The target file would have the same name as the source file. For instance, if this pattern were applied to the project created in the previous example, and the target language were French, the target location would be /Users/cloudwords/fr/test.txt.

Pattern codes

CodeDefinition
%DThe absolute path of the directory that contains the source file (e.g. /Users/cloudwords)
%dThe name of the directory that contains the source file (e.g. cloudwords)
%NThe full name of the source file (e.g. test.txt)
%nThe name of the source file, without the extension (e.g. test). This includes the name up to the last dot in the filename
%EThe extension of the filename, including the dot (e.g. .txt)
%eThe extension of the filename, without the dot (e.g. txt)
%LThe full name of the target language (e.g. French or French (France))
%lThe target language code (e.g. fr or fr-fr)

The pattern may be set at any time before translations are downloaded using the project tool. The following command will update the pattern for the default project.

$ cw-cli project --pattern %D/%l/%N
                

This option can also be added when creating a project to set the pattern when the project is created.

Downloading Translations

The pull tool (cw-cli pull) will download translations. The -x parameter (--cross-project) will pull translations for all projects. The -a parameter (--approved) will only pull translations that have been approved. The Cloudwords API provides the last time a translation was updated, once a translation has been pulled it will not be downloaded again until it is updated.

Sample Scripts

The Cloudwords CLI is intended to be scriptable. This means that it can be used in shell scripts to automate your translation process in various ways. A prime example of this is to automatically submit files for translation when updates to them are committed to your version control repository (git, Mercurial, Subversion, etc.).

The following script is an example of a post-receive hook for git. It will automatically create a project when files ending in -en.html in the root are updated. The script should be easy to modify to handle whatever source files you are targeting. The script requires an existing working directory, which it will update in order to check source files and perform commits.

#! /bin/bash

# To use this script, name it hooks/post-receive in your central git repository.

# Set the parameters below based on the details of your installation
REPO=/Users/cloudwords/version-control-test/gitrepo
WORKING_DIR=/Users/cloudwords/version-control-test/working/git
CW_CLI=/opt/cloudwords-cli/cw-cli

unset GIT_DIR

cd $WORKING_DIR

git pull

# pull -x means "pull for all projects", -a means "only pull files which have been reviewed and approved"
$CW_CLI pull -x -a

CHANGES=$(git status -s | wc -l)

if [ "$CHANGES" != "0" ]; then
    git add --all .
    git commit -m "(Cloudwords Integration) Add translations from Cloudwords"
    git push
fi

HEAD=`git rev-list --count HEAD`

# This is the project name
PROJECT_NAME="Changes in $HEAD"

# The files to translate are set at the end of the last command
$CW_CLI project -c "$PROJECT_NAME" --pattern %D/%n-%l.html -u *-en.html
                
#!/bin/sh

# To use this script, name it hooks/post-commit in your central svn repository.

# Set the parameters below based on the details of your installation
CW_CLI=/opt/cloudwords-cli/cw-cli
WORKING_DIR=/Users/cloudwords/version-control-test/working/svnrepo

if [ ! -e "$WORKING_DIR" ]; then
    svn co "file://$1" $WORKING_DIR
else
    cd $WORKING_DIR
    svn update
fi

cd $WORKING_DIR

$CW_CLI pull -x -a

CHANGES=$(svn st | wc -l)

if [ "$CHANGES" != "0" ]; then
    svn add --force * --auto-props --parents --depth infinity -q
    svn ci -m "(Cloudwords Integration) Add translations from Cloudwords" --force-log
fi

$CW_CLI project -c "changes in $2" --pattern %D/%n-%l.%e -u *-en.html
                            

This script should be easy to modify for your particular source control system. However, since it only runs when a commit is made, this may result in delays in receiving translations. To improve this, the following script can be run as a cron job.

#! /bin/bash

# Set the parameters below based on the details of your installation
REPO=/Users/cloudwords/version-control-test/gitrepo
WORKING_DIR=/Users/cloudwords/version-control-test/working/git
CW_CLI=/opt/cloudwords-cli/cw-cli

unset GIT_DIR

if [ ! -e "$WORKING_DIR" ]; then
    git clone $REPO $WORKING_DIR
else
    cd $WORKING_DIR
    git pull
fi

cd $WORKING_DIR

$CW_CLI pull -x -a

CHANGES=$(git status -s | wc -l)

if [ "$CHANGES" != "0" ]; then
    git add --all .
    git commit -m "(Cloudwords Integration) Add translations from Cloudwords"
    git push
fi
                
#! /bin/bash

CW_CLI=/opt/cloudwords-cli/cw-cli
WORKING_DIR=/Users/cloudwords/version-control-test/working/svnrepo

if [ ! -e "$WORKING_DIR" ]; then
    svn co "file://$1" $WORKING_DIR
else
    cd $WORKING_DIR
    svn update
fi

cd $WORKING_DIR

$CW_CLI pull -x -a

CHANGES=$(svn st | wc -l)

if [ "$CHANGES" != "0" ]; then
    svn add --force * --auto-props --parents --depth infinity -q
    svn ci -m "(Cloudwords Integration) Add translations from Cloudwords" --force-log
fi

                            

By adding the last line of the of the post-receive script to the script above, it can also create projects. This may be useful if you cannot create a post-receive hook in your control system. This is the case for many cloud-based version control services.