This how-to guide describes how to set up automatic machine-based translations for content on a Drupal site. Whenever a node is created, translation jobs will be created for every language specified, and depending on how you set up your translator, you should be able to completely automate the translation process. For this project we are using SDL, but you should be able to use other translators.
1. Download and enable the translation management modules. This can be done through Drush or through the modules interface in drupal.
drush dl -y tmgmt
drush dl -y tmgmt_sdl_beglobal
drush en -y tmgmt_sdl_beglobal
drush en -y tmgmt_node
drush en -y tmgmt_ui
drush en -y rules_admin
# optional
drush en -y tmgmt*
2. [Optional] Add languages. This step is only necessary if you have started from a vanilla Drupal install or do not have more than one language added to your site. To add additional languages, go to Configuration -> Regional and Language -> Languages -> Add Language.
3. Get an SDL API key. We need to obtain an API key for the translator to work. To do that, go to "languagecloud.sdl.com" and generate an API key.
4. Configure the SDL translator. Go to Configuration -> Translation management translators. Edit "SDL BeGlobal translator", and add the API key generated in the previous step. Select "Machine Translation", and map language codes in the remote language mapping settings. Also, check the option to auto accept translations. This is important. If you don’t check this, jobs will not be auto-accepted and will have to be run manually. If you don’t see the languages you want to translate to, step 2 will help you add languages to your site.
5. Import rules component. A rules component is analogous to the function construct from most programming languages. More information about components can be found here. We use a component to do almost all the work for us. To set up the component, go to Configuration -> Workflow -> Rules -> Components. By default, there are components from TMGMT that can help make routine translation tasks easier, but none in particular “auto-translate” a node. Depending on whether you enabled all TMGMT modules or not, you may or may-not see all of these components. We do not need them all for our current goal, but they provide useful functionality and I encourage you to explore them. To keep things simple, I’ve created a simple component, that takes as parameters a node, a list of languages and the machine name for a translator. It can then create translation jobs with the translator specified for every language that is in the list. Import the component from this gist. Alternatively, you could build your own component, but this should probably serve as a good starting point.
{
"tmgmt_node_ui_translate_node_to_multiple_languages" : {
"LABEL" : "Translate node to multiple languages",
"PLUGIN" : "rule",
"OWNER" : "rules",
"REQUIRES" : [ "tmgmt", "rules" ],
"USES VARIABLES" : {
"node" : { "label" : "Node", "type" : "node" },
"target_languages" : { "label" : "Target languages", "type" : "list\u003Ctext\u003E" },
"translator" : { "label" : "Translator", "type" : "tmgmt_translator" }
},
"DO" : [
{ "LOOP" : {
"USING" : { "list" : [ "target-languages" ] },
"ITEM" : { "target_language" : "Current target language" },
"DO" : [
{ "tmgmt_rules_create_job" : {
"USING" : { "source_language" : [ "node:language" ] },
"PROVIDE" : { "job" : { "job" : "Job" } }
}
},
{ "tmgmt_rules_job_add_item" : {
"job" : [ "job" ],
"plugin" : "node",
"item_type" : "node",
"item_id" : [ "node:nid" ]
}
},
{ "data_set" : { "data" : [ "job:target-language" ], "value" : [ "target-language" ] } },
{ "data_set" : { "data" : [ "job:translator" ], "value" : [ "translator" ] } },
{ "tmgmt_rules_job_checkout" : { "job" : [ "job" ] } }
]
}
}
]
}
}
Note: This component has been sent up to the tmgmt maintainers as a Drupal issue. You can help get it committed to the project by testing and commenting that it works.
6. Import rule. Now, switch to the Rules tab. We need to set up a rule that will perform the actions from our component. To do that, create a new rule that reacts when new content is being saved, and set our component(the one we imported/created earlier) as the action that is performed. The parameters for the component need to be specified. You can build this rule yourself or you could import the following rule and use it as a template. It is a simple rule that translates nodes to french and german when they are first created.
{
"rules_auto_translate_to_multiple_languages" : {
"LABEL" : "Auto translate to multiple languages",
"PLUGIN" : "reaction rule",
"OWNER" : "rules",
"REQUIRES" : [ "rules" ],
"ON" : { "node_insert" : [] },
"DO" : [
{ "component_tmgmt_node_ui_translate_node_to_multiple_languages" : {
"node" : [ "node" ],
"target_languages" : { "value" : [ "fr", "de" ] },
"translator" : "sdl_beglobal"
}
}
]
}
}
7. Try it out. Try creating a new node. If you’ve set everything up correctly, you should see your node with links to the translated alternatives.
I should note that it might not be practical to do this for every node on your site. But that’s the best part about using Rules. You should be able to use these and tweak them to suit your needs.