How to Style Your Website Based on Language with GTranslate
Content
Many WordPress users who rely on the 'Translate WordPress with GTranslate' plugin want to customize their site's appearance for different languages. A common request is to change fonts, colors, or layouts based on the active language. This guide explains how to achieve this using CSS.
Why Style by Language?
Different languages can have unique typographic needs. For instance, Arabic script often requires a specific font family and a right-to-left (RTL) layout for optimal readability. The free version of the GTranslate plugin does not add a specific CSS class to the <body> tag for each language. However, it provides a powerful, standard alternative.
The Solution: Use the HTML Lang Attribute
The plugin dynamically changes the lang attribute of your site's <html> tag. This attribute is a reliable and semantic way to target specific languages with CSS. You can use it to create style rules that only apply when a particular language is active.
CSS Examples:
Here is how you can use the lang attribute in your CSS to style content for different languages:
/* Change the font for Arabic language */
html[lang="ar"] body {
font-family: 'Your Arabic Font', sans-serif;
}
/* Change the font for Spanish language */
html[lang="es"] body {
font-family: 'Your Spanish Font', serif;
}
/* Apply a background color for French */
html[lang="fr"] .my-section {
background-color: #f0f0f0;
}
How to Implement This
- Identify the Language Code: Find the two-letter code for the language you want to target (e.g., "es" for Spanish, "de" for German).
- Add Custom CSS: Navigate to your WordPress Customizer (Appearance > Customize) or your theme's settings page. Look for a "Custom CSS" or "Additional CSS" section.
- Paste the Code: Enter your CSS rules using the format shown in the examples above.
- Publish: Save and publish your changes.
Troubleshooting and Limitations
- Cache Issues: If your changes aren't appearing, clear your WordPress cache and your browser cache.
- Theme Specificity: Your theme's existing CSS might be overriding your new rules. You may need to use more specific CSS selectors or add the
!importantdeclaration (use this sparingly). - RTL Layouts: Simply changing the font for RTL languages like Arabic or Hebrew is often not enough. Proper RTL support requires a theme that can handle layout mirroring. The 'Translate WordPress with GTranslate' team has stated that full RTL/LTR switching is a feature of their paid version, as it involves complex changes beyond just a CSS direction rule.
By leveraging the html[lang] CSS selector, you can effectively create a multi-lingual design that adapts to your visitors' chosen language.
Related Support Threads Support
-
Set Albanian Language by default to websitehttps://wordpress.org/support/topic/set-albanian-language-by-default-to-website/
-
Page redirection.https://wordpress.org/support/topic/page-redirection-10/
-
About classhttps://wordpress.org/support/topic/about-class/
-
How to change image in second language front pagehttps://wordpress.org/support/topic/how-to-change-image-in-second-language-front-page/
-
Sharing links with the translationhttps://wordpress.org/support/topic/sharing-links-with-the-translation/
-
Page direction support for ltr/rtl languageshttps://wordpress.org/support/topic/page-direction-support-for-ltr-rtl-languages-2/
-
Gtranslate hookhttps://wordpress.org/support/topic/gtranslate-hook/
-
Images Not Loadinghttps://wordpress.org/support/topic/images-not-loading-170/
-
Gtranslate CSS ISSUEhttps://wordpress.org/support/topic/gtranslate-css-issue/
-
Content Display Right to Lefthttps://wordpress.org/support/topic/content-display-right-to-left/
-
Programmatically trigger re-scan of page?https://wordpress.org/support/topic/programmatically-trigger-re-scan-of-page/
-
I want Chinese to be autoselected when opening our websitehttps://wordpress.org/support/topic/i-want-chinese-to-be-autoselected-when-opening-our-website/
-
Page Loading Issue with Language Selectorhttps://wordpress.org/support/topic/page-loading-issue-with-language-selector/
-
2 shortcode on same sitehttps://wordpress.org/support/topic/2-shortcode-on-same-site/
-
Page in Spanish and plugins in Englishhttps://wordpress.org/support/topic/page-in-spanish-and-plugins-in-english-2/
-
Site is not translate first after document.cookie = ‘googtrans=/en/ar’;https://wordpress.org/support/topic/site-is-not-translate-first-after-document-cookie-googtrans-en-ar/
-
Need Current Language in my Template filehttps://wordpress.org/support/topic/need-current-language-in-my-template-file/
-
Date pickerhttps://wordpress.org/support/topic/date-picker-50/
-
Detect language in wordpress templatehttps://wordpress.org/support/topic/detect-language-in-wordpress-template/
-
Slow site in other countrieshttps://wordpress.org/support/topic/slow-site-in-other-countries/
-
Translation not workinghttps://wordpress.org/support/topic/translation-not-working-145/
-
I wanted to translate the site while first load. Now it is in English .https://wordpress.org/support/topic/i-wanted-to-translate-the-site-while-first-load-now-it-is-in-english/
-
Html class to add which the javascript/plugin will not translatehttps://wordpress.org/support/topic/html-class-to-add-which-the-javascript-plugin-will-not-translate/
-
Custom Text Link to change languagehttps://wordpress.org/support/topic/custom-text-link-to-change-language/
-
How to hide Google translate logo with loading sign?https://wordpress.org/support/topic/how-to-hide-google-translate-logo-with-loading-sign/
-
I am triggering an Arabic anchor tag on the page load using jQuery.https://wordpress.org/support/topic/i-am-triggering-an-arabic-anchor-tag-on-the-page-load-using-jquery/
-
Need to change the Font family for Different languagehttps://wordpress.org/support/topic/need-to-change-the-font-family-for-different-language/
-
Gtranslate to change html code when changing languagehttps://wordpress.org/support/topic/gtranslate-to-change-html-code-when-changing-language/
-
load popup on page loadhttps://wordpress.org/support/topic/load-popup-on-page-load/
-
Different font for different language in GTranslatehttps://wordpress.org/support/topic/different-font-for-different-language-in-gtranslate/
-
Header Expanding with the language listhttps://wordpress.org/support/topic/header-expanding-with-the-language-list/
-
How can I detect that language has beenn changedhttps://wordpress.org/support/topic/how-can-i-detect-that-language-has-beenn-changed/
-
Translation takes time when reload new pagehttps://wordpress.org/support/topic/translation-takes-time-when-reload-new-page/