Exemplo n.º 1
0
 /**
  * Create a new LanguageDropdownField
  * @param string $name
  * @param string $title
  * @param array $dontInclude list of languages that won't be included
  * @param string $translatingClass Name of the class with translated instances where to look for used languages
  * @param string $list Indicates the source language list. Can be either Common-English, Common-Native  Locale
  */
 function __construct($name, $title, $dontInclude = array(), $translatingClass = 'SiteTree', $list = 'Common-English')
 {
     $usedlangs = array_diff(i18n::get_existing_content_languages($translatingClass), $dontInclude);
     // we accept in dontInclude both language codes and names, so another diff is required
     $usedlangs = array_diff($usedlangs, array_flip($dontInclude));
     if (isset($usedlangs[Translatable::default_lang()])) {
         unset($usedlangs[Translatable::default_lang()]);
     }
     if ('Common-English' == $list) {
         $languageList = i18n::get_common_languages();
     } else {
         if ('Common-Native' == $list) {
             $languageList = i18n::get_common_languages(true);
         } else {
             $languageList = i18n::get_locale_list();
         }
     }
     $alllangs = array_diff($languageList, (array) $usedlangs, $dontInclude);
     $alllangs = array_flip(array_diff(array_flip($alllangs), $dontInclude));
     if (isset($alllangs[Translatable::default_lang()])) {
         unset($alllangs[Translatable::default_lang()]);
     }
     asort($alllangs);
     if (count($usedlangs)) {
         asort($usedlangs);
         $labelAvail = _t('Form.LANGAVAIL', "Available languages");
         $labelOther = _t('Form.LANGAOTHER', "Other languages");
         parent::__construct($name, $title, array($labelAvail => $usedlangs, $labelOther => $alllangs), reset($usedlangs));
     } else {
         parent::__construct($name, $title, $alllangs);
     }
 }
 /**
  * Create a new LanguageDropdownField
  * @param string $name
  * @param string $title
  * @param array $excludeLocales List of locales that won't be included
  * @param string $translatingClass Name of the class with translated instances where to look for used languages
  * @param string $list Indicates the source language list. Can be either Common-English, Common-Native, Locale-English, Locale-Native
  */
 function __construct($name, $title, $excludeLocales = array(), $translatingClass = 'SiteTree', $list = 'Common-English', $instance = null)
 {
     $usedLocalesWithTitle = Translatable::get_existing_content_languages($translatingClass);
     $usedLocalesWithTitle = array_diff_key($usedLocalesWithTitle, $excludeLocales);
     if ('Common-English' == $list) {
         $allLocalesWithTitle = i18n::get_common_languages();
     } else {
         if ('Common-Native' == $list) {
             $allLocalesWithTitle = i18n::get_common_languages(true);
         } else {
             if ('Locale-English' == $list) {
                 $allLocalesWithTitle = i18n::get_common_locales();
             } else {
                 if ('Locale-Native' == $list) {
                     $allLocalesWithTitle = i18n::get_common_locales(true);
                 } else {
                     $allLocalesWithTitle = i18n::get_locale_list();
                 }
             }
         }
     }
     if (isset($allLocales[Translatable::default_locale()])) {
         unset($allLocales[Translatable::default_locale()]);
     }
     // Limit to allowed locales if defined
     // Check for canTranslate() if an $instance is given
     $allowedLocales = Translatable::get_allowed_locales();
     foreach ($allLocalesWithTitle as $locale => $localeTitle) {
         if ($allowedLocales && !in_array($locale, $allowedLocales) || $excludeLocales && in_array($locale, $excludeLocales) || $usedLocalesWithTitle && array_key_exists($locale, $usedLocalesWithTitle)) {
             unset($allLocalesWithTitle[$locale]);
         }
     }
     // instance specific permissions
     foreach ($allLocalesWithTitle as $locale => $localeTitle) {
         if ($instance && !$instance->canTranslate(null, $locale)) {
             unset($allLocalesWithTitle[$locale]);
         }
     }
     foreach ($usedLocalesWithTitle as $locale => $localeTitle) {
         if ($instance && !$instance->canTranslate(null, $locale)) {
             unset($usedLocalesWithTitle[$locale]);
         }
     }
     // Sort by title (array value)
     asort($allLocalesWithTitle);
     if (count($usedLocalesWithTitle)) {
         asort($usedLocalesWithTitle);
         $source = array(_t('Form.LANGAVAIL', "Available languages") => $usedLocalesWithTitle, _t('Form.LANGAOTHER', "Other languages") => $allLocalesWithTitle);
     } else {
         $source = $allLocalesWithTitle;
     }
     parent::__construct($name, $title, $source);
 }
Exemplo n.º 3
0
 /**
  * Set default language. Please set this value *before* creating
  * any database records (like pages), as this locale will be attached
  * to all new records.
  * 
  * @param $locale String
  */
 static function set_default_locale($locale)
 {
     if ($locale && !i18n::validate_locale($locale)) {
         throw new InvalidArgumentException(sprintf('Invalid locale "%s"', $locale));
     }
     $localeList = i18n::get_locale_list();
     if (isset($localeList[$locale])) {
         self::$default_locale = $locale;
     } else {
         user_error("Translatable::set_default_locale(): '{$locale}' is not a valid locale.", E_USER_WARNING);
     }
 }
Exemplo n.º 4
0
 /**
  * Set default language. Please set this value *before* creating
  * any database records (like pages), as this locale will be attached
  * to all new records.
  * 
  * @param $locale String
  */
 static function set_default_locale($locale)
 {
     $localeList = i18n::get_locale_list();
     if (isset($localeList[$locale])) {
         self::$default_locale = $locale;
     } else {
         user_error("Translatable::set_default_locale(): '{$locale}' is not a valid locale.", E_USER_WARNING);
     }
 }
 /**
  */
 function translate($data, $form)
 {
     increase_time_limit_to(600);
     // Simple validation
     if (!isset($data['From']) || !$data['From']) {
         echo "Specify origin language.";
         exit;
     }
     if (!isset($data['To']) || !$data['To']) {
         echo "Specify destination language.";
         exit;
     }
     $data['From'] = i18n::get_locale_from_lang($data['From']);
     $data['To'] = i18n::get_locale_from_lang($data['To']);
     $locales = array_keys(i18n::get_locale_list());
     if (!in_array($data['From'], $locales)) {
         echo "Origin language invalid.";
         exit;
     }
     if (!in_array($data['To'], $locales)) {
         echo "Destination language invalid.";
         exit;
     }
     if ($data['From'] == $data['To']) {
         echo "Origin and destination languages are the same.";
         exit;
     }
     $mangle = false;
     if (isset($data['Mangle']) && $data['Mangle']) {
         $mangle = true;
     }
     // We want to work off Draft, because that's what's visible in the CMS.
     Versioned::reading_stage('Stage');
     Translatable::set_current_locale($data['From']);
     $pages = SiteTree::get();
     // Remove the target locale's site.
     SiteTree::get()->filter('Locale', $data['To'])->removeAll();
     foreach ($pages as $page) {
         echo "Now processing {$page->ID}<br/>\n";
         // Start from the highest parent for each page - otherwise the parents are not translated properly.
         $stack = array();
         $parent = $page;
         while ($parent && $parent->ID) {
             array_unshift($stack, $parent);
             $parent = $parent->Parent();
         }
         // We will hit the same pages multiple times, but this is an easiest way to do it and it's just a tool.
         foreach ($stack as $stackPage) {
             $translation = $stackPage->getTranslation($data['To']);
             if ($translation && $translation->ID) {
                 // Skip pages that have already been translated.
                 echo "{$stackPage->ID}: exists, skipping.<br/>\n";
             } else {
                 $translation = $stackPage->createTranslation($data['To']);
                 if ($mangle) {
                     $this->fakeTranslation($translation);
                 }
                 $translation->write();
                 echo "{$stackPage->ID}: translated.<br/>\n";
             }
         }
     }
 }
 /**
  * Creates missing locale files for all modules.
  *
  * @return void
  *
  * @author Sascha Koehler <*****@*****.**>
  * @since 16.05.2012
  */
 public static function createMissingLocales()
 {
     global $lang;
     $base = Director::baseFolder();
     $topLevel = scandir($base);
     $template = file_get_contents($base . '/silvercart/code_templates/locale/SilvercartLocaleTemplate.php');
     $locales = i18n::get_locale_list();
     foreach ($locales as $locale => $localeName) {
         if ($locale == 'en_US') {
             continue;
         }
         $moduleIncludeStr = array();
         foreach ($topLevel as $module) {
             if ($module[0] == '.' || $module == 'silvercart-cache') {
                 continue;
             }
             $localeFileName = "{$base}/silvercart-cache/lang/{$locale}.php";
             if (is_dir("{$base}/{$module}") && file_exists("{$base}/{$module}/_config.php") && file_exists("{$base}/{$module}/lang/en_US.php")) {
                 $moduleIncludeStr[] = "i18n::include_locale_file('" . $module . "', 'en_US');";
             }
         }
         $localeContent = str_replace(array('__MODULE_INCLUDES__', '__LOCALE__'), array(implode(PHP_EOL, $moduleIncludeStr), $locale), $template);
         file_put_contents($localeFileName, $localeContent);
     }
 }