joppot

コピペで絶対動く。説明を妥協しない

プログラミング

How to change the language form “en-US” to “en” by wordpres bogo

投稿日:2017年3月29日 更新日:



Abstract

Hello everyone, It’s me candle.
In this time, I will show you to customize bogo plugins.
The bogo is wonderful plugin which can adapt the wordpress site to many languages as a simple.
but, there is a problem that you can’t choose general English.

When writing English articles, you may not always have to write it limited to country.
However, you can choose ‘en-UK’, ‘en-CA’ and ‘en-US’, but ‘en’ can not be chosen in bogo.

I checked the source code.
The bogo got a language list from wordpress function, and there is no general English in it.
So I used this method which select “en-US” language and replace the string to “en” when it is displayed.

It may be fixed in future development.

It is described that it is also possible for google’s official to choose ‘en’ for language.

https://support.google.com/webmasters/answer/189077?hl=en


Related article

Customize wordpress bogo plugin’s short code

Precondition

Bogo plugin has been installed to wordpress.
You can edit wordpress theme.



Find out where en-US is displayed

First we find out where en-US is displayed
Open the web inspector ( development tool ) on the page translated to English in your blog.
Search “en-US” against DOM.

The first thing we can see is the lang value of the html tag.

<!DOCTYPE html>
<!–[if lt IE 7]>
<html class=”ie6″ lang=”en-US”> <![endif]–>
<!–[if IE 7]>
<html class=”i7″ lang=”en-US”> <![endif]–>
<!–[if IE 8]>
<html class=”ie” lang=”en-US”> <![endif]–>
<!–[if gt IE 8]><!–>
<html lang=”en-US”>

Next, we can see the “alternate” value of link tag in the header tag.

<link rel=”alternate” hreflang=”en-US” href=”http://localhost:8080/alice_blog/wp/en/2017/03/26/33/” />
<link rel=”alternate” hreflang=”ja” href=”http://localhost:8080/alice_blog/wp/2016/02/14/23/” />

The last one has en-US with class attribute at body tag, but even if you do not change it there is nothing to do with SEO.
You don’t change it.

In summary, you need to change the value of “lang” in html tag and the value of “alternate” from en-US to en


Chnage the lang value of html tag

For changing “lang” value of html tag, I refer to the following site.
https://wordpress.org/support/topic/how-to-change-the-hreflang-annotation-and-the-lang-attribute-from-en-us-to-en/

Open the functions.php in the your theme folder. If it isn’t, you make it.
It is generally in the following places.

wp-content/themes/”theme name”/functions.php

After open it, write this.

add_filter( 'language_attributes', 'language_attributes_fix' );
function language_attributes_fix( $language_attributes ) {
  return str_replace('en-US','en', $language_attributes);
}

Save it and check the blog again.
It turns to lang=”en”.


Change the value of alternate

Let’s change the value of alternate to en.
Write this code in the same functions.php.

add_filter( 'bogo_rel_alternate_hreflang', 'bogo_rel_alternate_hreflang_fix' );
function bogo_rel_alternate_hreflang_fix($languages) {
 foreach ($languages as $key => $language ) {
   $languages[$key]["hreflang"] = str_replace('en-US', "en", $language["hreflang"]);
 }
 return $languages;
}

To enhance the versatility of this source code, we loop “$languages” with foreach to replace all languages.
If you know where the location of “en-US” in the $languages array, the next code is better performance than before.

function bogo_rel_alternate_hreflang_fix($languages) {
$languages[1][“hreflang”] = str_replace(‘en-US’, “en”, $languages[1][“hreflang”]);
return $languages;
}

Please be careful, if you add or remove the translation language, the “en-US” position would be changed,.

The alternate value turns to en.

It’s over.

Conclusion

The displayed language in this way could be changed from en-US to en.

スポンサードリンク

If you think this article is good, share it please

-プログラミング
-,

執筆者:


comment

Your email address will not be published. Required fields are marked *

関連記事

Using sphinx how to generate documents of python classes and functions

Abstruct Hello everyone it’s me candle. In this article I will explain how to make documents about classes and functions with Sphinx automatically. condition I will explain it using Python 3, but I think that it will probably work with python 2 as well. Prepare test files Create a main.py and dog.py. touch main.py dog.py Write this to the main.py from dog import Dog def main(): dog = Dog() dog.show_name() if __name__ == "__main__": main() Open the dog.py and write it. class Dog(object): def __init__(self, name="bob"): self.name = name def bark(): print("Bow Wow") def show_name(self): print(self.name) def get_name(self): return self.name …

Deploy React to s3 with aws command

Abstract Hello veryone it’s me candle. In this time I will show you how to deploy a react project created by create-react-app to S3. A good thing about React web app is that you can publish the products created by building them as long as you upload them such as html and js files and images to storage. In this article I don’t write these three items. How to get a IAM that can access to S3 Allocate your own domain using Route53 SSL connection of React web app Condition You have aws command Please install the aws command in …

Ruby regex that does not match if there are keywords that you don’t want included in the string

Abstract Hello everyone it’s me candle. In this time I would like to create a regular expression that does not pass if keywords that I do not want included in the string match. This is such case like this. For example, the regex matches these words “Bitcoin” and “Bitcoin ~” but doesn’t match “BitcoinCash” and “Bitcoin Cash”. ○ Bitcoin ○ Bitcoin Core × BitcoinCash × Bitcoin Cash Condition Nothing Write the regex This is a regex that does not match if there are keywords that you do not want included in the string. /Keyword to include(?!Keywords you do not want …

How to fix the background scroll of react-modal

Abstract Hello everyone it’s me candle. This time I will solve the react-modal background moving problem when you scroll. Condition You use react Completed sample code If you want to run the sample code actually, you would need to install two libraries before. faker is installed for dummy data generation. yarn add faker react-modal First, I will write the sample code of the completed version. This is described in src/App.js. import React, { Component } from 'react' import Faker from 'faker' import Modal from 'react-modal' Modal.setAppElement('#root') class App extends Component { constructor(props) { super(props) this.state = { users: [], user: …

4 functions of scikit-learn preprocesses data such as machine learning

Abstract Hello every one this is candle. In this time we will prreprocess a data with scikit-learn which is machine learning library of python. We will use scikit-learn called With scikit-learn you can use what is called a converter, and you can convert the input data with fit_transform () method.Since there are many converters, I will introduce the following four converters that are often used in machine learning. Imputer StandardScaler MinMaxScaler OneHotEncorder Condition Python3 scikit-learn 0.19.1 For running sample code, you need numpy aside from these libs.


I work in the venture company as a CTO. I start to write program in University, first I learned java, C++ and PHP. In the company, I'm developing web services by Rails. I do like to automation.