diff --git a/src/Amara/Varcon/Translator.php b/src/Amara/Varcon/Translator.php index 70aadf7..a71a6de 100644 --- a/src/Amara/Varcon/Translator.php +++ b/src/Amara/Varcon/Translator.php @@ -49,13 +49,30 @@ 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) { @@ -63,10 +80,34 @@ public function translate($string, $fromSpelling, $toSpelling, $questionable = s } 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; + } } diff --git a/tests/Amara/Varcon/TranslatorTest.php b/tests/Amara/Varcon/TranslatorTest.php index 404f2bc..84f0953 100644 --- a/tests/Amara/Varcon/TranslatorTest.php +++ b/tests/Amara/Varcon/TranslatorTest.php @@ -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', @@ -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'], @@ -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',