Ensuring your WordPress site displays correctly in Arabic involves more than just translating the text; it also requires switching to a Right-to-Left (RTL) layout. GTranslate makes it easy to handle translations, and with a few additional steps, you can automatically apply RTL styles for Arabic. Here’s how:
Step 1: Ensure Your Theme Supports RTL
Before diving in, make sure your theme supports RTL languages. Most modern WordPress themes come with built-in RTL support. If you’re unsure, check your theme’s documentation or contact the theme developer.
Step 2: Install and Configure GTranslate
Install GTranslate Plugin:
Go to your WordPress dashboard.
Navigate to Plugins > Add New.
Search for GTranslate.
Install and activate the plugin.
Configure GTranslate:
Go to Settings > GTranslate.
Select Arabic from the list of available languages.
Save your settings.
Step 3: Add Custom JavaScript for RTL
To automatically switch to RTL when Arabic is selected, you need to add some custom JavaScript. Here’s how:
Create a JavaScript File:
Access your site’s files via an FTP client or your hosting provider’s file manager.
Navigate to
wp-content/themes/your-theme/
(replaceyour-theme
with the name of your active theme).Create a folder named
js
if it doesn’t exist.Inside the
js
folder, create a file namedcustom-rtl.js
.
Add JavaScript Code:
Open the
custom-rtl.js
file and add the following code:
document.addEventListener(“DOMContentLoaded”, function() {
var lang = document.documentElement.getAttribute(“lang”);
if (lang === “ar”) {
document.body.classList.add(“rtl”);
}
});
3. Enqueue the JavaScript File:
In your theme’s
functions.php
file, add the following PHP code to enqueue your custom JavaScript:
function custom_rtl_script() {
wp_enqueue_script( ‘custom-rtl-script’, get_template_directory_uri() . ‘/js/custom-rtl.js’, array(), null, true );
}
add_action( ‘wp_enqueue_scripts’, ‘custom_rtl_script’ );
Step 4: Add Custom CSS for RTL
You may need to add some custom CSS to handle the RTL layout properly. Here’s an example:
Create an RTL CSS File:
In the same
js
directory, create a file namedrtl.css
.Add your RTL-specific CSS rules, for example:
.rtl {
direction: rtl;
text-align: right;
}
2. Enqueue the CSS File:
In your theme’s
functions.php
file, add the following PHP code to enqueue your custom CSS:
function custom_rtl_styles() {
if ( is_rtl() ) {
wp_enqueue_style( ‘custom-rtl-style’, get_template_directory_uri() . ‘/js/rtl.css’ );
}
}
add_action( ‘wp_enqueue_scripts’, ‘custom_rtl_styles’ );
Step 5: Test Your Site
Preview: Navigate to your site and use GTranslate to switch to Arabic.
Check Layout: Verify that the layout changes to RTL and that all elements display correctly.
By following these steps, you can ensure that your WordPress site automatically switches to an RTL layout when translating to Arabic using GTranslate. This not only improves readability for Arabic-speaking users but also enhances their overall user experience.
For a step-by-step guide on setting up GTranslate for WordPress, you might find this video helpful: