Simple language switching for multilingual sites

日本語ページへ

Although there are popular multilingual plugins such as Bogo and Polylang, I implemented a function in Plugin Load Filter for the simple case of converting a few pages to other languages without changing the permalink.

About the permalink

Many multilingual plugins generate language specific permalinks and allow you to separate URLs, but this plugin doesn’t do anything about permalinks.

Adding or changing anything to the permalink will often cause problems, so it’s safest not to do anything.

So how do you sort out the language? That’s what I thought.

It’s just a matter of leveraging the existing features there!

Use the parent and child pages of Pages.

For example, let’s say you want to create a few pages in English on your Japanese site.

First, create a page that is the root of your English content, with the title English Language Subsite (or whatever), the slug en to match your locale, and the language setting English (United States)

If you are creating a non-English page, set the slug for that language locale.

Some of the content is translated and published in English. (It is better if you can display a list of English content links, etc.)

When you publish this page, the URL of this page will use the slug you set up earlier, http(s)//example.com/en/

From now on, you will create pages in English content as child pages of this parent page.

To create a child page, just specify the parent page (English Language Subsite) that you created in the Parent Page section of Page Attributes.

This will allow you to automatically organize the URLs of your English content so you don’t have to worry about permalinks!

By default, no excerpts or tags will be displayed on Pages. If you need excerpts and tags, you can add the following code to your theme’s functions.php.

//Enable Pages excerpts
function my_setup() {
    add_post_type_support('page', 'excerpt');
}
add_action( 'after_setup_theme', 'my_setup' );

//Enable Pages tags
function my_add_tag_to_page() {
    register_taxonomy_for_object_type('post_tag', 'page');
}
add_action('init', 'my_add_tag_to_page');
 
//Include Pages in the tag archive
function my_add_page_to_tag_archive( $obj ) {
    if ( is_tag() ) {
        $obj->query_vars['post_type'] = array( 'post', 'page' );
    }
}
add_action( 'pre_get_posts', 'my_add_page_to_tag_archive' );

Multilingual features of this plugin

Once again, let’s take a look at what this plugin does.

  • Switching language locales per post
  • Outputting hreflang metadata to the head tag when it’s multilingual

These are the only two things the plugin does, so please decide if this feature alone is sufficient to meet your multilingualization needs.

If you find it lacking in features, we recommend using popular multilingual program!

Switching Language Locales

For example, by default, the Japanese version of WordPress is available in two languages, Japanese and English (United States).

This plugin supports switching language locales per post.

If you want to use another language that is not installed, you will need to install a translation file for another language in your WordPress unit by going to SettingsGeneralSite Language and selecting Available Languages and saving the changes.

A plugin may also include a translation file within the plugin itself, but if it doesn’t, try referring to the translation file page on its plugin page on the wordpress.org site https://translate.wordpress.org/projects/wp-plugins/(plugin slug)/language-packs/
If the translation files for the target language exist, you can download them.

After the above preparations, you will be able to specify the language locale for each post/page from the installed language.

By switching the language locale for each page, the MO translation files for WordPress, plugins and themes will be loaded according to the language.

If you’re using a multilingual theme or plugin, the lang attribute in the head and OGP locale information should also be automatically switched.

hreflang metadata output

Another feature is the hreflang output feature to prevent duplicate content in SEO

For example, if you have a page in English and the content is only in English, you don’t need this feature, but if the original content is in Japanese and you have translated it into English, it is essentially the same content in a different language.

Use the hreflang metadata to correctly tell Google to find Japanese articles when searching in Japan and English articles when searching in English-speaking countries.

It’s easy to use, just set the original Post ID of the original article in the Original post ID for hreflang field of the translated article.

In this example, the Post ID of the original Post is 2177, and the edit link for the original post will be shown when you save.

Leave Original post ID for hreflang on the edit page side of the original post blank.

Once you’ve created an English page, make sure that both the English and Japanese pages output the following hreflang (you can check your browser’s page source display, for example)

<link rel="alternate" hreflang="en_US"  href="http://localhost/wordpress/en/plugin-load-filter-addon/" />
<link rel="alternate" hreflang="ja"  href="http://localhost/wordpress/plugin-load-filter-addon/" />

 

If you are using the cache plugin, please stop it and check.

What this plugin can’t do

It doesn’t have the following features like the popular multilingual plugins.

  • Translation of Site Title and Site Description
  • Translation of the menu
  • Automatic content translation
  • Translation of archives, search pages, etc.
  • Language switching display on the front page (shortcode/widget)
  • Language display settings for widgets

Disappointing ?

If you think it’s surprisingly useful, give it a try!

If you can use PHP to some extent, you can do a lot of things pretty freely by preparing template files for other language pages.

For example, you can output your site title and site descriptor directly from the template for that language, or you can create and call a separate menu for that language.

About templates for other languages

In order for the created template to be selectable on a per-post basis, the following description must be included in the template file.

For example, if you create a template file named singular-en.php and insert the following description at the beginning of the file, you will be able to select the created template from Page Attributes on the editing screen.

<?php
/**
 * The template for displaying english locale posts.
 *
 * Template Name: one column english locale page
 * Template Post Type: post, page
 *
 * @file    singular-en.php
 * @author  xxxxxxxx 
 * @version Release: 1.0.0
 */

You can select a template for a post or a fixed page by setting Template Name to the name of the template and specifying Template Post Type as post and page. Don’t forget to write these two lines as they are very important!

This is the introduction of the language switching feature implemented in Plugin Load Filter for building multilingual sites.

go-to-top