/var/www/html_it/wp-content/plugins/loco-translate/src/fs/LocaleFile.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
<?php
/**
 * A file with metadata about the locale it relates to
 */
class Loco_fs_LocaleFile extends Loco_fs_File {
    
    
/**
     * @var Loco_Locale|null
     */
    
private $locale;
    
    
/**
     * @var string
     */
    
private $suffix;
    
    
/**
     * @var string
     */
    
private $prefix;

    
/**
     * @var string
     */
    
private $hash '';


    
/**
     * Lazy handling of localized path info
     * @return array [ prefix, suffix, hash ]
     */
    
public function split(){
        if( 
is_null($this->suffix) ){
            
// note that `filename` isn't used here because of double extensions (.l10n.php)
            
$parts explode('-',$this->basename() );
            
$tail array_pop($parts);
            
$this->suffix explode('.',$tail,2)[0];
            
// handle script hashes for JSONs only
            
if( '.json' === substr($tail,-5) && preg_match('/^[0-9a-f]{32}$/',$this->suffix) ){
                
$this->hash $this->suffix;
                
$this->suffix array_pop($parts);
            }
            
$this->prefix implode('-',$parts);
            
// handle situations where unsuffixed name is wrongly taken as the prefix
            // e.g. "de.po" is valid but "hello.po" is not. 
            // There are still some ambiguous situations, e.g. "foo-bar.po" is valid, but nonsense
            
if( ! $this->prefix && ! $this->getLocale()->isValid() ){
                
$this->prefix $this->suffix;
                
$this->suffix '';
                
$this->locale null;
            }
        }
        return [ 
$this->prefix$this->suffix$this->hash ];
    }
    
    
    
/**
     * @return Loco_Locale
     */
    
public function getLocale(){
        if( ! 
$this->locale ){
            if( 
$tag $this->getSuffix() ){
                
$this->locale Loco_Locale::parse($tag);
            }
            else {
                
$this->locale = new Loco_Locale('');
            }
        }
        return 
$this->locale;
    }


    
/**
     * @param $locale Loco_locale
     * @return Loco_fs_LocaleFile
     */
    
public function cloneLocaleLoco_locale $locale ){
        
$this->split();
        
$path = (string) $locale;
        if( 
$str $this->prefix ){
            
$path $str.'-'.$path;
        }
        if( 
$str $this->extension() ){
            
$path .= '.'.$str;
        }
        if( 
$dir $this->getParent() ){
            
$path $dir->getPath().'/'.$path;
        }
        return new 
Loco_fs_LocaleFile($path);
    }


    
/**
     * Get prefix (or stem) from name that comes before locale suffix.
     * @return string
     */
    
public function getPrefix(){
        
$info $this->split();
        return 
$info[0];
    }
    

    
/**
     * Get suffix (or locale code) from name that comes after "-" separator
     * @return string
     */
    
public function getSuffix(){
        
$info $this->split();
        return 
$info[1];
    }

    
/**
     * @return string
     */
    
public function getHash(){
        
$info $this->split();
        return 
$info[2];
    }

    
/**
     * Test if file is suffix only, e.g. "en_US.po"
     * @return bool
     */
    
public function hasSuffixOnly(){
        
$info $this->split();
        return 
$info[1] && ! $info[0];
    }


    
/**
     * Test if file is prefix only, e.g. "incorrect.po"
     * @return bool
     */
    
public function hasPrefixOnly(){
        
$info $this->split();
        return 
$info[0] && ! $info[1];
    }

}