From fd5a9f4efd6eff419c130c5fc7e4c9f547aa9338 Mon Sep 17 00:00:00 2001 From: Derek Roth Date: Tue, 10 Nov 2015 16:37:56 +0100 Subject: [PATCH 1/2] Fallback language_COUNTRY locales to language only --- .../Translatable/TranslatableMethods.php | 21 +++++++++- .../ORM/TranslatableTest.php | 39 +++++++++++++++++++ 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/src/Model/Translatable/TranslatableMethods.php b/src/Model/Translatable/TranslatableMethods.php index 13870108..a0a34804 100644 --- a/src/Model/Translatable/TranslatableMethods.php +++ b/src/Model/Translatable/TranslatableMethods.php @@ -103,8 +103,16 @@ protected function doTranslate($locale = null, $fallbackToDefault = true) return $translation; } - if ($fallbackToDefault && $defaultTranslation = $this->findTranslationByLocale($this->getDefaultLocale(), false)) { - return $defaultTranslation; + if ($fallbackToDefault) { + if ($fallbackLocale = $this->computeFallbackLocale($locale)) { + if ($translation = $this->findTranslationByLocale($fallbackLocale)) { + return $translation; + } + } + + if ($defaultTranslation = $this->findTranslationByLocale($this->getDefaultLocale(), false)) { + return $defaultTranslation; + } } $class = self::getTranslationEntityClass(); @@ -208,4 +216,13 @@ protected function findTranslationByLocale($locale, $withNewTranslations = true) return $this->getNewTranslations()->get($locale); } } + + protected function computeFallbackLocale($locale) + { + if (strrchr($locale, '_') !== false) { + return substr($locale, 0, -strlen(strrchr($locale, '_'))); + } + + return false; + } } diff --git a/tests/Knp/DoctrineBehaviors/ORM/TranslatableTest.php b/tests/Knp/DoctrineBehaviors/ORM/TranslatableTest.php index 449f18b2..542067ab 100644 --- a/tests/Knp/DoctrineBehaviors/ORM/TranslatableTest.php +++ b/tests/Knp/DoctrineBehaviors/ORM/TranslatableTest.php @@ -83,6 +83,45 @@ public function should_persist_translations() ); } + /** + * @test + */ + public function should_fallback_country_locale_to_language_only_translation() + { + $em = $this->getEntityManager(); + + $entity = new \BehaviorFixtures\ORM\TranslatableEntity(); + $entity->translate('en', false)->setTitle('plastic bag'); + $entity->translate('fr', false)->setTitle('sac plastique'); + $entity->translate('fr_CH', false)->setTitle('cornet'); + $entity->mergeNewTranslations(); + + $em->persist($entity); + $em->flush(); + $id = $entity->getId(); + $em->clear(); + + $entity = $em + ->getRepository('BehaviorFixtures\ORM\TranslatableEntity') + ->find($id) + ; + + $this->assertEquals( + 'plastic bag', + $entity->translate('de')->getTitle() + ); + + $this->assertEquals( + 'sac plastique', + $entity->translate('fr_FR')->getTitle() + ); + + $this->assertEquals( + 'cornet', + $entity->translate('fr_CH')->getTitle() + ); + } + /** * @test */ From b7326551823669b15f5598290953924906f2583c Mon Sep 17 00:00:00 2001 From: Derek Roth Date: Thu, 3 Dec 2015 14:40:36 +0100 Subject: [PATCH 2/2] Grouped two ifs --- src/Model/Translatable/TranslatableMethods.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/Model/Translatable/TranslatableMethods.php b/src/Model/Translatable/TranslatableMethods.php index a0a34804..f83d0dc0 100644 --- a/src/Model/Translatable/TranslatableMethods.php +++ b/src/Model/Translatable/TranslatableMethods.php @@ -104,10 +104,9 @@ protected function doTranslate($locale = null, $fallbackToDefault = true) } if ($fallbackToDefault) { - if ($fallbackLocale = $this->computeFallbackLocale($locale)) { - if ($translation = $this->findTranslationByLocale($fallbackLocale)) { - return $translation; - } + if (($fallbackLocale = $this->computeFallbackLocale($locale)) + && ($translation = $this->findTranslationByLocale($fallbackLocale))) { + return $translation; } if ($defaultTranslation = $this->findTranslationByLocale($this->getDefaultLocale(), false)) {