PageRenderTime 27ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/loco-translate/src/admin/bundle/LocaleController.php

https://gitlab.com/campus-academy/krowkaramel
PHP | 162 lines | 109 code | 21 blank | 32 comment | 10 complexity | 25dc71caacf01cd44e105ad932094c06 MD5 | raw file
  1. <?php
  2. /**
  3. * Pseudo-bundle view, lists all files available in a single locale
  4. */
  5. class Loco_admin_bundle_LocaleController extends Loco_mvc_AdminController {
  6. /**
  7. * @var Loco_Locale
  8. */
  9. private $locale;
  10. /**
  11. * {@inheritdoc}
  12. */
  13. public function init(){
  14. parent::init();
  15. $tag = $this->get('locale');
  16. $locale = Loco_Locale::parse($tag);
  17. if( $locale->isValid() ){
  18. $api = new Loco_api_WordPressTranslations;
  19. $this->set('title', $locale->ensureName($api) );
  20. $this->locale = $locale;
  21. $this->enqueueStyle('locale')->enqueueStyle('fileinfo');
  22. }
  23. }
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public function getHelpTabs(){
  28. return [
  29. __('Overview','default') => $this->viewSnippet('tab-locale-view'),
  30. ];
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function render(){
  36. // locale already parsed during init (for page title)
  37. $locale = $this->locale;
  38. if( ! $locale || ! $locale->isValid() ){
  39. throw new Loco_error_Exception('Invalid locale argument');
  40. }
  41. // language may not be "installed" but we still want to inspect available files
  42. $api = new Loco_api_WordPressTranslations;
  43. $installed = $api->isInstalled($locale);
  44. $tag = (string) $locale;
  45. $package = new Loco_package_Locale( $locale );
  46. // Get PO files for this locale
  47. $files = $package->findLocaleFiles();
  48. $translations = [];
  49. $modified = 0;
  50. $npofiles = 0;
  51. $nfiles = 0;
  52. // source locale means we want to see POT instead of translations
  53. if( 'en_US' === $tag ){
  54. $files = $package->findTemplateFiles()->augment($files);
  55. }
  56. /* @var Loco_fs_File $file */
  57. foreach( $files as $file ){
  58. $nfiles++;
  59. if( 'pot' !== $file->extension() ){
  60. $npofiles++;
  61. }
  62. $modified = max( $modified, $file->modified() );
  63. $project = $package->getProject($file);
  64. // do similarly to Loco_admin_bundle_ViewController::createFileParams
  65. $meta = Loco_gettext_Metadata::load($file);
  66. $dir = new Loco_fs_LocaleDirectory( $file->dirname() );
  67. // arguments for deep link into project
  68. $slug = $project->getSlug();
  69. $domain = $project->getDomain()->getName();
  70. $bundle = $project->getBundle();
  71. $type = strtolower( $bundle->getType() );
  72. $args = [
  73. // 'locale' => $tag,
  74. 'bundle' => $bundle->getHandle(),
  75. 'domain' => $project->getId(),
  76. 'path' => $meta->getPath(false),
  77. ];
  78. // append data required for PO table row, except use bundle data instead of locale data
  79. $translations[$type][] = new Loco_mvc_ViewParams( [
  80. // bundle info
  81. 'title' => $project->getName(),
  82. 'domain' => $domain,
  83. 'short' => ! $slug || $project->isDomainDefault() ? $domain : $domain.'→'.$slug,
  84. // file info
  85. 'meta' => $meta,
  86. 'name' => $file->basename(),
  87. 'time' => $file->modified(),
  88. 'type' => strtoupper( $file->extension() ),
  89. 'todo' => $meta->countIncomplete(),
  90. 'total' => $meta->getTotal(),
  91. // author / system / custom / other
  92. 'store' => $dir->getTypeLabel( $dir->getTypeId() ),
  93. // links
  94. 'view' => Loco_mvc_AdminRouter::generate( $type.'-file-view', $args ),
  95. 'info' => Loco_mvc_AdminRouter::generate( $type.'-file-info', $args ),
  96. 'edit' => Loco_mvc_AdminRouter::generate( $type.'-file-edit', $args ),
  97. 'move' => Loco_mvc_AdminRouter::generate( $type.'-file-move', $args ),
  98. 'delete' => Loco_mvc_AdminRouter::generate( $type.'-file-delete', $args ),
  99. 'copy' => Loco_mvc_AdminRouter::generate( $type.'-msginit', $args ),
  100. ] );
  101. }
  102. $title = __( 'Installed languages', 'loco-translate' );
  103. $breadcrumb = new Loco_admin_Navigation;
  104. $breadcrumb->add( $title, Loco_mvc_AdminRouter::generate('lang') );
  105. //$breadcrumb->add( $locale->getName() );
  106. $breadcrumb->add( $tag );
  107. // It's unlikely that an "installed" language would have no files, but could happen if only MO on disk
  108. if( 0 === $nfiles ){
  109. return $this->view('admin/errors/no-locale', compact('breadcrumb','locale') );
  110. }
  111. // files may be available for language even if not installed (i.e. no core files on disk)
  112. if( ! $installed || ! isset($translations['core']) && 'en_US' !== $tag ){
  113. Loco_error_AdminNotices::warn( __('No core translation files are installed for this language','loco-translate') )
  114. ->addLink('https://codex.wordpress.org/Installing_WordPress_in_Your_Language', __('Documentation','loco-translate') );
  115. }
  116. // Translated type labels and "See all <type>" links
  117. $types = [
  118. 'core' => new Loco_mvc_ViewParams( [
  119. 'name' => __('WordPress Core','loco-translate'),
  120. 'text' => __('See all core translations','loco-translate'),
  121. 'href' => Loco_mvc_AdminRouter::generate('core')
  122. ] ),
  123. 'theme' => new Loco_mvc_ViewParams( [
  124. 'name' => __('Themes','loco-translate'),
  125. 'text' => __('See all themes','loco-translate'),
  126. 'href' => Loco_mvc_AdminRouter::generate('theme')
  127. ] ),
  128. 'plugin' => new Loco_mvc_ViewParams( [
  129. 'name' => __('Plugins','loco-translate'),
  130. 'text' => __('See all plugins','loco-translate'),
  131. 'href' => Loco_mvc_AdminRouter::generate('plugin')
  132. ] ),
  133. ];
  134. $this->set( 'locale', new Loco_mvc_ViewParams( [
  135. 'code' => $tag,
  136. 'name' => $locale->getName(),
  137. 'attr' => 'class="'.$locale->getIcon().'" lang="'.$locale->lang.'"',
  138. ] ) );
  139. return $this->view( 'admin/bundle/locale', compact('breadcrumb','translations','types','npofiles','modified') );
  140. }
  141. }