WC_POS_i18n

Define the internationalization functionality.

Defined (1)

The class is defined in the following location(s).

/includes/class-wc-pos-i18n.php  
  1. class WC_POS_i18n { 
  2.  
  3. private $github_url; 
  4.  
  5. /** 
  6. * Constructor 
  7. */ 
  8. public function __construct() { 
  9.  
  10. // raw github url for language packs 
  11. // todo: use last commit info and switch to cdn 
  12. // $this->github_url = 'https://cdn.rawgit.com/kilbot/WooCommerce-POS-Language-Packs/master/'; 
  13. $this->github_url = 'https://raw.githubusercontent.com/kilbot/WooCommerce-POS-Language-Packs/master/'; 
  14.  
  15. // add_action( 'init', array( $this, 'load_plugin_textdomain' ) ); 
  16. $this->load_plugin_textdomain(); 
  17. add_filter( 'pre_set_site_transient_update_plugins', array( $this, 'update_check' ) ); 
  18. add_filter( 'upgrader_pre_download', array( $this, 'upgrader_pre_download' ), 10, 3 ); 
  19. add_filter( 'woocommerce_pos_enqueue_scripts', array( $this, 'js_locale' ) ); 
  20. add_filter( 'woocommerce_pos_admin_enqueue_scripts', array( $this, 'js_locale' ) ); 
  21.  
  22. // ajax 
  23. add_action( 'wp_ajax_wc_pos_update_translations', array( $this, 'update_translations' ) ); 
  24.  
  25. /** 
  26. * Load the plugin text domain for translation. 
  27. */ 
  28. public function load_plugin_textdomain() { 
  29.  
  30. $locale = apply_filters( 'plugin_locale', get_locale(), 'woocommerce-pos' ); 
  31. $dir = trailingslashit( WP_LANG_DIR ); 
  32.  
  33. load_textdomain( 'woocommerce-pos', $dir . 'woocommerce-pos/woocommerce-pos-' . $locale . '.mo' ); 
  34. load_textdomain( 'woocommerce-pos', $dir . 'plugins/woocommerce-pos-' . $locale . '.mo' ); 
  35.  
  36. // admin translations 
  37. if ( is_admin() ) { 
  38. load_textdomain( 'woocommerce-pos', $dir . 'woocommerce-pos/woocommerce-pos-admin-' . $locale . '.mo' ); 
  39. load_textdomain( 'woocommerce-pos', $dir . 'plugins/woocommerce-pos-admin-' . $locale . '.mo' ); 
  40. } else { 
  41. load_textdomain( 'woocommerce', $dir . 'woocommerce/woocommerce-admin-' . $locale . '.mo' ); 
  42. load_textdomain( 'woocommerce', $dir . 'plugins/woocommerce-admin-' . $locale . '.mo' ); 
  43.  
  44.  
  45. /** 
  46. * Check GitHub repo for updated language packs 
  47. * @param $transient 
  48. * @param bool $force 
  49. * @return mixed 
  50. */ 
  51. public function update_check( $transient, $force = false ) { 
  52. $locale = get_locale(); 
  53.  
  54. // pre_set_site_transient_update_plugins is called twice 
  55. // we only want to act on the second run 
  56. // also only continue for non English locales 
  57. if ( empty( $transient->checked ) || strpos( $locale, 'en_' ) === 0 ) { 
  58. return $transient; 
  59.  
  60. // get package.json from github 
  61. $request = wp_remote_get( 
  62. $this->github_url . 'package.json',  
  63. array( 'timeout' => 45 ) 
  64. ); 
  65.  
  66. if ( is_wp_error( $request ) || wp_remote_retrieve_response_code( $request ) != 200 ) { 
  67. return $transient; 
  68.  
  69. // see if translation pack exists 
  70. $response = json_decode( wp_remote_retrieve_body( $request ) ); 
  71. $transient = apply_filters( 'woocommerce_pos_language_packs_upgrade', $transient, $response, $this->github_url, $force ); 
  72. if ( !isset( $response->locales->$locale ) ) { 
  73. return $transient; 
  74.  
  75. // compare 
  76. $new = strtotime( $response->locales->$locale ); 
  77. $options = get_option( 'woocommerce_pos_language_packs' ); 
  78.  
  79. if ( isset( $options[ $locale ] ) && $options[ $locale ] >= $new && !$force ) { 
  80. return $transient; 
  81.  
  82. // update required 
  83. $transient->translations[] = array( 
  84. 'type' => 'plugin',  
  85. 'slug' => 'woocommerce-pos',  
  86. 'language' => $locale,  
  87. 'version' => WC_POS_VERSION,  
  88. 'updated' => date( 'Y-m-d H:i:s', $new ),  
  89. 'package' => $this->github_url . 'packages/woocommerce-pos-' . $locale . '.zip',  
  90. 'autoupdate' => 1 
  91. ); 
  92.  
  93. return $transient; 
  94.  
  95.  
  96. /** 
  97. * Update the database with new language pack date 
  98. * TODO: is there no later hook for translation install? 
  99. * @param $reply 
  100. * @param $package 
  101. * @param $upgrader 
  102. * @return mixed 
  103. */ 
  104. public function upgrader_pre_download( $reply, $package, $upgrader ) { 
  105.  
  106. if ( isset( $upgrader->skin->language_update ) 
  107. && $upgrader->skin->language_update->slug == 'woocommerce-pos' 
  108. ) { 
  109.  
  110. $options = get_option( 'woocommerce_pos_language_packs', array() ); 
  111. $locale = get_locale(); 
  112. $options[ $locale ] = current_time( 'timestamp' ); 
  113. if ( !add_option( 'woocommerce_pos_language_packs', $options, '', 'no' ) ) { 
  114. update_option( 'woocommerce_pos_language_packs', $options ); 
  115.  
  116. return $reply; 
  117.  
  118. /** 
  119. * Force update translations from AJAX 
  120. */ 
  121. public function update_translations() { 
  122. // security 
  123. WC_POS_Server::check_ajax_referer(); 
  124.  
  125. header( "Content-Type: text/event-stream" ); 
  126. header( "Cache-Control: no-cache" ); 
  127. header( "Access-Control-Allow-Origin: *" ); 
  128.  
  129. echo ":" . str_repeat( " ", 2048 ) . PHP_EOL; // 2 kB padding for IE 
  130.  
  131. $this->manual_update(); 
  132.  
  133. die(); 
  134.  
  135. /** 
  136. * Force update translations 
  137. */ 
  138. public function manual_update() { 
  139. ob_start(); 
  140. $locale = get_locale(); 
  141. $creds = request_filesystem_credentials( $_GET[ 'security' ], '', false, false, null ); 
  142.  
  143. /** translators: wordpress */ 
  144. $this->flush( sprintf( __( 'Updating translations for %1$s (%2$s)…' ), 'WooCommerce POS', $locale ) ); 
  145.  
  146. $transient = (object)array( 'checked' => true ); 
  147. $update = $this->update_check( $transient, true ); 
  148.  
  149. if ( empty( $update->translations ) ) { 
  150. /** note: no translation exists */ 
  151. $this->flush( 'No translations found for ' . $locale . '. <a href="mailto:support@woopos.com.au">Contact us</a> if you would like to help translate WooCommerce POS into your language.' ); 
  152. $this->flush( 'complete' ); 
  153.  
  154. return; 
  155.  
  156. if ( !$creds || !WP_Filesystem( $creds ) ) { 
  157. /** translators: wordpress */ 
  158. $this->flush( __( 'Translation update failed.' ) ); 
  159. $this->flush( 'complete' ); 
  160.  
  161. return; 
  162.  
  163. foreach ( $update->translations as $translation ) { 
  164.  
  165. /** translators: wordpress */ 
  166. $this->flush( sprintf( __( 'Downloading translation from <span class="code">%s</span>…' ), $translation[ 'package' ] ) ); 
  167.  
  168. $response = wp_remote_get( 
  169. $translation[ 'package' ],  
  170. array( 'sslverify' => false, 'timeout' => 60, 'filename' => $locale . '.zip' ) 
  171. ); 
  172.  
  173. if ( is_wp_error( $response ) || ( $response[ 'response' ][ 'code' ] < 200 || $response[ 'response' ][ 'code' ] >= 300 ) ) { 
  174. /** translators: wordpress */ 
  175. $this->flush( __( 'Translation update failed.' ) ); 
  176. continue; 
  177.  
  178. global $wp_filesystem; 
  179.  
  180. $upload_dir = wp_upload_dir(); 
  181. $file = trailingslashit( $upload_dir[ 'path' ] ) . $locale . '.zip'; 
  182.  
  183. // Save the zip file 
  184. if ( !$wp_filesystem->put_contents( $file, $response[ 'body' ], FS_CHMOD_FILE ) ) { 
  185. /** translators: wordpress */ 
  186. $this->flush( __( 'Translation update failed.' ) ); 
  187. continue; 
  188.  
  189. // Unzip the file to wp-content/languages/plugins directory 
  190. $dir = trailingslashit( WP_LANG_DIR ) . 'plugins/'; 
  191. $unzip = unzip_file( $file, $dir ); 
  192. if ( true !== $unzip ) { 
  193. /** translators: wordpress */ 
  194. $this->flush( __( 'Translation update failed.' ) ); 
  195. continue; 
  196.  
  197. // Delete the package file 
  198. $wp_filesystem->delete( $file ); 
  199.  
  200. // Update options timestamp 
  201. $key = str_replace( '-', '_', $translation[ 'slug' ] ) . '_language_packs'; 
  202. $options = get_option( $key, array() ); 
  203. $options[ $locale ] = current_time( 'timestamp' ); 
  204. if ( !add_option( $key, $options, '', 'no' ) ) { 
  205. update_option( $key, $options ); 
  206.  
  207. /** translators: wordpress */ 
  208. $this->flush( __( 'Translation updated successfully.' ) ); 
  209.  
  210.  
  211. $this->flush( 'complete' ); 
  212.  
  213. return; 
  214.  
  215.  
  216. /** 
  217. * Flush output 
  218. * @param $data 
  219. */ 
  220. private function flush( $data ) { 
  221. echo 'data:' . $data . PHP_EOL; 
  222. echo PHP_EOL; 
  223. ob_flush(); 
  224. flush(); 
  225.  
  226. /** 
  227. * Load translations for js plugins 
  228. * @param $scripts 
  229. * @return string 
  230. */ 
  231. public function js_locale( array $scripts ) { 
  232. $locale = apply_filters( 'plugin_locale', get_locale(), WC_POS_PLUGIN_NAME ); 
  233. $dir = WC_POS_PLUGIN_PATH . 'languages/js/'; 
  234. $url = WC_POS_PLUGIN_URL . 'languages/js/'; 
  235. list( $country ) = explode( '_', $locale ); 
  236.  
  237. if ( is_readable( $dir . $locale . '.js' ) ) { 
  238. $scripts[ 'locale' ] = $url . $locale . '.js'; 
  239. } elseif ( is_readable( $dir . $country . '.js' ) ) { 
  240. $scripts[ 'locale' ] = $url . $country . '.js'; 
  241.  
  242. return $scripts; 
  243.  
  244. /** 
  245. * Return currency denomination for a given country code 
  246. * @param string $code 
  247. * @return array 
  248. */ 
  249. static public function currency_denominations( $code = '' ) { 
  250. if ( !$code ) { 
  251. $code = get_woocommerce_currency(); 
  252. $denominations = json_decode( file_get_contents( WC_POS_PLUGIN_PATH . 'includes/denominations.json' ) ); 
  253.  
  254. return isset( $denominations->$code ) ? $denominations->$code : $denominations; 
  255.  
  256. /** 
  257. * i18n payload to init POS app 
  258. * @return mixed 
  259. */ 
  260. static public function payload() { 
  261.  
  262. return apply_filters( 'woocommerce_pos_i18n', array( 
  263. 'titles' => array( 
  264. 'browser' => _x( 'Browser', 'system status: browser capabilities', 'woocommerce-pos' ),  
  265. /** translators: woocommerce */ 
  266. 'cart' => __( 'Cart', 'woocommerce' ),  
  267. /** translators: woocommerce */ 
  268. 'checkout' => __( 'Checkout', 'woocommerce' ),  
  269. /** translators: woocommerce */ 
  270. 'coupons' => __( 'Coupons', 'woocommerce' ),  
  271. /** translators: woocommerce */ 
  272. 'customers' => __( 'Customers', 'woocommerce' ),  
  273. /** translators: woocommerce */ 
  274. 'fee' => __( 'Fee', 'woocommerce' ),  
  275. 'hotkeys' => _x( 'HotKeys', 'keyboard shortcuts', 'woocommerce-pos' ),  
  276. /** translators: woocommerce */ 
  277. 'order' => __( 'Order', 'woocommerce' ),  
  278. /** translators: woocommerce */ 
  279. 'orders' => __( 'Orders', 'woocommerce' ),  
  280. /** translators: woocommerce */ 
  281. 'products' => __( 'Products', 'woocommerce' ),  
  282. /** translators: woocommerce */ 
  283. 'receipt' => __( 'Receipt', 'woocommerce' ),  
  284. /** translators: woocommerce */ 
  285. 'shipping' => __( 'Shipping', 'woocommerce' ),  
  286. 'to-pay' => __( 'To Pay', 'woocommerce-pos' ),  
  287. 'paid' => __( 'Paid', 'woocommerce-pos' ),  
  288. 'unpaid' => __( 'Unpaid', 'woocommerce-pos' ),  
  289. 'email-receipt' => __( 'Email Receipt', 'woocommerce-pos' ),  
  290. 'open' => _x( 'Open', 'order status, ie: open order in cart', 'woocommerce-pos' ),  
  291. 'change' => _x( 'Change', 'Money returned from cash sale', 'woocommerce-pos' ),  
  292. 'support-form' => __( 'Support Form', 'woocommerce-pos' ),  
  293. /** translators: woocommerce */ 
  294. 'system-status' => __( 'System Status', 'woocommerce' ),  
  295. ),  
  296. 'buttons' => array( 
  297. /** translators: woocommerce */ 
  298. 'checkout' => __( 'Checkout', 'woocommerce' ),  
  299. 'clear' => _x( 'Clear', 'system status: delete local records', 'woocommerce-pos' ),  
  300. /** translators: woocommerce */ 
  301. 'close' => __( 'Close' ),  
  302. /** translators: woocommerce */ 
  303. 'coupon' => __( 'Coupon', 'woocommerce' ),  
  304. 'discount' => __( 'Discount', 'woocommerce-pos' ),  
  305. /** translators: wordpress */ 
  306. 'email' => __( 'Email' ),  
  307. /** translators: woocommerce */ 
  308. 'fee' => __( 'Fee', 'woocommerce' ),  
  309. /** translators: woocommerce */ 
  310. 'new-order' => __( 'New Order', 'woocommerce' ),  
  311. /** translators: woocommerce */ 
  312. 'note' => __( 'Note', 'woocommerce' ),  
  313. /** translators: wordpress */ 
  314. 'print' => __( 'Print' ),  
  315. 'process-payment' => __( 'Process Payment', 'woocommerce-pos' ),  
  316. /** translators: wordpress */ 
  317. 'refresh' => __( 'Refresh' ),  
  318. 'restore' => _x( 'Restore defaults', 'restore default settings', 'woocommerce-pos' ),  
  319. 'return' => _x( 'return', 'Numpad return key', 'woocommerce-pos' ),  
  320. 'return-to-sale' => __( 'Return to Sale', 'woocommerce-pos' ),  
  321. /** translators: woocommerce */ 
  322. 'save' => __( 'Save Changes', 'woocommerce' ),  
  323. 'send' => __( 'Send', 'woocommerce-pos' ),  
  324. /** translators: woocommerce */ 
  325. 'shipping' => __( 'Shipping', 'woocommerce' ),  
  326. 'void' => __( 'Void', 'woocommerce-pos' ),  
  327. /** translators: woocommerce */ 
  328. 'expand-all' => __( 'Expand all', 'woocommerce' ),  
  329. /** translators: woocommerce */ 
  330. 'close-all' => __( 'Close all', 'woocommerce' ),  
  331. 'legacy' => __( 'Enable legacy server support', 'woocommerce-pos' ),  
  332. ),  
  333. 'messages' => array( 
  334. /** translators: woocommerce */ 
  335. 'choose' => __( 'Choose an option', 'woocommerce' ),  
  336. /** translators: woocommerce */ 
  337. 'error' => __( 'Sorry, there has been an error.', 'woocommerce' ),  
  338. /** translators: woocommerce */ 
  339. 'loading' => __( 'Loading ...' ),  
  340. /** translators: woocommerce */ 
  341. 'success' => __( 'Your changes have been saved.', 'woocommerce' ),  
  342. 'browser' => __( 'Your browser is not supported!', 'woocommerce-pos' ),  
  343. 'legacy' => __( 'Unable to use RESTful HTTP methods', 'woocommerce-pos' ),  
  344. /** translators: woocommerce */ 
  345. 'no-products' => __( 'No Products found', 'woocommerce' ),  
  346. /** translators: woocommerce */ 
  347. 'cart-empty' => __( 'Your cart is currently empty.', 'woocommerce' ),  
  348. 'no-gateway' => __( 'No payment gateways enabled.', 'woocommerce-pos' ),  
  349. /** translators: woocommerce */ 
  350. 'no-customer' => __( 'Customer not found', 'woocommerce' ) 
  351. ),  
  352. 'plural' => array( 
  353. 'records' => _x( 'record |||| records', 'eg: 23 records', 'woocommerce-pos' ),  
  354. ) ); 
  355.  
  356.