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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
/**
 * Pseudo-bundle view, lists all files available in a single locale
 */
class Loco_admin_bundle_LocaleController extends Loco_mvc_AdminController {
    
    
/**
     * @var Loco_Locale
     */
    
private $locale;


    
/**
     * {@inheritdoc}
     */
    
public function init(){
        
parent::init();
        
$tag $this->get('locale');
        
$locale Loco_Locale::parse($tag);
        if( 
$locale->isValid() ){
            
$api = new Loco_api_WordPressTranslations;
            
$this->set('title'$locale->ensureName($api) );
            
$this->locale $locale;
            
$this->enqueueStyle('locale')->enqueueStyle('fileinfo');
        }
    }


    
/**
     * {@inheritdoc}
     */
    
public function getHelpTabs(){
        return  [
            
__('Overview','loco-translate') => $this->viewSnippet('tab-locale-view'),
        ];
    }


    
/**
     * {@inheritdoc}
     */
    
public function render(){

        
// locale already parsed during init (for page title)
        
$locale $this->locale;
        if( ! 
$locale || ! $locale->isValid() ){
            throw new 
Loco_error_Exception('Invalid locale argument');
        }

        
// language may not be "installed" but we still want to inspect available files
        
$api = new Loco_api_WordPressTranslations;
        
$installed $api->isInstalled($locale);
        
        
$tag = (string) $locale;
        
$package = new Loco_package_Locale$locale );
        
        
// search for base language, unless it's a separate, installed language
        
if( $locale->lang !== (string) $locale ){
            
$fallback = new Loco_Locale($locale->lang);
            if( ! 
$api->isInstalled($fallback) ){
                
$package->addLocale($fallback);
            }
        }

        
// Get PO files for this locale
        
$files $package->findLocaleFiles();
        
$translations = [];
        
$modified 0;
        
$npofiles 0;
        
$nfiles 0;

        
// source locale means we want to see POT instead of translations
        
if( 'en_US' === $tag ){
            
$files $package->findTemplateFiles()->augment($files);
        }
        
        
/* @var Loco_fs_File $file */
        
foreach( $files as $file ){
            
$nfiles++;
            if( 
'pot' !== $file->extension() ){
                
$npofiles++;
            }
            
$modified max$modified$file->modified() );
            
$project $package->getProject($file);
            
// do similarly to Loco_admin_bundle_ViewController::createFileParams
            
$meta Loco_gettext_Metadata::load($file);
            
$dir = new Loco_fs_LocaleDirectory$file->dirname() );
            
// arguments for deep link into project
            
$slug $project->getSlug();
            
$domain $project->getDomain()->getName();
            
$bundle $project->getBundle();
            
$type strtolower$bundle->getType() );
            
$args = [
                
// 'locale' => $tag,
                
'bundle' => $bundle->getHandle(),
                
'domain' => $project->getId(),
                
'path' => $meta->getPath(false),
            ];
            
// append data required for PO table row, except use bundle data instead of locale data
            
$translations[$type][] = new Loco_mvc_ViewParams(  [
                
// bundle info
                
'title' => $project->getName(),
                
'domain' => $domain,
                
'short' => ! $slug || $project->isDomainDefault() ? $domain $domain.'→'.$slug,
                
// file info
                
'meta' => $meta,
                
'name' => $file->basename(),
                
'time' => $file->modified(),
                
'type' => strtoupper$file->extension() ),
                
'todo' => $meta->countIncomplete(),
                
'total' => $meta->getTotal(),
                
// author / system / custom / other
                
'store' => $dir->getTypeLabel$dir->getTypeId() ),
                
// links
                
'view' =>   Loco_mvc_AdminRouter::generate$type.'-file-view'$args ),
                
'info' =>   Loco_mvc_AdminRouter::generate$type.'-file-info'$args ),
                
'edit' =>   Loco_mvc_AdminRouter::generate$type.'-file-edit'$args ),
                
'move' =>   Loco_mvc_AdminRouter::generate$type.'-file-move'$args ),
                
'delete' => Loco_mvc_AdminRouter::generate$type.'-file-delete'$args ),
                
'copy' =>   Loco_mvc_AdminRouter::generate$type.'-msginit'$args ),
            ] );
        }
        
        
$title __'Installed languages''loco-translate' );
        
$breadcrumb = new Loco_admin_Navigation;
        
$breadcrumb->add$titleLoco_mvc_AdminRouter::generate('lang') );
        
//$breadcrumb->add( $locale->getName() );
        
$breadcrumb->add$tag );

        
// It's unlikely that an "installed" language would have no files, but could happen if only MO on disk
        
if( === $nfiles ){
            return 
$this->view('admin/errors/no-locale'compact('breadcrumb','locale') );
        }
        
        
// files may be available for language even if not installed (i.e. no core files on disk)
        
if( ! $installed || ! isset($translations['core']) && 'en_US' !== $tag ){
            
Loco_error_AdminNotices::warn__('No core translation files are installed for this language','loco-translate') )
                ->
addLink('https://codex.wordpress.org/Installing_WordPress_in_Your_Language'__('Documentation','loco-translate') );
        }

        
// Translated type labels and "See all <type>" links
        
$types = [
            
'core' => new Loco_mvc_ViewParams( [
                
'name' => __('WordPress Core','loco-translate'),
                
'text' => __('See all core translations','loco-translate'), 
                
'href' => Loco_mvc_AdminRouter::generate('core'
            ] ),
            
'theme' => new Loco_mvc_ViewParams( [
                
'name' => __('Themes','loco-translate'),
                
'text' => __('See all themes','loco-translate'), 
                
'href' => Loco_mvc_AdminRouter::generate('theme'
            ] ),
            
'plugin' => new Loco_mvc_ViewParams( [
                
'name' => __('Plugins','loco-translate'),
                
'text' => __('See all plugins','loco-translate'), 
                
'href' => Loco_mvc_AdminRouter::generate('plugin'
            ] ),
        ];
        
        
$this->set'locale', new Loco_mvc_ViewParams( [
            
'code' => $tag,
            
'name' => $locale->getName(),
            
'attr' => 'class="'.$locale->getIcon().'" lang="'.$locale->lang.'"',
        ] ) );
        
        
// Sort each translation set alphabetically by bundle name...
        
foreach( array_keys($translations) as $type ){
            
usort$translations[$type], function( ArrayAccess $aArrayAccess $b ):int 
                return 
strcasecmp($a['title'],$b['title']);
            } );
        }

        return 
$this->view'admin/bundle/locale'compact('breadcrumb','translations','types','npofiles','modified') );
    }

    
}