public function updateEditForm(Form $form)
 {
     $locale = isset($_REQUEST['locale']) ? $_REQUEST['locale'] : $_REQUEST['Locale'];
     if (!empty($locale) && i18n::validate_locale($locale) && singleton('SiteConfig')->has_extension('Translatable') && (Translatable::get_allowed_locales() === null || in_array($locale, (array) Translatable::get_allowed_locales(), false)) && $locale != Translatable::get_current_locale()) {
         $orig = Translatable::get_current_locale();
         Translatable::set_current_locale($locale);
         $formAction = $form->FormAction();
         $form->setFormAction($formAction);
         Translatable::set_current_locale($orig);
     }
 }
 /**
  * Explicitly set the locales that should be translated.
  *
  * @example
  * <code>
  * // Set locales to en_US and fr_FR
  * TranslatableDataObject::set_locales(array('en_US', 'fr_FR'));
  * </code>
  *
  * Defaults to `null`. In this case, locales are being taken from
  * Translatable::get_allowed_locales or Translatable::get_existing_content_languages
  *
  * @param array|null $locales an array of locales or null
  * @deprecated 2.0 use YAML config `locales` instead
  */
 public static function set_locales($locales)
 {
     if (is_array($locales)) {
         $list = array();
         foreach ($locales as $locale) {
             if (i18n::validate_locale($locale)) {
                 $list[] = $locale;
             }
         }
         $list = array_unique($list);
         Config::inst()->update('TranslatableDataObject', 'locales', empty($list) ? null : $list);
     } else {
         Config::inst()->update('TranslatableDataObject', 'locales', null);
     }
 }
 public function testValidateLocale()
 {
     $this->assertTrue(i18n::validate_locale('en_US'), 'Known locale in underscore format is valid');
     $this->assertTrue(i18n::validate_locale('en-US'), 'Known locale in dash format is valid');
     $this->assertFalse(i18n::validate_locale('en'), 'Short lang format is not valid');
     $this->assertFalse(i18n::validate_locale('xx_XX'), 'Unknown locale in correct format is not valid');
     $this->assertFalse(i18n::validate_locale(''), 'Empty string is not valid');
 }
示例#4
0
 /**
  * Returns TRUE if the current record has a translation in this language.
  * Use {@link getTranslation()} to get the actual translated record from
  * the database.
  * 
  * @param string $locale
  * @return boolean
  */
 function hasTranslation($locale)
 {
     if ($locale && !i18n::validate_locale($locale)) {
         throw new InvalidArgumentException(sprintf('Invalid locale "%s"', $locale));
     }
     return $this->owner->Locale == $locale || array_search($locale, $this->getTranslatedLocales()) !== false;
 }
 /**
  * Is the prefix a valid locale? (@see alternateGetByLink)
  *
  * @param string $prefix
  * @return boolean
  */
 public static function valid_prefix($prefix)
 {
     // is this part of the prefix map if there is one?
     if (!empty(self::$locale_prefix_map)) {
         return in_array($prefix, self::$locale_prefix_map);
     }
     // is this part of the allowed languages, if there are any
     $alowed_locales = Translatable::get_allowed_locales();
     if (!empty($alowed_locales)) {
         return in_array($prefix, $alowed_locales);
     }
     // is this a valid locale anyway?
     return i18n::validate_locale($prefix);
 }
 /**
  * Explicitly set the locales that should be translated.
  * 
  * @example
  * <code>
  * // Set locales to en_US and fr_FR
  * TranslatableDataObject::set_locales(array('en_US', 'fr_FR'));
  * </code>
  * 
  * Defaults to `null`. In this case, locales are being taken from 
  * Translatable::get_allowed_locales or Translatable::get_existing_content_languages
  * 
  * @param array $locales an array of locales or null
  */
 public static function set_locales($locales)
 {
     if (is_array($locales)) {
         foreach ($locales as $locale) {
             if (i18n::validate_locale($locale)) {
                 if (!is_array(self::$locales)) {
                     self::$locales = array();
                 }
                 if (array_search($locale, self::$locales) === false) {
                     self::$locales[] = $locale;
                 }
             }
         }
     } else {
         self::$locales = null;
     }
 }
 /**
  * This acts the same as {@link Controller::handleRequest()}, but if an action cannot be found this will attempt to
  * fall over to a child controller in order to provide functionality for nested URLs.
  *
  * @param SS_HTTPRequest $request
  * @param DataModel $model
  * @return SS_HTTPResponse
  * @throws SS_HTTPResponse_Exception
  */
 public function handleRequest(SS_HTTPRequest $request, DataModel $model = null)
 {
     $child = null;
     $action = $request->param('Action');
     $this->setDataModel($model);
     // If nested URLs are enabled, and there is no action handler for the current request then attempt to pass
     // control to a child controller. This allows for the creation of chains of controllers which correspond to a
     // nested URL.
     if ($action && SiteTree::config()->nested_urls && !$this->hasAction($action)) {
         // See ModelAdController->getNestedController() for similar logic
         if (class_exists('Translatable')) {
             Translatable::disable_locale_filter();
         }
         // look for a page with this URLSegment
         $child = $this->model->SiteTree->filter(array('ParentID' => $this->ID, 'URLSegment' => rawurlencode($action)))->first();
         if (class_exists('Translatable')) {
             Translatable::enable_locale_filter();
         }
     }
     // we found a page with this URLSegment.
     if ($child) {
         $request->shiftAllParams();
         $request->shift();
         $response = ModelAsController::controller_for($child)->handleRequest($request, $model);
     } else {
         // If a specific locale is requested, and it doesn't match the page found by URLSegment,
         // look for a translation and redirect (see #5001). Only happens on the last child in
         // a potentially nested URL chain.
         if (class_exists('Translatable')) {
             $locale = $request->getVar('locale');
             if ($locale && i18n::validate_locale($locale) && $this->dataRecord && $this->dataRecord->Locale != $locale) {
                 $translation = $this->dataRecord->getTranslation($locale);
                 if ($translation) {
                     $response = new SS_HTTPResponse();
                     $response->redirect($translation->Link(), 301);
                     throw new SS_HTTPResponse_Exception($response);
                 }
             }
         }
         Director::set_current_page($this->data());
         try {
             $response = parent::handleRequest($request, $model);
             Director::set_current_page(null);
         } catch (SS_HTTPResponse_Exception $e) {
             $this->popCurrent();
             Director::set_current_page(null);
             throw $e;
         }
     }
     return $response;
 }