PluginProbe
Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant / 2.3.2
Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant v2.3.2
2.3.2 2.3.1 2.3.0 2.2.8 2.2.7 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.10.5 1.11.0 1.11.1 1.11.2 1.6 1.7 1.8 1.8.1 1.8.2 1.8.3 1.9.0 1.9.1 1.9.10 1.9.11 All 60 releases
merchant / inc / MultiLang / class-merchant-translator.php

class-merchant-translator.php in Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant 2.3.2, at inc/MultiLang/class-merchant-translator.php

240 lines 6.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) {
4 exit; // Exit if accessed directly
5 }
6
7 /**
8 * Merchant - Multi Language Translator.
9 */
10 if ( ! class_exists( 'Merchant_Translator' ) ) {
11 class Merchant_Translator {
12
13 /**
14 * @var Merchant_Language_Strategy $language_strategy The language strategy class.
15 */
16 private static $language_strategy;
17
18 /**
19 * Merchant_Translator constructor.
20 */
21 public function __construct() {
22 self::set_language_strategy();
23 }
24
25 /**
26 * Check if Polylang is active.
27 *
28 * @return bool
29 */
30 public static function is_polylang_active() {
31 return function_exists( 'pll_count_posts' );
32 }
33
34 /**
35 * Check if WPML is active.
36 *
37 * @return bool
38 */
39 public static function is_wpml_active() {
40 return function_exists( 'icl_object_id' );
41 }
42
43 /**
44 * Set the translator class.
45 */
46 public static function set_language_strategy(): void {
47 if ( self::$language_strategy !== null ) {
48 return;
49 }
50
51 if ( self::is_polylang_active() ) {
52 $translator_object = new Merchant_PolyLang_Support();
53 } elseif ( self::is_wpml_active() ) {
54 $translator_object = new Merchant_WPML_Support();
55 } else {
56 $translator_object = new Merchant_No_Plugin_Support();
57 }
58
59 /**
60 * Filter the translator class.
61 *
62 * @param Merchant_Language_Strategy $translator_object The translator class object.
63 *
64 * @since 1.8
65 */
66 self::$language_strategy = apply_filters( 'merchant_translator_class_object', $translator_object );
67 }
68
69 /**
70 * Register a string for translation.
71 *
72 * @param string $string_to_register The string to translate.
73 * @param string $context The context of the string.
74 */
75 public static function register_string( $string_to_register, $context = 'merchant' ): void {
76 self::set_language_strategy();
77 self::$language_strategy->register_string( $string_to_register, $context );
78 }
79
80 /**
81 * Translate a string.
82 *
83 * @param string $string_to_translate The string to translate.
84 *
85 * @return string
86 */
87 public static function translate( $string_to_translate ) {
88 self::set_language_strategy();
89 $translated_string = self::$language_strategy->translate_string( $string_to_translate );
90
91 /**
92 * Filter the translated string.
93 *
94 * @param string $string_to_translate The string to translate.
95 * @param string $translated_string The translated string.
96 *
97 * @since 1.8
98 */
99 return apply_filters( 'merchant_multi_lang_translated_string', $translated_string, $string_to_translate );
100 }
101
102 /**
103 * Translate a taxonomy term slug or ID to the current (or specified) language.
104 *
105 * Accepts a single term or an array of terms. When an array is given,
106 * each element is translated individually and an array is returned.
107 *
108 * @param array<int, string|int>|string|int $term Term slug(s) or term ID(s) to translate.
109 * @param string $taxonomy The taxonomy name (e.g. 'product_cat').
110 * @param string|null $language Target language code. Null = current language.
111 *
112 * @return array<int, string|int>|string|int Translated term(s) (same type as input).
113 *
114 * @since 2.1.9
115 */
116 public static function translate_term( $term, $taxonomy, $language = null ) {
117 self::set_language_strategy();
118
119 if ( is_array( $term ) ) {
120 return array_map(
121 static function ( $single ) use ( $taxonomy, $language ) {
122 return self::$language_strategy->translate_term( $single, $taxonomy, $language );
123 },
124 $term
125 );
126 }
127
128 return self::$language_strategy->translate_term( $term, $taxonomy, $language );
129 }
130
131 /**
132 * Translate a taxonomy term slug or ID to the default (primary) language.
133 *
134 * Useful for admin UI where saved values must always be in the default language
135 * regardless of the admin's active dashboard language.
136 *
137 * @param string|int $term The term slug or term ID to translate.
138 * @param string $taxonomy The taxonomy name (e.g. 'product_cat').
139 *
140 * @return string|int Translated term in the default language (same type as input).
141 *
142 * @since 2.1.9
143 */
144 public static function translate_term_to_default( $term, $taxonomy ) {
145 self::set_language_strategy();
146 $default_lang = self::$language_strategy->get_default_language();
147
148 return self::$language_strategy->translate_term( $term, $taxonomy, $default_lang );
149 }
150
151 /**
152 * Switch the active language context for subsequent queries.
153 *
154 * Must be paired with restore_language() to prevent side effects.
155 *
156 * @param string $language Language code to switch to.
157 *
158 * @return void
159 *
160 * @since 2.1.9
161 */
162 public static function switch_language( $language ): void {
163 self::set_language_strategy();
164 self::$language_strategy->switch_language( $language );
165 }
166
167 /**
168 * Restore the language context to what it was before switch_language().
169 *
170 * @return void
171 *
172 * @since 2.1.9
173 */
174 public static function restore_language(): void {
175 self::set_language_strategy();
176 self::$language_strategy->restore_language();
177 }
178
179 /**
180 * Switch to the default (primary) language for subsequent queries.
181 *
182 * Convenience wrapper around switch_language( get_default_language() ).
183 * Must be paired with restore_language().
184 *
185 * @return void
186 *
187 * @since 2.1.9
188 */
189 public static function switch_to_default_language(): void {
190 self::set_language_strategy();
191 $default = self::$language_strategy->get_default_language();
192
193 if ( ! empty( $default ) ) {
194 self::$language_strategy->switch_language( $default );
195 }
196 }
197
198 /**
199 * Disable language filtering for subsequent queries.
200 *
201 * Useful for admin dropdowns that must show ALL terms regardless of
202 * language (including terms that only exist in secondary languages).
203 * Must be paired with restore_language().
204 *
205 * - WPML: passes 'all' to wpml_switch_language
206 * - Polylang: passes '' to get_terms_args lang filter
207 * - No-Plugin: no-op
208 *
209 * @return void
210 *
211 * @since 2.1.9
212 */
213 public static function switch_to_all_languages(): void {
214 self::set_language_strategy();
215 $code = self::$language_strategy->get_all_languages_code();
216
217 self::$language_strategy->switch_language( $code );
218 }
219
220 /**
221 * Get the language code for a specific post.
222 *
223 * Reliable in any context (page load, AJAX, REST) because it
224 * queries the post's stored language rather than relying on
225 * the current request's language detection.
226 *
227 * @param int $post_id The post ID.
228 *
229 * @return string Language code (e.g. 'ar'), or empty string.
230 *
231 * @since 2.1.9
232 */
233 public static function get_post_language( $post_id ) {
234 self::set_language_strategy();
235
236 return self::$language_strategy->get_post_language( $post_id );
237 }
238 }
239 }
240