NB: Make sure you do a back up of your theme, files and database before attempting the tutorials
Last modified : Sep 03 2022
Estimated reading time : 2 minutes, 36 seconds - 185 words
Share the post "Configuring Language Files in a WordPress Plugin"
With this tutorial, we will show you how to configure language files in a WordPress plugin.
If it’s not already the case, first we need to adapt our WordPress’ core file this way:
Your file can be index.php for example, but it needs to be in the root folder of your WordPress plugin. We cannot add all details in a WordPress core plugin. Edit it with your parameters of your WordPress plugin.
/* Plugin Name: Le nom du Plugin Plugin URI: https:www.mondomaine.com/ Description: Explication en une phrase de votre plugin et de ses caractéristiques. Version: La version actuelle (Ex:2.0) Author: Votre Nom ou nom d'auteur Author URI: L'adresse du site Web de l'auteur */
By adding the following code snippet, we will also secure your file:
if ( ! defined( 'ABSPATH' ) ) { exit; }
Then we need to create a folder called languages where we will include all of our .mo and .po files for the required languages. Afterwards, we need to create a new function which will execute the language files:
/**************************************** Load folders Language ************************************/ function via_woocommerce_classement_load_plugin_textdomain() { $domain = 'woocommerce-classement'; $locale = apply_filters( 'plugin_locale', get_locale(), $domain ); if ( $loaded = load_textdomain( $domain, trailingslashit( WP_LANG_DIR ) . $domain . '/' . $domain . '-' . $locale . '.mo' ) ) { return $loaded; } else { load_plugin_textdomain( $domain, FALSE, basename( dirname( __FILE__ ) ) . '/languages/' ); } } add_action( 'init', 'via_woocommerce_classement_load_plugin_textdomain' );
In the function, we can see that the variable $domain = ‘woocommerce-classement’; is storing the plugin name. It is vital to change it by the name of the WordPress plugin folder. Finally, make sure that your language folder exists.
Now all that there’s left to do is edit your language files with Poedit.
Here’s how the code snippet should look:
<?php /* Plugin Name: Le nom du Plugin Plugin URI: https:www.mondomaine.com/ Description: Explication en une phrase de votre plugin et de ses caractéristiques. Version: La version actuelle (Ex:2.0) Author: Votre Nom ou nom d'auteur Author URI: L'adresse du site Web de l'auteur */ if ( ! defined( 'ABSPATH' ) ) { exit; } /**************************************** Load folders Language ************************************/ function via_woocommerce_classement_load_plugin_textdomain() { $domain = 'woocommerce-classement'; $locale = apply_filters( 'plugin_locale', get_locale(), $domain ); if ( $loaded = load_textdomain( $domain, trailingslashit( WP_LANG_DIR ) . $domain . '/' . $domain . '-' . $locale . '.mo' ) ) { return $loaded; } else { load_plugin_textdomain( $domain, FALSE, basename( dirname( __FILE__ ) ) . '/languages/' ); } } add_action( 'init', 'via_woocommerce_classement_load_plugin_textdomain' );