Translate your WordPress Site for Each Users
- Article
- Comment
Translate your WordPress Site for Each Users. Translating a WordPress site based on User choice is really easy with your theme development, Here Let me explain you step by step to make your WordPress site perfectly viewed based on your registered users preferred language.
we need to create language files such as .PO and .MO files for translating from one language to another. Here I am taking Tamil(தமிழ்)`ta_IN` as my customization language to make it work on WordPress now.
Steps :
- Download POEdit From here.
- Install and create your own PO and MO files by using it.
After compiling PO and MO files you are almost ready to apply it on live. now, you have to upload it on the current active themes directory by creating a separate folder.
Here I made a folder “languages” and uploaded mine files.
Now, you need to write all your theme words like this.
__( 'Next', 'Kvcodes' )
__( 'Previous', 'Kvcodes' )
Now, you can verify whether the language is working or not.
define("WPLANG", "ta_IN");
Place this code on wp-config.php. And write the following function on theme `functions.php`.
/***************************************** *Kvcodes Languages - Setup ****************************************/ if ( ! function_exists( 'kvcodes_setup' ) ) : function kvcodes_setup() { load_theme_textdomain( 'kvcodes', get_template_directory() . '/languages' ); } endif; // kvcodes_setup
Than you can check it on your browser. This is constant method, which will not help you to create translations based on users.
Here is the function for language hook. Just undo the `wp-config.php` added line. Than add the following function on `functions.php`.
add_filter( 'locale', 'kvcodes_default_language' ); function kvcodes_default_language( $locale ){ return 'ta_IN'; }
The above one will hook the Tamil language in default. you have to create a drop down on registered users profile page and ask them to select their language. And than save it as user meta. You can retrieve the language code and hook the language file based on the users choice. So it will work for every users.