Skip to content

Commit

Permalink
Separate translations of single words to enable more precise tracking (
Browse files Browse the repository at this point in the history
  • Loading branch information
loranmutafov authored and oradwell committed Mar 20, 2017
1 parent 0786b64 commit 762b605
Showing 1 changed file with 47 additions and 33 deletions.
80 changes: 47 additions & 33 deletions src/Amara/Varcon/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,44 +38,58 @@ public function translate($string, $fromSpelling, $toSpelling, $questionable = s
$return = [];

foreach ($words as $w) {
$originalWord = $w;

$ucFirst = $this->isFirstLetterUppercase($originalWord);
$allCaps = $this->isWholeStringUppercase($originalWord);

if ($ucFirst || $allCaps) {
$w = strtolower($originalWord);
}

if (!isset($trans[$w])) {
$return[] = $originalWord;
continue;
}

$translationCount = count($trans[$w]);

if ($ucFirst) {
$trans[$w] = array_map('ucfirst', $trans[$w]);
}

if ($allCaps) {
$trans[$w] = array_map('strtoupper', $trans[$w]);
}

if (1 === $translationCount) {
$return[] = $trans[$w][0];
} elseif ($translationCount > 1 && $questionable == self::QUESTIONABLE_INCLUDE) {
$return[] = $trans[$w][0];
} elseif ($translationCount > 1 && $questionable == self::QUESTIONABLE_MARK) {
$return[] = '?'.implode('/', $trans[$w]).'?';
} else {
$return[] = $originalWord;
}
$return[] = $this->translateWord($w, $trans, $questionable);
}

return implode('', $return);
}

/**
* Translate the given word according to the given translations array
* Having this in a separate function can help track single-word translations
*
* @param string $word
* @param array $translations
* @param int $questionable
*
* @return string
*/
protected function translateWord($word, array $translations, $questionable = self::QUESTIONABLE_IGNORE)
{
$originalWord = $word;

$ucFirst = $this->isFirstLetterUppercase($originalWord);
$allCaps = $this->isWholeStringUppercase($originalWord);

if ($ucFirst || $allCaps) {
$word = strtolower($originalWord);
}

if (!isset($translations[$word])) {
return $originalWord;
}

$translationCount = count($translations[$word]);

if ($ucFirst) {
$translations[$word] = array_map('ucfirst', $translations[$word]);
}

if ($allCaps) {
$translations[$word] = array_map('strtoupper', $translations[$word]);
}

if (1 === $translationCount) {
return $translations[$word][0];
} elseif ($translationCount > 1 && $questionable === self::QUESTIONABLE_INCLUDE) {
return $translations[$word][0];
} elseif ($translationCount > 1 && $questionable === self::QUESTIONABLE_MARK) {
return '?'.implode('/', $translations[$word]).'?';
}

return $originalWord;
}

/**
* Is The First Letter Capital
*
Expand Down

0 comments on commit 762b605

Please sign in to comment.