Skip to content

Commit

Permalink
Add support for words starting with a capital, as well as ALL-CAPS words
Browse files Browse the repository at this point in the history
  • Loading branch information
loranmutafov committed Dec 19, 2016
1 parent fef4f6f commit f91a0b9
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 18 deletions.
45 changes: 43 additions & 2 deletions src/Amara/Varcon/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,65 @@ 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[] = $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[] = $w;
$return[] = $originalWord;
}
}

return implode('', $return);
}

/**
* Is The First Letter Capital
*
* @param string $string
*
* @return bool
*/
private function isFirstLetterUppercase($string)
{
return ucfirst(strtolower($string)) === $string;
}

/**
* IS EVERYTHING IN CAPITAL LETTERS
*
* @param string $string
*
* @return bool
*/
private function isWholeStringUppercase($string)
{
return strtoupper($string) === $string;
}
}
38 changes: 22 additions & 16 deletions tests/Amara/Varcon/TranslatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,37 @@ public function translateDataProvider()
{
return [
[
// Assert words starting with a capital are translated and their capitalisation is preserved
$questionable = Translator::QUESTIONABLE_IGNORE,
$british = 'My pyjama\'s colour is not as greyish as it looks',
$american = 'My pajama\'s color is not as grayish as it looks',
$canadian = 'My pyjama\'s colour is not as greyish as it looks',
$australian = 'My pyjama\'s colour is not as greyish as it looks',
$british_z = 'My pyjama\'s colour is not as greyish as it looks',
$british = 'My Pyjama\'s Colour is not as greyish as it looks',
$american = 'My Pajama\'s Color is not as grayish as it looks',
$canadian = 'My Pyjama\'s Colour is not as greyish as it looks',
$australian = 'My Pyjama\'s Colour is not as greyish as it looks',
$british_z = 'My Pyjama\'s Colour is not as greyish as it looks',
$variation = null,
],
[
// Assert all-caps strings are being translated and kept all-caps
$questionable = Translator::QUESTIONABLE_IGNORE,
$british = 'The 50x50centimetres-cabinet is finally finalised',
$american = 'The 50x50centimeters-cabinet is finally finalized',
$canadian = 'The 50x50centimetres-cabinet is finally finalized',
$australian = 'The 50x50centimetres-cabinet is finally finalised',
$british_z = 'The 50x50centimetres-cabinet is finally finalized',
$british = 'The 50x50centimetres-cabinet is finally FINALISED',
$american = 'The 50x50centimeters-cabinet is finally FINALIZED',
$canadian = 'The 50x50centimetres-cabinet is finally FINALIZED',
$australian = 'The 50x50centimetres-cabinet is finally FINALISED',
$british_z = 'The 50x50centimetres-cabinet is finally FINALIZED',
$variation = null,
],
[
// Assert weird capitalisation is not translated
$questionable = Translator::QUESTIONABLE_IGNORE,
$british = 'No uncommon words here',
$american = 'No uncommon words here',
$canadian = 'No uncommon words here',
$australian = 'No uncommon words here',
$british_z = 'No uncommon words here',
$variation = 'No uncommon words here',
$british = 'The quick GraY cat is gREy',
$american = 'The quick GraY cat is gREy',
$canadian = 'The quick GraY cat is gREy',
$australian = 'The quick GraY cat is gREy',
$british_z = 'The quick GraY cat is gREy',
$variation = 'The quick GraY cat is gREy',
],
[
// Assert questionable translations are used when the include flag is set
$questionable = Translator::QUESTIONABLE_INCLUDE,
$british = ['One metre', 'One meter'],
$american = 'One meter',
Expand All @@ -65,6 +69,7 @@ public function translateDataProvider()
$variation = null,
],
[
// Assert questionable translations are used when the include flag is set
$questionable = Translator::QUESTIONABLE_INCLUDE,
$british = ['adviser', 'advisor'],
$american = ['adviser', 'advisor'],
Expand All @@ -74,6 +79,7 @@ public function translateDataProvider()
$variation = ['adviser', 'advisor'],
],
[
// Assert questionable translations are marked when the mark flag is set
$questionable = Translator::QUESTIONABLE_MARK,
$british = ['One metre high', 'One ?meter/metre? high'],
$american = 'One meter high',
Expand Down

0 comments on commit f91a0b9

Please sign in to comment.