STATUS ÜBERPRÜFEN
I AM LISTENING TO
|

Mix languages in a WordPress post / pages with a simple shortcode and gettext

5. Februar 2015
.SHARE

Table of Contents

In a current project I am using pages as templates, to load content into specific containers on the website. The website will be using up to 15 different languages.

The main language setup is done through WPML, which creates linked content per language. This helps, as some languages will fail terribly when translating them through a pure gettext setup, as the words setup is often completely different.

But there is always static stuff that can be translated directly through the language files. For that I use a simple shortcode that allows me to keep the native language as a basis in all linked content. Easier to move around, as I can actually read and understand it :)

Call the shortcode from the functions.php

add_shortcode('quick_trans','do_quick_trans');

The shortcode function itself. This loads the language textdomain from the specified location and translates the string.

function do_quick_trans($atts, $content) {
 extract(shortcode_atts(array(
    'location' => 'PATH_TO_LANG_FILE',
    'lang' => 'LANG_TO_USE',
'textdomain' => 'TEXTDOMAIN'
 ), $atts));

load_textdomain($textdomain, $location.$lang.'.mo'); 
$content = __($content, $textdomain);

return $content;

 }

How to use it in your post or page content

German

[quick_trans lang=“de_DE“]Simple is nice![/quick_trans]

Spanish

[quick_trans lang=“es_ES“]Simple is nice![/quick_trans]

Spanish – Latin America / with a special path for the lang file location [quick_trans lang=“es_LA“ location=“/wp-content/…“]Simple is nice![/quick_trans]

Let’s Talk!

Looking for a reliable partner to bring your project to the next level? Whether it’s development, design, security, or ongoing support—I’d love to chat and see how I can help.

Get in touch,
and let’s create something amazing together!

RELATED POSTS

Here’s the thing about the macOS menu bar: Apple gives you zero control over it. Your apps just pile in from the right, squeezing together like commuters on a rush-hour train, and you either live with it or you don’t. There’s no padding, no grouping, no breathing room. Just a wall of tiny icons staring […]

PDF operations are one of those recurring pain points that never fully go away. You need to fill a contract template, strip and rewrite document metadata before archiving, generate an invoice from an HTML template, and stamp every page with a branded header. The default answer is a SaaS API subscription that charges per document […]

Generating PDFs on a server is one of those tasks that sounds simple until you actually sit down to do it. HTML-to-PDF rendering drifts between browsers, LibreOffice headless mode is finicky to install, and most SaaS solutions charge per page once you hit volume. Gotenberg solves this cleanly: a single Docker container that bundles headless […]

Alexander

I am a full-stack developer. My expertise include:

  • Server, Network and Hosting Environments
  • Data Modeling / Import / Export
  • Business Logic
  • API Layer / Action layer / MVC
  • User Interfaces
  • User Experience
  • Understand what the customer and the business needs


I have a deep passion for programming, design, and server architecture—each of these fuels my creativity, and I wouldn’t feel complete without them.

With a broad range of interests, I’m always exploring new technologies and expanding my knowledge wherever needed. The tech world evolves rapidly, and I love staying ahead by embracing the latest innovations.

Beyond technology, I value peace and surround myself with like-minded individuals.

I firmly believe in the principle: Help others, and help will find its way back to you when you need it.