Skip to content

Commit

Permalink
Merge branch 'release/1.0.3'
Browse files Browse the repository at this point in the history
* release/1.0.3:
  Version bump
  Added manual translation correction functionality
  • Loading branch information
beneverard committed Sep 5, 2019
2 parents c726b97 + 4779baa commit 767df7b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ The Idea Bureau, WordPresss Foundation

A collection of classes to base WordPress theme functionality on

Version 1.0.2
Version 1.0.3
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "theideabureau/wordpress-foundation",
"version": "1.0.2",
"version": "1.0.3",
"description": "A collection of classes to base WordPress theme functionality on",
"license": "MIT",
"autoload": {
Expand Down
43 changes: 43 additions & 0 deletions src/Translation.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,39 @@ protected function makeKey(string $language_code, string $key) {
return 'translation_' . $language_code . '_' . $key;
}

/**
* attempts to fetch manually corrected translations
* @param string $string the content to translate
* @return mixed the translated string or false
*/
function getCorrectedTranslation(string $string) {

// fetch the google translate corrections array
$translations = get_field('google_translate_correction', 'options');

// if the translations don't exist return false
if ( ! $translations ) {
return false;
}

// attempt to find and return the translated text
foreach ( $translations as $translation ) {

// compare the strings lower case and trimmed, some strings on the
// front end are uppercase (css text-transform) and may be copied
// with their case, this helps match them up

if ( strtolower(trim($translation['english'])) === strtolower(trim($string)) ) {
return $translation['translated'];
}

}

// return false so the caller knows to continue
return false;

}

/**
* return a cached translation if there is one, otherwise translate the string
* @param string $string the content to translate
Expand All @@ -215,6 +248,11 @@ function translateString(string $string, string $key, string $language_code) {
return $string;
}

// check if there is a google translate correction before continuing
if ( $correction = $this->getCorrectedTranslation($string) ) {
return $correction;
}

// check for a cached translation to return
if ( $cached_translation = get_option($this->makeKey($language_code, $key)) ) {
return $cached_translation;
Expand Down Expand Up @@ -259,6 +297,11 @@ function translateString(string $string, string $key, string $language_code) {
*/
function translateContent(string $content, int $post_id, string $key, string $language_code) {

// check if there is a google translate correction before continuing
if ( $correction = $this->getCorrectedTranslation($content) ) {
return $correction;
}

// check for a cached translation to return
if ( $cached_translation = get_post_meta($post_id, $this->makeKey($language_code, $key), TRUE) ) {
return $cached_translation;
Expand Down

0 comments on commit 767df7b

Please sign in to comment.