Beispiel #1
0
 /**
  * Admin tools page render call
  */
 public static function render_page_tools()
 {
     self::check_capability();
     do {
         try {
             // libs required for all manage translation pages
             loco_require('loco-locales', 'loco-packages');
             // most actions except root listing define a single package by name and type
             $package = null;
             if (isset($_GET['name']) && isset($_GET['type'])) {
                 $package = LocoPackage::get($_GET['name'], $_GET['type']);
             }
             // Extract messages if 'xgettext' is in query string
             //
             if (isset($_GET['xgettext'])) {
                 $domain = $_GET['xgettext'];
                 if ($pot_path = $package->get_pot($domain)) {
                     throw new Exception('POT already exists at ' . $pot_path);
                 }
                 // Establish best/intended location for new POT file
                 $dir = $package->lang_dir($domain);
                 $pot_path = $dir . '/' . $domain . '.pot';
                 $export = self::xgettext($package, $dir);
                 self::render_poeditor($package, $pot_path, $export);
                 break;
             }
             // Initialize a new PO file if 'msginit' is in query string
             //
             if (isset($_GET['msginit'])) {
                 $domain = $_GET['msginit'];
                 $force_global = isset($_GET['gforce']) ? (bool) $_GET['gforce'] : null;
                 // handle PO file creation if locale is set
                 if (isset($_GET['custom-locale'])) {
                     try {
                         $locale = $_GET['custom-locale'] or $locale = $_GET['common-locale'];
                         $po_path = self::msginit($package, $domain, $locale, $export, $head, $force_global);
                         if ($po_path) {
                             self::render_poeditor($package, $po_path, $export, $head);
                             break;
                         }
                     } catch (Exception $Ex) {
                         // fall through to msginit screen with error
                         self::error($Ex->getMessage());
                     }
                 }
                 // else do a dry run to pre-empt failures and allow manual alteration of target path
                 $path = self::msginit($package, $domain, 'zz_ZZ', $export, $head, $force_global);
                 // get alternative location options
                 $pdir = $package->lang_dir($domain, true);
                 $gdir = $package->global_lang_dir();
                 $pdir_ok = is_writeable($pdir);
                 $gdir_ok = is_writeable($gdir);
                 $is_global = $package->is_global_path($path);
                 // warn about unwriteable locations?
                 // render msginit start screen
                 $title = Loco::__('New PO file');
                 $locales = LocoLocale::get_names();
                 Loco::enqueue_scripts('build/admin-common', 'build/admin-poinit');
                 Loco::render('admin-poinit', compact('package', 'domain', 'title', 'locales', 'path', 'pdir', 'gdir', 'pdir_ok', 'gdir_ok', 'is_global'));
                 break;
             }
             // Render existing file in editor if 'poedit' contains a valid file path relative to content directory
             //
             if (isset($_GET['poedit']) && ($po_path = self::resolve_path($_GET['poedit']))) {
                 $export = self::parse_po_with_headers($po_path, $head);
                 // support incorrect usage of PO files as templates
                 if (isset($_GET['pot']) && !self::is_pot($po_path)) {
                     $po_path = dirname($po_path) . '/' . $_GET['pot'] . '.pot';
                     self::warning(sprintf(Loco::__('PO file used as template. This will be renamed to %s on first save'), basename($po_path)));
                 }
                 self::render_poeditor($package, $po_path, $export, $head);
                 break;
             }
             // Show filesystem check if 'fscheck' in query
             //
             if (isset($_GET['fscheck'])) {
                 $args = $package->meta() + compact('package');
                 Loco::enqueue_scripts('build/admin-common');
                 Loco::render('admin-fscheck', $args);
                 break;
             }
         } catch (Exception $Ex) {
             self::error($Ex->getMessage());
         }
         // default screen renders root page with available themes and plugins to translate
         // @var WP_Theme $theme
         $themes = array();
         foreach (wp_get_themes(array('allowed' => true)) as $name => $theme) {
             $package = LocoPackage::get($name, 'theme') and $name = $package->get_name();
             $themes[$name] = $package;
         }
         // @var array $plugin
         $plugins = array();
         foreach (get_plugins() as $plugin_file => $plugin) {
             $package = LocoPackage::get($plugin_file, 'plugin') and $plugins[] = $package;
         }
         // @var array $core
         $core = array();
         $conf = Loco::config();
         if (!empty($conf['enable_core'])) {
             foreach (LocoPackage::get_core_packages() as $package) {
                 // if package has no PO or POT we skip it because core packages have no source
                 if ($package->get_po() || $package->get_pot()) {
                     $core[] = $package;
                 }
             }
         }
         // order most active packges first in each set
         $args = array('themes' => LocoPackage::sort_modified($themes), 'plugins' => LocoPackage::sort_modified($plugins), 'core' => LocoPackage::sort_modified($core));
         // upgrade notice
         if ($updates = get_site_transient('update_plugins')) {
             $key = Loco::NS . '/loco.php';
             if (isset($updates->checked[$key]) && isset($updates->response[$key])) {
                 $old = $updates->checked[$key];
                 $new = $updates->response[$key]->new_version;
                 if (1 === version_compare($new, $old)) {
                     // current version is lower than latest
                     $args['update'] = $new;
                 }
             }
         }
         Loco::enqueue_scripts('build/admin-common');
         Loco::render('admin-root', $args);
     } while (false);
 }
Beispiel #2
0
 /**
  * Get package errors, or things that may cause problems displaying translations
  */
 public function get_author_warnings()
 {
     $warn = array();
     $type = $this->get_type();
     if ('core' !== $type) {
         $camelType = strtoupper($type[0]) . substr($type, 1);
         // check package declares Text Domain
         $domain = $this->get_original('TextDomain');
         if (!$domain) {
             $domain = $this->get_domain();
             $warn[] = sprintf(Loco::__('%s does not declare a "Text Domain"'), $camelType) . ' .. ' . sprintf(Loco::__('Loco has guessed "%s"'), $domain);
         }
         // check package declares "Domain Path"
         $path = $this->get_original('Domain Path');
         if (!$domain) {
             $warn[] = sprintf(Loco::__('%s does not declare a "Domain Path"'), $camelType) . ' .. ' . sprintf(Loco::__('Loco has guessed "%s"'), $this->domainpath);
         }
         // check POT exists and looks correct
         $path = $this->get_pot($domain);
         if (!$path) {
             $warn[] = sprintf(Loco::__('%s has no POT file. Create one at "%s/%s.pot" if you need one.'), $camelType, $this->domainpath, $domain);
         } else {
             if ($domain . '.pot' !== basename($path)) {
                 $warn[] = sprintf(Loco::__('%s has a strange POT file name (%s). A better name would be "%s.pot"'), $camelType, basename($path), $domain);
             }
         }
         // TODO check references to other domains in xgettext
     }
     // Check if any locale codes are not an official WordPress languages
     $meta = $this->meta();
     foreach ($meta['po'] as $po_data) {
         $wplang = $po_data['locale']->get_code() or $wplang = $po_data['locale']->get_name();
         if (!LocoLocale::is_valid_wordpress($wplang)) {
             $warn[] = sprintf(Loco::__('%s is not an official WordPress language'), $wplang);
         }
     }
     return $warn;
 }
Beispiel #3
0
 public function testPluralFormArrays()
 {
     // English - two forms
     $locale = LocoLocale::init('en', 'GB');
     extract($locale->export());
     $this->assertCount(2, $plurals);
     // Chinese - one form
     $locale = LocoLocale::init('zh', 'TW');
     extract($locale->export());
     $this->assertCount(1, $plurals);
     // Polish - three forms
     $locale = LocoLocale::init('pl', 'PL');
     extract($locale->export());
     $this->assertCount(3, $plurals);
     // Arabic - six forms
     $locale = LocoLocale::init('ar', 'AE');
     extract($locale->export());
     $this->assertCount(6, $plurals);
 }
Beispiel #4
0
 public function equal_to(LocoLocale $locale)
 {
     return $this->get_code() === $locale->get_code();
 }
 /**
  * Build name of PO file for given or default domain
  */
 public function create_po_path(LocoLocale $locale, $domain = '', $force_global = null)
 {
     if (!$domain) {
         $domain = $this->get_domain();
     }
     // get best directory
     if (is_null($force_global)) {
         $dir = $this->lang_dir($domain);
         $force_global = $this->is_global_path($dir);
     } else {
         if ($force_global) {
             $dir = $this->global_lang_dir();
         } else {
             $dir = $this->lang_dir($domain, true);
         }
     }
     $name = $locale->get_code() . '.po';
     // only prefix with text domain for plugins and files in global lang directory
     if ('plugin' === $this->get_type() || $force_global) {
         $prefix = $domain . '-';
     } else {
         $prefix = '';
     }
     // if PO files exist, copy their naming format and use location if writable
     if (is_null($force_global) && !empty($this->po[$domain])) {
         foreach ($this->po[$domain] as $code => $path) {
             $info = pathinfo($path);
             $prefix = str_replace($code . '.' . $info['extension'], '', $info['basename']);
             if (is_writable($info['dirname'])) {
                 $dir = $info['dirname'];
                 break;
             }
         }
     }
     return $dir . '/' . $prefix . $name;
 }