PluginProbe ʕ •ᴥ•ʔ
Loco Translate / 2.8.0
Loco Translate v2.8.0
2.8.5 2.8.4 2.5.8 2.6.0 2.6.1 2.6.10 2.6.11 2.6.12 2.6.13 2.6.14 2.6.2 2.6.3 2.6.4 2.6.5 2.6.6 2.6.7 2.6.8 2.6.9 2.7.0 2.7.1 2.7.2 2.7.3 2.8.0 2.8.1 2.8.2 2.8.3 trunk 1.0.0 1.1.0 1.1.1 1.1.2 1.1.3 1.2 1.2.1 1.2.2 1.3 1.3.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.5 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 2.0.0 2.0.1 2.0.10 2.0.11 2.0.12 2.0.13 2.0.14 2.0.15 2.0.16 2.0.17 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.2.0 2.2.1 2.2.2 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7
loco-translate / src / api / WordPressTranslations.php
loco-translate / src / api Last commit date
ChatGpt.php 1 year ago Client.php 1 year ago DeepL.php 1 year ago Providers.php 1 year ago WordPressFileSystem.php 1 year ago WordPressTranslations.php 3 years ago
WordPressTranslations.php
172 lines
1 <?php
2 /**
3 * Wrapper for WordPress language availability
4 */
5 class Loco_api_WordPressTranslations {
6
7 /**
8 * Cache of whether network access is allowed
9 * @var bool
10 */
11 private $enabled;
12
13 /**
14 * Cache of core locale objects
15 * @var Loco_Locale[]
16 */
17 private $locales;
18
19 /**
20 * Cache of data returned from get_available_languages (not cached by WP)
21 * @var array
22 */
23 private $installed;
24
25 /**
26 * Hash map of installed languages indexed by tag
27 * @var array
28 */
29 private $installed_hash;
30
31
32 /**
33 * Wrap wp_get_available_translations
34 * @return array[]
35 */
36 private function wp_get_available_translations(){
37 if( ! function_exists('wp_get_available_translations') ){
38 require_once ABSPATH.'wp-admin/includes/translation-install.php';
39 }
40 // WordPress will raise Warning if offline, and will cache result otherwise.
41 return wp_get_available_translations();
42 }
43
44
45 /**
46 * Get fully fledged locale objects from available core translation data
47 * @return Loco_Locale[]
48 */
49 public function getAvailableCore(){
50 $locales = $this->locales;
51 if( is_null($locales) ){
52 $locales = [];
53 // get official locales from API if we have network
54 $cached = $this->wp_get_available_translations();
55 if( is_array($cached) && $cached ){
56 $english_name = 'english_name';
57 $native_name = 'native_name';
58 }
59 // else fall back to bundled data cached
60 else {
61 $english_name = 0;
62 $native_name = 1;
63 $cached = Loco_data_CompiledData::get('locales');
64 // debug so we can see on front end that data was offline
65 // $locales['en-debug'] = ( new Loco_Locale('en','','debug') )->setName('OFFLINE DATA');
66 }
67 /* @var string $tag */
68 foreach( $cached as $tag => $raw ){
69 $locale = Loco_Locale::parse($tag);
70 if( $locale->isValid() ){
71 $locale->setName( $raw[$english_name], $raw[$native_name] );
72 $locales[ (string) $locale ] = $locale;
73 }
74 /* Skip invalid language tags, e.g. "pt_PT_ao90" should be "pt_PT_ao1990"
75 * No point fixing invalid tags, because core translation files won't match.
76 else {
77 Loco_error_AdminNotices::debug( sprintf('Invalid locale: %s', $tag) );
78 }*/
79 }
80 $this->locales = $locales;
81 }
82 return $locales;
83 }
84
85
86 /**
87 * Wrap get_available_languages
88 * @return string[]
89 */
90 public function getInstalledCore(){
91 // wp-includes/l10n.php should always be included at runtime
92 if( ! is_array($this->installed) ){
93 $this->installed = get_available_languages();
94 // en_US is implicitly installed
95 if( ! in_array('en_US',$this->installed) ){
96 array_unshift( $this->installed, 'en_US' );
97 }
98 }
99 return $this->installed;
100 }
101
102
103 /**
104 * @return array
105 */
106 private function getInstalledHash(){
107 if( ! is_array($this->installed_hash) ){
108 $this->installed_hash = array_flip( $this->getInstalledCore() );
109 }
110 return $this->installed_hash;
111 }
112
113
114 /**
115 * Check if a given locale is installed
116 * @param string|Loco_Locale
117 * @return bool
118 */
119 public function isInstalled( $locale ){
120 return array_key_exists( (string) $locale, $this->getInstalledHash() );
121 }
122
123
124 /**
125 * Get WordPress locale data by strictly well-formed language tag
126 * @param string $tag
127 * @return Loco_Locale
128 */
129 public function getLocale( $tag ){
130 $all = $this->getAvailableCore();
131 return isset($all[$tag]) ? $all[$tag] : null;
132 }
133
134
135 /**
136 * Check whether remote API may be disabled for whatever reason, usually debugging.
137 * @return bool
138 */
139 public function hasNetwork(){
140 if( is_null($this->enabled) ){
141 $this->enabled = (bool) apply_filters('loco_allow_remote', true );
142 }
143 return $this->enabled;
144 }
145
146
147 /**
148 * Wrapper for translations_api
149 * @param string
150 * @param array
151 * @return array[]
152 */
153 public function apiGet( $type, array $args ){
154 if( ! function_exists('translations_api') ){
155 require_once ABSPATH.'wp-admin/includes/translation-install.php';
156 }
157 $response = translations_api($type,$args);
158 if( $response instanceof WP_Error ){
159 $message = 'Unknown error from translations_api';
160 foreach( $response->get_error_messages() as $message ){
161 Loco_error_AdminNotices::debug('translations_api error: '.$message);
162 }
163 throw new Loco_error_Exception($message);
164 }
165
166 return (array) $response;
167 }
168
169
170 }
171
172