Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[WordPress] Exclude text domains from language files by wrapping them into custom functions instead of default l10n functions. (http://glck.be/6651/)
<?php
/* String from your theme/plugin */
_e( 'Margaritas!', 'your-textdomain' );
__( 'Margaritas!', 'your-textdomain' );
/* Original string from WooCommerce: */
_e( 'Product', 'woocommerce' );
__( 'Product', 'woocommerce' );
<?php
/* In your own theme/plugin replace with: */
wcl10n_e( 'Product', 'woocommerce' );
wcl10n__( 'Product', 'woocommerce' );
<?php
/**
* Wrappers for default l10n functions to include in your theme or plugin.
*/
if ( ! function_exists( 'wcl10n__' ) ) {
/**
* Wrapper for __() gettext function.
* @param string $string Translatable text string
* @param string $textdomain Text domain, default: woocommerce
* @return void
*/
function wcl10n__( $string, $textdomain = 'woocommerce' ) {
return __( $string, $textdomain );
}
}
if ( ! function_exists( 'wcl10n_e' ) ) {
/**
* Wrapper for _e() gettext function.
* @param string $string Translatable text string
* @param string $textdomain Text domain, default: woocommerce
* @return void
*/
function wcl10n_e( $string, $textdomain = 'woocommerce' ) {
echo __( $string, $textdomain );
}
}
/* repeat for other l10n function, see wp-includes/l10n.php */
@swissspidy

This comment has been minimized.

Copy link

@swissspidy swissspidy commented May 27, 2015

The _e function in WordPress echoes the string, so you can't return it. This means your second example here doesn't quite work as expected. Use echo __( $string, $textdomain ) instead.

@glueckpress

This comment has been minimized.

Copy link
Owner Author

@glueckpress glueckpress commented Jul 27, 2015

Bummer, thanks. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment