PluginProbe ʕ •ᴥ•ʔ
Rank Math SEO – AI SEO Tools to Dominate SEO Rankings / 1.0.271.1
Rank Math SEO – AI SEO Tools to Dominate SEO Rankings v1.0.271.1
1.0.272 1.0.271 1.0.271.1 1.0.270 1.0.269 trunk 1.0.216 1.0.217 1.0.218 1.0.219 1.0.220 1.0.221 1.0.222 1.0.223 1.0.224 1.0.225 1.0.226 1.0.227 1.0.227.1 1.0.228 1.0.229 1.0.230 1.0.231 1.0.232 1.0.233 1.0.234 1.0.234.1 1.0.235 1.0.236 1.0.237 1.0.238 1.0.239 1.0.240 1.0.241 1.0.242 1.0.243 1.0.244 1.0.245 1.0.246 1.0.247 1.0.248 1.0.249 1.0.250 1.0.251 1.0.251.1 1.0.252 1.0.252.1 1.0.253 1.0.254 1.0.255 1.0.256 1.0.257 1.0.258 1.0.259 1.0.259.1 1.0.260 1.0.261 1.0.262 1.0.263 1.0.264 1.0.264.1 1.0.265 1.0.266 1.0.266.1 1.0.267 1.0.268
seo-by-rank-math / includes / 3rdparty / loco / class-loco-i18n-inline.php
seo-by-rank-math / includes / 3rdparty / loco Last commit date
class-loco-i18n-inline.php 8 months ago
class-loco-i18n-inline.php
142 lines
1 <?php
2 /**
3 * Loco Translate inline i18n helper for injecting translations without JSON files.
4 *
5 * @since 1.0.256
6 * @package RankMath
7 * @subpackage RankMath\Core
8 * @author Rank Math <support@rankmath.com>
9 */
10
11 namespace RankMath\ThirdParty\Loco;
12
13 use RankMath\Traits\Hooker;
14 use RankMath\Helper;
15 use RankMath\Helpers\Str;
16
17 defined( 'ABSPATH' ) || exit;
18
19 /**
20 * Loco_I18n_Inline class.
21 */
22 class Loco_I18n_Inline {
23
24 use Hooker;
25
26 /**
27 * Class constructor.
28 */
29 public function __construct() {
30 $this->action( 'rank-math/admin_enqueue_scripts', 'inject_settings_locale' );
31 }
32
33 /**
34 * Inject inline JED locale for settings screens when Loco is active.
35 */
36 public function inject_settings_locale() {
37 // Load translations from all Loco locations (Customization, System, Author).
38 $json = $this->get_jed_json( 'rank-math', false );
39 if ( empty( $json ) ) {
40 return;
41 }
42
43 $inline = sprintf(
44 'try{if(window.wp&&wp.i18n&&wp.i18n.setLocaleData){wp.i18n.setLocaleData(%s,"rank-math");var __rm=wp.i18n.getLocaleData&&wp.i18n.getLocaleData("rank-math");if(__rm){wp.i18n.setLocaleData(__rm,"seo-by-rank-math");}}}catch(e){}',
45 $json
46 );
47
48 wp_add_inline_script( 'common', $inline, 'before' );
49 }
50
51 /**
52 * Build a JED locale JSON from the best available MO/PO for the current locale.
53 *
54 * @param string $domain Text domain (e.g. 'rank-math').
55 * @param bool $loco_only Whether to restrict to Loco-managed files only.
56 * @return string JSON string suitable for wp.i18n.setLocaleData(), or empty string.
57 */
58 private function get_jed_json( $domain, $loco_only = true ) {
59 $locale = get_user_locale();
60
61 $file = $this->find_translation_file( $domain, $locale, $loco_only );
62 if ( ! $file ) {
63 return '';
64 }
65
66 return $this->build_jed_json( $file, $domain, $locale );
67 }
68
69 /**
70 * Build JED JSON from translation file.
71 *
72 * @param string $file Path to MO or PO file.
73 * @param string $domain Text domain.
74 * @param string $locale Locale.
75 * @return string JSON string or false.
76 */
77 private function build_jed_json( $file, $domain, $locale ) {
78 require_once ABSPATH . WPINC . '/pomo/mo.php';
79 require_once ABSPATH . WPINC . '/pomo/po.php';
80
81 $translations = Str::ends_with( '.mo', $file ) ? new \MO() : new \PO();
82 if ( ! $translations->import_from_file( $file ) ) {
83 return false;
84 }
85
86 $data = [
87 '' => [
88 'domain' => $domain,
89 'lang' => $locale,
90 ],
91 ];
92
93 if ( ! empty( $translations->headers['plural-forms'] ) ) {
94 $data['']['plural-forms'] = $translations->headers['plural-forms'];
95 }
96
97 foreach ( $translations->entries as $entry ) {
98 if ( empty( $entry->translations ) ) {
99 continue;
100 }
101 $key = empty( $entry->context ) ? $entry->singular : $entry->context . "\004" . $entry->singular;
102 $data[ $key ] = $entry->translations;
103 }
104
105 return wp_json_encode( $data );
106 }
107
108 /**
109 * Find translation file for domain/locale.
110 *
111 * @param string $domain Text domain.
112 * @param string $locale Locale.
113 * @param bool $loco_only Restrict to Loco files only.
114 * @return string|false File path or false.
115 */
116 private function find_translation_file( $domain, $locale, $loco_only ) {
117 $paths = [];
118
119 // 1) Loco "Customization" location.
120 $paths[] = WP_CONTENT_DIR . '/languages/loco/plugins/' . $domain . '-' . $locale . '.mo';
121 $paths[] = WP_CONTENT_DIR . '/languages/loco/plugins/' . $domain . '-' . $locale . '.po';
122
123 // 2) Loco "System" location (global WP languages dir).
124 if ( ! $loco_only ) {
125 $paths[] = WP_LANG_DIR . '/plugins/' . $domain . '-' . $locale . '.mo';
126 $paths[] = WP_LANG_DIR . '/plugins/' . $domain . '-' . $locale . '.po';
127 }
128
129 // 3) Loco "Author" location (plugin bundled languages directory).
130 $paths[] = RANK_MATH_PATH . 'languages/' . $domain . '-' . $locale . '.mo';
131 $paths[] = RANK_MATH_PATH . 'languages/' . $domain . '-' . $locale . '.po';
132
133 foreach ( $paths as $path ) {
134 if ( is_file( $path ) && is_readable( $path ) ) {
135 return $path;
136 }
137 }
138
139 return false;
140 }
141 }
142