wordpress 国际化

tech2022-07-07  213

wordpress 国际化

WordPress is used to create a variety of types of websites. When building a WordPress theme, you should build it for as large of an audience as possible. That goal also implies that your theme should be ready for sites in different languages. WordPress provides a simple API that you can use in a theme to provide internationalization for it. In this article, you will see how you can make your theme ready for different languages.

WordPress用于创建各种类型的网站。 构建WordPress主题时,您应该为尽可能多的受众构建它。 该目标还意味着您的主题应该已准备就绪,可用于使用不同语言的网站。 WordPress提供了一个简单的API,您可以在主题中使用它来为其提供国际化。 在本文中,您将看到如何使主题适合不同的语言。

如何为不同的语言配置WordPress (How to Configure WordPress for Different Languages)

You can add different languages to your WordPress site. For that, you can download the translation files from the blog of the WordPress translator team. From this page, you can see the various languages whose translations are present, as well as what percentage of the translation is complete. Suppose I want to download the French language. I will go to the French language row, then click on the percentage as shown in the image below.

您可以将不同的语言添加到WordPress网站。 为此,您可以从WordPress翻译团队的博客中下载翻译文件。 在此页面上,您可以看到提供翻译的各种语言,以及完成翻译的百分比。 假设我要下载法语。 我将转到法语行,然后单击下图所示的百分比。

Then, you can click on the WordPress version, and export the .mo file as shown in the image below

然后,您可以单击WordPress版本,然后导出.mo文件,如下图所示。

Once you have downloaded the .mo file, you will have to upload it to the wp-content/languages folder of your WordPress installation. You can then go to the Settings -> General in your WordPress admin. There you should be able to see the language options which you have put in the wp-content/languages folder as shown below in the image. Please select the desired language you want to change the site to and click ‘Save Changes’

下载.mo文件后,您将必须将其上传到WordPress安装的wp-content/languages文件夹中。 然后,您可以在WordPress管理员中转到“设置”->“常规”。 在那里,您应该能够看到已放入wp-content/languages文件夹中的语言选项,如下图所示。 请选择您想要将网站更改为的语言,然后单击“保存更改”

在主题中加载文本域 (Loading the Text Domain in Your Theme)

The first step to internationalization for your theme is to create the theme. You can start, in this example by creating a child theme of the theme twentyseventeen. To create a child theme, first create a folder wp-content/themes/wpinternationlizationtheme. In this folder add the file style.css with the following content:

主题国际化的第一步是创建主题。 在本示例中,您可以通过创建主题二十七的子主题开始。 要创建子主题,请首先创建一个文件夹wp-content/themes/wpinternationlizationtheme 。 在此文件夹中添加文件style.css ,其内容如下:

/* Theme Name: wpinternationlizationtheme Description: Twenty Seventeen Child Theme. Author: Abbas Suterwala Author URI: http://example.com Template: twentyseventeen Version: 1.0.0 Text Domain: wpinternationlizationtheme */

This file defines a child theme with the name wpinternationlizationtheme. This is the child theme of twentyseventeen. In the above, every field is standard fields which we define for a child theme. The field Text Domain is the field which defines a unique name for the text domain of this theme. This theme should load the translation files with this key as the unique identifier.

该文件定义了一个名为wpinternationisationtheme的子主题。 这是二十七岁的孩子的主题。 在上面,每个字段都是我们为子主题定义的标准字段。 “文本域”字段是为该主题的文本域定义唯一名称的字段。 此主题应使用此键作为唯一标识符加载翻译文件。

Create a functions.php with the following code

使用以下代码创建一个functions.php

<?php function wpinternationlizationtheme_enqueue_styles() { $parent_style = 'parent-style'; wp_enqueue_style( $parent_style, get_template_directory_uri() . '/style.css' ); wp_enqueue_style( 'child-style', get_stylesheet_directory_uri() . '/style.css', array( $parent_style ), wp_get_theme()->get('Version') ); } add_action( 'wp_enqueue_scripts', 'wpinternationlizationtheme_enqueue_styles' ); ?>

The above code loads the parent theme (which is twentyseventeen in this case) styles. Then the styles from the child theme are loaded.

上面的代码加载父主题(在本例中为27)样式。 然后,加载子主题的样式。

This allows the child theme styles to be the ones who are loaded last, and can be customized to the needs of the child theme. Now you’ll want to load the text domain, which means indicating where will WordPress search for the translations for this theme. You can load the theme’s text domain using the WordPress function load_theme_textdomain.

这样可以使子主题样式成为最后加载的样式,并且可以根据子主题的需要进行自定义。 现在,您将要加载文本域,这意味着指示WordPress将在何处搜索该主题的翻译。 您可以使用WordPress函数load_theme_textdomain加载主题的文本域。

To do that, add the following code to your functions.php:

为此,将以下代码添加到您的functions.php :

function wpinternationlizationtheme_setup(){ $domain = 'wpinternationlizationtheme'; // wp-content/languages/wpinternationlizationtheme/de_DE.mo load_theme_textdomain( $domain, trailingslashit( WP_LANG_DIR ) . $domain ); // wp-content/themes/wpinternationlizationtheme/languages/de_DE.mo load_theme_textdomain( $domain, get_stylesheet_directory() . '/languages' ); // wp-content/themes/wpinternationlizationtheme/languages/de_DE.mo load_theme_textdomain( $domain, get_template_directory() . '/languages' ); } add_action( 'after_setup_theme', 'wpinternationlizationtheme_setup' );

The above code hooks up to the after_setup_theme action. On that action, you load the text domain for the theme. This is done using the function load_theme_textdomain. This function is setup to look for the .mo files in the following directories:

上面的代码与after_setup_theme操作相关。 在该操作上,您将加载主题的文本域。 这可以通过使用load_theme_textdomain函数来完成。 设置此功能可以在以下目录中查找.mo文件:

- Languages directory - Child theme directory - Parent theme directory

WordPress国际化功能 (WordPress Functions for Internationalization)

Once you have set up the text domain, take a look at the functions you can use for internationalization in WordPress. There are primarily two functions which you can use. The first one is __ . This function takes two arguments, the first being the string and the second the domain. This function then returns the approriate localized string based on the language selected.

设置文本域后,请看一下可用于WordPress国际化的功能。 您主要可以使用两个功能。 第一个是__ 。 此函数有两个参数,第一个是字符串,第二个是域。 然后,此函数根据所选的语言返回适当的本地化字符串。

So if you want to add some text at the end of each post, but also want this to be localized based on the language select, add the following code to your functions.php:

因此,如果您想在每个帖子的末尾添加一些文本,但又希望根据选择的语言对其进行本地化,请将以下代码添加到您的functions.php :

function wpinternationlizationtheme_after($content) { return $content . __('Read more', 'wpinternationlizationtheme'); } add_filter('the_content', 'wpinternationlizationtheme_after');

The other function is _e. This takes the same two arguments as __. This function displays the localized text directly on the page rather than just returning it.

另一个函数是_e 。 这和__接受两个相同的参数。 此功能直接在页面上显示本地化的文本,而不仅仅是返回它。

So, for example, if you wanted to add a footer message which should display localized, then you should create a footer.php with the following content:

因此,例如,如果要添加显示本地化的页脚消息,则应创建具有以下内容的footer.php :

<?php _e('This site is powered by WordPress','wpinternationlizationtheme'); ?>

创建.mo文件 (Creating the .mo Files)

Once you have created the code necessary for internationalization you will want to create the localization files. There are many tools available to create .mo files. In this article, you are going to see one of the popular ones, poedit. You can download poedit for your operating system from https://poedit.net/download.

创建国际化所需的代码后,您将需要创建本地化文件。 有许多工具可用于创建.mo文件。 在本文中,您将看到流行的语言之一poedit 。 您可以从https://poedit.net/download下载适用于您操作系统的poedit。

Once you have downloaded poedit you can select ‘file->New Catalog’ to see the following screen:

下载poedit后,您可以选择“文件->新目录”以查看以下屏幕:

In this screen, you can enter the basic information about the project. The next tab is to give the path of the code which needs to be parsed to find the strings which need to be localized, as shown below:

在此屏幕中,您可以输入有关项目的基本信息。 下一个选项卡是给出需要解析的代码的路径,以查找需要本地化的字符串,如下所示:

The next tab lets you enter the keywords which need to be searched to get all the strings which need localization. As you have already used the two functions __ and _e in the above examples, add those two on this tab.

在下一个选项卡中,您可以输入需要搜索的关键字以获取所有需要本地化的字符串。 在上述示例中,您已经使用了__和_e这两个函数,因此请在此选项卡上添加这两个函数。

Once you have done this, the tool will search all the strings which need localization as shown in the image below.

完成此操作后,该工具将搜索所有需要本地化的字符串,如下图所示。

You can now add the localization for each string and then click ‘Save’ to save the file at wp-content/themes/wpinternationlizationtheme/languages/fr_FR.mo

现在,您可以为每个字符串添加本地化,​​然后单击“保存”以将文件保存在wp-content/themes/wpinternationlizationtheme/languages/fr_FR.mo

Now, finally, if you change the language to ‘French’ you should see your strings localized in French on the main site.

现在,最后,如果您将语言更改为“法语”,则应该在主站点上看到以法语本地化的字符串。

结论 (Conclusion)

WordPress is one of the most popular CMS platforms. You can learn about WordPress theme development with relative ease. The users of sites made on WordPress themes are vast in number and variety. Hence the pressing need for your theme to have a wide range of languages available. In many industries, it’s absolutely necessary for your WordPress site to be able to cater to an audience of different regions and countries.

WordPress是最受欢迎的CMS平台之一。 您可以相对轻松地了解WordPress主题开发 。 以WordPress主题制作的网站的用户数量众多且种类繁多。 因此,迫切需要您的主题具有多种可用的语言。 在许多行业中,WordPress网站必须能够迎合不同地区和国家/地区的受众,这是绝对必要的。

Making your theme ready for internationalization can be a key factor for success. The WordPress API for internationalization is easy to use. It makes it very easy to localize your theme without changing any of the code files. So, have fun internationalizing your next WordPress theme, and tell us about your experiences in the comments below!

使您的主题为国际化做好准备可能是成功的关键因素。 用于国际化的WordPress API易于使用。 这使得在不更改任何代码文件的情况下本地化主题变得非常容易。 因此,乐于将您的下一个WordPress主题国际化,并在下面的评论中告诉我们您的经历!

翻译自: https://www.sitepoint.com/internationalization-wordpress-theme/

wordpress 国际化

最新回复(0)