PluginProbe
MakeCommerce for WooCommerce / trunk
MakeCommerce for WooCommerce vtrunk
4.1.0 4.0.8 trunk 1.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 2.0.0 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 All 96 releases
makecommerce / includes / i18n.php

i18n.php in MakeCommerce for WooCommerce trunk, at includes/i18n.php

298 lines 7.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace MakeCommerce;
4
5 /**
6 * Define the internationalization functionality
7 *
8 * Loads and defines the internationalization files for this plugin
9 * so that it is ready for translation.
10 *
11 * @link https://makecommerce.net/
12 * @since 3.0.0
13 *
14 * @package Makecommerce
15 * @subpackage Makecommerce/includes
16 */
17
18 /**
19 * Define the internationalization functionality.
20 *
21 * Loads and defines the internationalization files for this plugin
22 * so that it is ready for translation.
23 *
24 * @since 3.0.0
25 * @package Makecommerce
26 * @subpackage Makecommerce/includes
27 * @author Maksekeskus AS <support@maksekeskus.ee>
28 */
29 class i18n {
30
31 //set default locale when there is nothing specified
32 public static string $defaultLocale = "et";
33
34 /**
35 * Load the plugin text domain for translation.
36 *
37 * @since 3.0.0
38 */
39 public function load_plugin_textdomain() {
40
41 load_plugin_textdomain(
42 'wc_makecommerce_domain',
43 false,
44 dirname( dirname( plugin_basename( __FILE__ ) ) ) . '/languages/'
45 );
46
47 }
48
49 /**
50 * Returns country name
51 *
52 * @since 3.0.0
53 */
54 public static function get_country_name( $country_code ) {
55
56 switch ( $country_code ) {
57 case 'ee':
58 return __( 'Estonia', 'wc_makecommerce_domain' );
59 break;
60 case 'lv':
61 return __( 'Latvia', 'wc_makecommerce_domain' );
62 break;
63 case 'lt':
64 return __( 'Lithuania', 'wc_makecommerce_domain' );
65 break;
66 case 'fi':
67 return __( 'Finland', 'wc_makecommerce_domain' );
68 break;
69 case 'other':
70 return __( 'International', 'wc_makecommerce_domain' );
71 break;
72 }
73
74 return $country_code;
75 }
76
77 /**
78 * Gets the default language for the site.
79 *
80 * @since 3.0.3
81 */
82 public static function get_site_default_language() {
83
84 //polylang
85 if ( self::is_polylang_active() ) {
86
87 return strtolower( substr( pll_default_language(), 0, 2 ) );
88 }
89
90 //woocommerce
91 if ( self::is_wpml_woocommerce_active() ) {
92
93 global $sitepress;
94
95 if ( isset($sitepress) ) {
96 return strtolower( substr( $sitepress->get_default_language(), 0, 2 ) );
97 }
98 }
99
100 //default
101 return self::get_two_char_locale();
102 }
103
104 /**
105 * Returns currently used locale
106 * WPML is superior to Polylang in case both are active
107 *
108 * @since 3.0.0
109 */
110 public static function get_locale() {
111
112 $locale = get_locale();
113
114 if ( $locale ) {
115
116 $locale = explode( '_', $locale );
117
118 if ( isset( $locale[0] ) ) {
119 $locale = $locale[0];
120 }
121 }
122
123 //polylang uses different locale names, while wpml uses lv, lt, en. Polylang uses lv_LV, lt_LT and en_GB
124 //this method should be improved and made generic everywhere
125 if ( function_exists( 'pll_languages_list' ) ) {
126 $locale = get_locale();
127 }
128
129 //nothing found, set default
130 if ( empty( $locale ) ) {
131 return self::$defaultLocale;
132 }
133
134 return $locale;
135 }
136
137 /**
138 * Returns the first two characters of the currently used locale
139 *
140 * @since 3.0.0
141 */
142 public static function get_two_char_locale() {
143
144 return strtolower( substr( self::get_locale(), 0, 2 ) );
145 }
146
147 /**
148 * if orderMeta contains wpml language parameter then change the language to it
149 *
150 * @since 3.0.0
151 */
152 public static function change_language_from_post( $orderMeta ) {
153
154 if ( isset( $orderMeta["meta"] ) ) {
155 foreach ( $orderMeta["meta"] as $key=>$value ) {
156 if ( is_array( $value ) ) {
157 if ( $value["key"] == "wpml_language" ) {
158 self::switch_language( $value["value"] );
159 }
160 }
161 }
162 }
163 }
164
165 /**
166 * Returns true if WPML for WooCommerce is active, else false
167 *
168 * @since 3.0.0
169 */
170 public static function is_wpml_woocommerce_active() {
171
172 if ( in_array( 'woocommerce-multilingual/wpml-woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
173 return true;
174 }
175
176 return false;
177 }
178
179 /**
180 * Returns true if polylang plugin is active, else false
181 *
182 * @since 3.0.0
183 */
184 public static function is_polylang_active() {
185
186 if ( function_exists( 'pll_languages_list' ) ) {
187 return true;
188 }
189
190 return false;
191 }
192
193 /**
194 * Check if there are any language plugins like Polylang or WPML used
195 *
196 * @since 3.0.0
197 */
198 public static function using_language_plugins() {
199
200 if ( self::is_polylang_active() || self::is_wpml_woocommerce_active() ) {
201 return true;
202 }
203
204 return false;
205 }
206
207 /**
208 * Returns a string from mo file for the specified locale
209 *
210 * @since 3.0.0
211 */
212 public static function get_string_from_mo( $string, $textdomain, $locale ) {
213
214 $file = plugin_dir_path( __FILE__ ) . '../languages/' . $textdomain . '-' . $locale . '.mo';
215
216 if ( file_exists( $file ) ) {
217
218 $mo = new \MO();
219 $mo->import_from_file( $file );
220
221 if ( isset( $mo->entries ) ) {
222 if ( isset( $mo->entries[$string] ) ) {
223 if ( isset( $mo->entries[$string]->translations ) ) {
224 if ( isset( $mo->entries[$string]->translations[0] ) ) {
225 $string = $mo->entries[$string]->translations[0];
226 }
227 }
228 }
229 }
230 }
231
232 return $string;
233 }
234
235 /**
236 * Get a list of languages that are used
237 * WPML is superior to Polylang in case both are active
238 *
239 * @since 3.0.0
240 */
241 public static function get_active_languages() {
242
243 $languages = array();
244
245 //wpml
246 if ( function_exists( 'icl_object_id' ) && !function_exists( 'pll_languages_list' ) ) {
247
248 $languages = apply_filters( 'wpml_active_languages', NULL, 'skip_missing=0' );
249 } else if ( function_exists( 'pll_languages_list' ) ) { //polylang
250
251 $pl_locales = pll_languages_list( array( 'fields'=>'locale' ) );
252 foreach ( $pl_locales as $key => $locale_pl ) {
253
254 $language = array( 'id' => $key, 'code' => $locale_pl ) ;
255 $languages[$locale_pl] = $language;
256 }
257 }
258
259 $languages2 = array();
260 foreach ( $languages as $key => $language ) {
261
262 $language["code"] = strtolower( substr( $language["code"], 0, 2 ) );
263 $languages2[strtolower( substr( $key, 0, 2 ) )] = $language;
264 }
265
266 return $languages2;
267 }
268
269 /**
270 * Switches language using WPML
271 *
272 * This function needs work
273 *
274 * @since 3.0.0
275 */
276 public static function switch_language( $language_code ) {
277
278 //switch wordpress language first
279 try {
280 switch_to_locale( $language_code );
281 } catch ( Throwable $t ) {
282 //switching to locale doesnt always work
283 error_log( print_r( $t, true ) );
284 }
285
286 //set wpml language
287 if ( function_exists( 'icl_object_id' ) ) {
288 do_action( 'wpml_switch_language', $language_code );
289
290 } else {
291 //set polylang language
292 if ( function_exists( 'pll_current_language' ) ) {
293 do_action( 'wpml_switch_language', $language_code ); //this also seems to work for polylang
294 }
295 }
296 }
297 }
298