PluginProbe
MakeCommerce for WooCommerce / 3.0.2
MakeCommerce for WooCommerce v3.0.2
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 3.0.2, at includes/i18n.php

268 lines 6.4 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 $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 }
70
71 return $country_code;
72 }
73
74 /**
75 * Returns currently used locale
76 * WPML is superior to Polylang in case both are active
77 *
78 * @since 3.0.0
79 */
80 public static function get_locale() {
81
82 $locale = get_locale();
83
84 if ( $locale ) {
85
86 $locale = explode( '_', $locale );
87
88 if ( isset( $locale[0] ) ) {
89 $locale = $locale[0];
90 }
91 }
92
93 //polylang uses different locale names, while wpml uses lv, lt, en. Polylang uses lv_LV, lt_LT and en_GB
94 //this method should be improved and made generic everywhere
95 if ( function_exists( 'pll_languages_list' ) ) {
96 $locale = get_locale();
97 }
98
99 //nothing found, set default
100 if ( empty( $locale ) ) {
101 return self::$defaultLocale;
102 }
103
104 return $locale;
105 }
106
107 /**
108 * Returns the first two characters of the currently used locale
109 *
110 * @since 3.0.0
111 */
112 public static function get_two_char_locale() {
113
114 return strtolower( substr( self::get_locale(), 0, 2 ) );
115 }
116
117 /**
118 * if orderMeta contains wpml language parameter then change the language to it
119 *
120 * @since 3.0.0
121 */
122 public static function change_language_from_post( $orderMeta ) {
123
124 if ( isset( $orderMeta["meta"] ) ) {
125 foreach ( $orderMeta["meta"] as $key=>$value ) {
126 if ( is_array( $value ) ) {
127 if ( $value["key"] == "wpml_language" ) {
128 self::switch_language( $value["value"] );
129 }
130 }
131 }
132 }
133 }
134
135 /**
136 * Returns true if WPML for WooCommerce is active, else false
137 *
138 * @since 3.0.0
139 */
140 public static function is_wpml_woocommerce_active() {
141
142 if ( in_array( 'woocommerce-multilingual/wpml-woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
143 return true;
144 }
145
146 return false;
147 }
148
149 /**
150 * Returns true if polylang plugin is active, else false
151 *
152 * @since 3.0.0
153 */
154 public static function is_polylang_active() {
155
156 if ( function_exists( 'pll_languages_list' ) ) {
157 return true;
158 }
159
160 return false;
161 }
162
163 /**
164 * Check if there are any language plugins like Polylang or WPML used
165 *
166 * @since 3.0.0
167 */
168 public static function using_language_plugins() {
169
170 if ( self::is_polylang_active() || self::is_wpml_woocommerce_active() ) {
171 return true;
172 }
173
174 return false;
175 }
176
177 /**
178 * Returns a string from mo file for the specified locale
179 *
180 * @since 3.0.0
181 */
182 public static function get_string_from_mo( $string, $textdomain, $locale ) {
183
184 $file = plugin_dir_path( __FILE__ ) . '../languages/' . $textdomain . '-' . $locale . '.mo';
185
186 if ( file_exists( $file ) ) {
187
188 $mo = new \MO();
189 $mo->import_from_file( $file );
190
191 if ( isset( $mo->entries ) ) {
192 if ( isset( $mo->entries[$string] ) ) {
193 if ( isset( $mo->entries[$string]->translations ) ) {
194 if ( isset( $mo->entries[$string]->translations[0] ) ) {
195 $string = $mo->entries[$string]->translations[0];
196 }
197 }
198 }
199 }
200 }
201
202 return $string;
203 }
204
205 /**
206 * Get a list of languages that are used
207 * WPML is superior to Polylang in case both are active
208 *
209 * @since 3.0.0
210 */
211 public static function get_active_languages() {
212
213 $languages = array();
214
215 //wpml
216 if ( function_exists( 'icl_object_id' ) && !function_exists( 'pll_languages_list' ) ) {
217
218 $languages = apply_filters( 'wpml_active_languages', NULL, 'skip_missing=0' );
219 } else if ( function_exists( 'pll_languages_list' ) ) { //polylang
220
221 $pl_locales = pll_languages_list( array( 'fields'=>'locale' ) );
222 foreach ( $pl_locales as $key => $locale_pl ) {
223
224 $language = array( 'id' => $key, 'code' => $locale_pl ) ;
225 $languages[$locale_pl] = $language;
226 }
227 }
228
229 $languages2 = array();
230 foreach ( $languages as $key => $language ) {
231
232 $language["code"] = strtolower( substr( $language["code"], 0, 2 ) );
233 $languages2[strtolower( substr( $key, 0, 2 ) )] = $language;
234 }
235
236 return $languages2;
237 }
238
239 /**
240 * Switches language using WPML
241 *
242 * This function needs work
243 *
244 * @since 3.0.0
245 */
246 public static function switch_language( $language_code ) {
247
248 //switch wordpress language first
249 try {
250 switch_to_locale( $language_code );
251 } catch ( Throwable $t ) {
252 //switching to locale doesnt always work
253 error_log( print_r( $t, true ) );
254 }
255
256 //set wpml language
257 if ( function_exists( 'icl_object_id' ) ) {
258 do_action( 'wpml_switch_language', $language_code );
259
260 } else {
261 //set polylang language
262 if ( function_exists( 'pll_current_language' ) ) {
263 do_action( 'wpml_switch_language', $language_code ); //this also seems to work for polylang
264 }
265 }
266 }
267 }
268