[BUGFIX] EXT:documentation does not support translated manuals 09/24109/3
authorXavier Perseguers <xavier@typo3.org>
Fri, 27 Sep 2013 13:00:54 +0000 (15:00 +0200)
committerChristian Kuhn <lolli@schwarzbu.ch>
Sun, 6 Oct 2013 18:51:37 +0000 (20:51 +0200)
Some extensions come with a translated manual and it should be used
if a match with the user's preferred language is found.

Fixes: #52332
Releases: 6.2
Change-Id: I84a87840db99fe2704c90bce8dc02f5bc39db4c1
Reviewed-on: https://review.typo3.org/24109
Reviewed-by: Eric Chavaillaz
Tested-by: Eric Chavaillaz
Tested-by: Philipp Gampe
Reviewed-by: Christian Kuhn
Tested-by: Christian Kuhn
typo3/sysext/documentation/Classes/Controller/DocumentController.php
typo3/sysext/documentation/Classes/Domain/Repository/DocumentRepository.php
typo3/sysext/documentation/Classes/Service/DocumentationService.php
typo3/sysext/documentation/README.rst

index 776672d..fcbb619 100644 (file)
@@ -131,8 +131,9 @@ class DocumentController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionControl
                        $this->redirect('list');
                }
 
+               $language = $this->languageUtility->getDocumentationLanguage();
                try {
-                       $result = $this->documentationService->fetchNearestDocument($url, $key, $version ?: 'latest');
+                       $result = $this->documentationService->fetchNearestDocument($url, $key, $version ?: 'latest', $language);
 
                        if ($result) {
                                $this->controllerContext->getFlashMessageQueue()->enqueue(
index dd90ffd..057401f 100644 (file)
@@ -70,9 +70,16 @@ class DocumentRepository {
                /** @var $locales \TYPO3\CMS\Core\Localization\Locales */
                $locales = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Localization\\Locales');
                // Language is found. Configure it:
-               if (in_array($language, $locales->getLocales())) {
+               $shortLanguage = $language;
+               if (!in_array($shortLanguage, $locales->getLocales()) && strpos($shortLanguage, '_') !== FALSE) {
+                       list($shortLanguage, $_) = explode('_', $shortLanguage);
+               }
+               if (in_array($shortLanguage, $locales->getLocales())) {
                        $languageDependencies[] = $language;
-                       foreach ($locales->getLocaleDependencies($language) as $languageDependency) {
+                       if ($language !== $shortLanguage) {
+                               $languageDependencies[] = $shortLanguage;
+                       }
+                       foreach ($locales->getLocaleDependencies($shortLanguage) as $languageDependency) {
                                $languageDependencies[] = $languageDependency;
                        }
                }
@@ -88,9 +95,22 @@ class DocumentRepository {
                        $translations = $document->getTranslations();
                        foreach ($translations as $translation) {
                                $languageIndex = array_search($translation->getLanguage(), $languageDependencies);
-                               if ($languageIndex !== FALSE && $languageIndex < $highestPriorityLanguageIndex) {
-                                       $selectedTranslation = $translation;
-                                       $highestPriorityLanguageIndex = $languageIndex;
+                               if ($languageIndex !== FALSE) {
+                                       if ($languageIndex < $highestPriorityLanguageIndex) {
+                                               $selectedTranslation = $translation;
+                                               $highestPriorityLanguageIndex = $languageIndex;
+                                       }
+                               } else {
+                                       // No exact translation found, perhaps another locale would fit as well. E.g., when requesting
+                                       // a documentation as fr_CA but only fr_FR exists
+                                       if (strpos($translation->getLanguage(), '_') !== FALSE) {
+                                               list($translationLanguage, $_) = explode('_', $translation->getLanguage());
+                                               $languageIndex = array_search($translationLanguage, $languageDependencies);
+                                               if ($languageIndex !== FALSE && $languageIndex < $highestPriorityLanguageIndex) {
+                                                       $selectedTranslation = $translation;
+                                                       $highestPriorityLanguageIndex = $languageIndex;
+                                               }
+                                       }
                                }
                        }
 
index d5b050d..f0c0c18 100644 (file)
@@ -105,9 +105,12 @@ class DocumentationService {
         * @return boolean TRUE if fetch succeeded, otherwise FALSE
         */
        public function fetchNearestDocument($url, $key, $version = 'latest', $language = 'default') {
+               // In case we could not find a working combination
+               $success = FALSE;
+
                $packages = $this->getAvailablePackages($url);
                if (count($packages) == 0) {
-                       return FALSE;
+                       return $success;
                }
 
                $languages = array($language);
@@ -117,24 +120,37 @@ class DocumentationService {
                foreach ($languages as $language) {
                        // Step 1)
                        if (isset($packages[$version][$language])) {
-                               return $this->fetchDocument($url, $key, $version, $language);
+                               $success |= $this->fetchDocument($url, $key, $version, $language);
+                               // Fetch next language
+                               continue;
+                       } else {
+                               foreach ($packages[$version] as $locale => $_) {
+                                       if (\TYPO3\CMS\Core\Utility\GeneralUtility::isFirstPartOfStr($locale, $language)) {
+                                               $success |= $this->fetchDocument($url, $key, $version, $locale);
+                                               // Fetch next language (jump current foreach up to the loop of $languages)
+                                               continue 2;
+                                       }
+                               }
                        }
                        // Step 2)
                        if (preg_match('/^(\d+\.\d+)\.\d+$/', $version, $matches)) {
                                // Instead of a 3-digit version, try to get it on 2 digits
                                $shortVersion = $matches[1];
                                if (isset($packages[$shortVersion][$language])) {
-                                       return $this->fetchDocument($url, $key, $shortVersion, $language);
+                                       $success |= $this->fetchDocument($url, $key, $shortVersion, $language);
+                                       // Fetch next language
+                                       continue;
                                }
                        }
                        // Step 3)
                        if ($version !== 'latest' && isset($packages['latest'][$language])) {
-                               return $this->fetchDocument($url, $key, 'latest', $language);
+                               $success |= $this->fetchDocument($url, $key, 'latest', $language);
+                               // Fetch next language
+                               continue;
                        }
                }
 
-               // Could not find a working combination
-               return FALSE;
+               return $success;
        }
 
        /**
@@ -151,7 +167,8 @@ class DocumentationService {
                $url = rtrim($url, '/') . '/';
 
                $packagePrefix = substr($key, strrpos($key, '.') + 1);
-               $packageName = sprintf('%s-%s-%s.zip', $packagePrefix, $version, $language);
+               $languageSegment = str_replace('_', '-', strtolower($language));
+               $packageName = sprintf('%s-%s-%s.zip', $packagePrefix, $version, $languageSegment);
                $packageUrl = $url . 'packages/' . $packageName;
                $absolutePathToZipFile = GeneralUtility::getFileAbsFileName('typo3temp' . DIRECTORY_SEPARATOR . $packageName);
 
index 7282b8b..2f67b9c 100644 (file)
@@ -35,10 +35,12 @@ To be listed, documents should be stored within ``typo3conf/Documentation/<docum
        Extensions use the documentation key ``typo3cms.extensions.<extension-key>``.
 
 ``language``
-       Either "default" (for English) or some (generally) two-letter ISO code identifying a translation of your document.
+       Either "default" (for English) or a proper locale identifying your translated documentation. E.g.,
+       ``fr_FR``, ``fr_CA``, ``de_DE`` ...
 
 ``format``
-       Either ``html`` or ``pdf``. Additional formats may be supported by 3rd party extensions (such as EXT:sphinx).
+       Either ``html`` or ``pdf``. Additional formats may be supported by 3rd party extensions
+       (such as `EXT:sphinx <http://typo3.org/extensions/repository/view/sphinx>`_).
 
 
 Registering Custom Documents
@@ -82,4 +84,3 @@ mod.help_DocumentationDocumentation.documents.hide
 
 mod.help_DocumentationDocumentation.documents.show
   Comma-separated list of keys of documentation that should be shown to the user (others are implicitly hidden).
-