I programmed the automated social media postings last week and have seen a visitor increase of 800 percent. Now I want to jump into it and program an automated translation, which then lets me automatically address my blog to almost the whole world.
But I would have to first proofread and release these posts. Then they would have to be crawled by the entire search engine and then you can expect slowly with visitors. So I give about two months before I can count on the first successes.
The Google Cloud Translation API
Well then let’s start. First, we need to create a Google Account and deposit our credit card details.
Please remember that every letter that gets translated costs money! The prices are quite payable. That’s why I’ll test it now.
So. After we have deposited our credit card data, we have to create a project and generate an API key within this project. We need to limit this API key to our server. Once we’ve done that, we’ll need to unlock the Google Cloud translation api and set our quotas for this project. It is extremely important to limit the API key to the server and set quotas, otherwise costs of several thousand Euros can be generated very quickly.
The REST interface
If you still have little experience with REST API’s, you are welcome to copy this function for use:
function REST($method, $url, $data = false){
$curl = curl_init();
switch ($method)
{
case "POST":
curl_setopt($curl, CURLOPT_POST, 1);
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
case "PUT":
curl_setopt($curl, CURLOPT_PUT, 1);
break;
default:
if ($data)
$url = sprintf("%s?%s", $url, http_build_query($data));
}
// Optional Authentication:
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "username:password");
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_REFERER, actual_link());
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
After you’ve done that, you should write a function that automatically uses Google’s REST API combined with the translation API and if successful, returns the translation.
function translate_text($args){
$ret = json_decode( (string) REST(
'POST',
'https://translation.googleapis.com/language/translate/v2?key=XXXXXXXXXX',
array(
'source' => $args['source_lang'],
'target' => $args['target_lang'],
'q' => $args['text']
)
));
return ($ret->error)? false : $ret->data->translations[0]->translatedText;
}
Translate Tags
If you are using Tags, it would be important to have them translated. In my case, this is important because my keywords are automatically posted as hashtags in the social media posts.
function translate_term($args){
$source_lang = $args['source_lang'];
$target_lang = $args['target_lang'];
$term_name_translation = translate_text(array(
'source_lang' => $source_lang,
'target_lang' => $target_lang,
'text' => $args['source_term']->name
));
$translated_term_id = wp_insert_term(
$term_name_translation,
$args['source_term']->taxonomy,
array(
'parent'=> $args['source_term']->parent,
'slug' => sanitize_title($term_name_translation).'-'.$target_lang
)
);
if($translated_term_id = $translated_term_id['term_id']):
pll_set_term_language($translated_term_id, $target_lang);
pll_save_term_translations(array(
$source_lang => $args['source_term']->term_id,
$target_lang => $translated_term_id
));
else:
var_dump('ERROR!');
var_dump($translated_term_id);
endif;
return $translated_term_id;
}
At the end, I also wrote a function that automatically translates and adds the keywords to a particular post:
function translate_post_tags($args){
$source_lang = $args['source_lang'];
$target_lang = $args['target_lang'];
$terms_to_append = array();
foreach (wp_get_post_tags($args['source_post_id']) as $original_tag) {
$translated_term_id = pll_get_term($original_tag->term_id, $target_lang);
if (!$translated_term_id) $translated_term_id = translate_term(array(
'source_lang' => $source_lang,
'target_lang' => $target_lang,
'source_term' => $original_tag
));
$terms_to_append[] = get_term_by( 'term_id', $translated_term_id, $original_tag->taxonomy )->name;
}
return wp_set_post_tags( $args['target_post_id'], $terms_to_append , true );
}
Afterwards you can use these functions to automatically translate posts, tags and excerpts.
Translate whole posts
The following function automatically translates and creates a post. This is saved as a draft and attached to the original post as a translation. I use the Polylang plugin to manage my translations. That’s why you will also see polylang functions in the code, which you can then replace with your translation plugin:
/****
translate_post(array(
'post_id' => $post_id
'source_lang' => $source_lang
'target_lang' => $target_lang
))
****/
function translate_post($args){
$post_to_translate = get_post($args['post_id']);
$source_lang = $args['source_lang'];
$target_lang = $args['target_lang'];
$translated_content = translate_text(array(
'source_lang' => $source_lang,
'target_lang' => $target_lang,
'text' => $post_to_translate->post_content
));
$translated_excerpt = translate_text(array(
'source_lang' => $source_lang,
'target_lang' => $target_lang,
'text' => $post_to_translate->post_excerpt
));
$translated_title = translate_text(array(
'source_lang' => $source_lang,
'target_lang' => $target_lang,
'text' => $post_to_translate->post_title
));
// if(!$translated_content || !$translated_excerpt) return false;
$translated_post_id = wp_insert_post(array(
'post_author' => $post_to_translate->post_author,
'post_date' => $post_to_translate->post_date,
'post_date_gmt' => $post_to_translate->post_date_gmt,
'post_content' => $translated_content,
'post_title' => html_entity_decode($translated_title, ENT_QUOTES),
'post_excerpt' => html_entity_decode($translated_excerpt, ENT_QUOTES),
'post_status' => 'draft',
'post_type' => $post_to_translate->post_type,
'post_modified' => $post_to_translate->post_modified,
'post_modified_gmt' => $post_to_translate->post_modified_gmt,
'post_category' => $post_to_translate->post_category,
));
foreach (get_post_meta($post_to_translate->ID) as $key => $value) {
if(
$key !== 'tweet_id' &&
$key !== 'facebook_post_id'
) update_post_meta( $translated_post_id, $key, $value[0] );
if($key == 'subline') update_post_meta( $translated_post_id, $key, translate_text(array(
'source_lang' => $source_lang,
'target_lang' => $target_lang,
'text' => $value[0]
)) );
}
pll_set_post_language($translated_post_id, $target_lang);
pll_save_post_translations(array(
$source_lang => $post_to_translate->ID,
$target_lang => $translated_post_id
));
translate_post_tags(array(
'source_post_id' => $post_to_translate->ID,
'target_post_id' => $translated_post_id,
'source_lang' => $source_lang,
'target_lang' => $target_lang
));
return $translated_post_id;
}
I’ve been testing this for a few days now and it works fine. The only thing I have to do now is to review all the posts I have written so far and publish them. This will cause chaos on my social media channels in the beginning, but settle down later.