Last active
December 14, 2015 21:30
-
-
Save glueckpress/9e5c649cfb492f002ec0 to your computer and use it in GitHub Desktop.
[WordPress] Check whether the active locale is formal and whether a formal WooCommerce translation has been downloaded.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Check whether formal language files exist for WooCommerce. | |
* | |
* - Admin locales are found in: | |
* - WP_LANG_DIR/woocommerce/woocommerce-admin-LOCALE.mo | |
* - WP_LANG_DIR/plugins/woocommerce-admin-LOCALE.mo | |
* | |
* - Front-end/global locales found in: | |
* - WP_LANG_DIR/woocommerce/woocommerce-LOCALE.mo | |
* - woocommerce/i18n/languages/woocommerce-LOCALE.mo (which if not found falls back to:) | |
* - WP_LANG_DIR/plugins/woocommerce-LOCALE.mo | |
* | |
* See: https://github.com/woothemes/woocommerce/blob/2.4.8/woocommerce.php#L306-L330 | |
* | |
* @return bool Whether or not formal MO files are provided by WooCommerce for current locale | |
*/ | |
function wc_maybe_formal_l10n_exists() { | |
$locale = get_locale(); | |
// MO file names for admin and front-end. | |
$wc_mo = array( | |
'admin' => sprintf( 'woocommerce-admin-%s.mo', $locale ), | |
'front' => sprintf( 'woocommerce-%s.mo', $locale ), | |
); | |
// Possible file paths for front-end/global. | |
$wc_mo_paths = array( | |
file_exists( WP_LANG_DIR . '/woocommerce/' . $wc_mo[ 'front' ] ), | |
file_exists( WP_PLUGIN_DIR . 'woocommerce/i18n/languages/' . $wc_mo[ 'front' ] ), | |
file_exists( WP_LANG_DIR . '/plugins/' . $wc_mo[ 'front' ] ), | |
); | |
// Possible file paths for admin. | |
$wc_admin_mo_paths = array( | |
file_exists( WP_LANG_DIR . '/woocommerce/' . $wc_mo[ 'admin' ] ), | |
file_exists( WP_LANG_DIR . '/plugins/' . $wc_mo[ 'admin' ] ), | |
); | |
// Files must exist for admin AND front-end in order to return true here. | |
return ( in_array( true, $wc_mo_paths ) && in_array( true, $wc_admin_mo_paths ) ) ? true : false; | |
} | |
// Visualize via admin notice. | |
add_action( 'admin_notices', function() { | |
$wp_maybe_formal = (bool) strpos( get_locale(), '_formal' ); | |
$wc_maybe_formal_l10n_exists = wc_maybe_formal_l10n_exists(); | |
if ( ! $wp_maybe_formal ) { | |
printf( '<div class="notice notice-info"><p>WordPress’ current locale (%s) is not formal.</p></div>', get_locale() ); | |
} elseif ( $wc_maybe_formal_l10n_exists ) { | |
printf( '<div class="notice notice-success"><p>A formal WooCommerce translation has been downloaded for this locale (%s).</p></div>', get_locale() ); | |
} else { | |
printf( '<div class="notice notice-error"><p>No formal WooCommerce translation has been downloaded for this locale (%s).</p></div>', get_locale() ); | |
} | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment