Usage and difference of __ (), _e (), _x, _EX and _n in WordPress translation

Source: Internet
Author: User

compiler function

WordPress uses the following functions to facilitate language localization.

    • __()
    • _e()
    • _x()
    • _ex()
    • _n()

The functions listed above are used to contain the strings of the required translations, depending on the different arguments and output types of the string, different functions are required. I believe there are many friends still do not understand the differences and usage of these functions, the following initiative to come to the details of the said.

__ () and _e ()

__ () and _e () are used to return the string content corresponding to the current language . Take a look at the following example:

Use __ ()

    1. <?php  
    2. If(Is_single(   
    3.     echo __);   
    4. }  
    5. ?>

Using _e ()

    1. <?php  
    2. If(Is_single(   
    3.     _e);   
    4. }  
    5. ?>

The final output of the above two sets of code is the same. Compare the 3rd line of the two sets of code, use the Echo function, and use __ () to return the content directly with _e (). As a result, we can simply understand:

If the string is returned to another function call, do not print it out, use __ (); Print the string directly to the HTML, using _e ().

Let's look at the following example:

    1. <?php  
    2. The_content(__)   
    3. ?>

' Click here to read more ' is directly wrapped by the function the_content (), so use __ () here, if you switch to _e (), it will directly output "Click here to read more", function the_content () will not work.

_x () and _EX ()

if the string to be translated is determined by context, you need to use _x (). For example, "post" can refer to "a post(noun)" or "to post (verb)" depending on the context. You need to clearly express the different meanings of the same word to facilitate the translator's identification. _x () is mostly used in a single word with multiple usages, and it has an extra parameter than the other compiler functions, which is a different content to display in different contexts.

For example, you use the word "post" in the two places of the subject, but the two places have a different meaning. At this point, you can do this:

Post in the first place uses the following code:

    1. echo _x(' post ',' A post. ') ?>    

In the second post use the following code:

    1. echo _x(' post ',' to post. ') ?>    

Then, the translator can understand the difference between the two post, the first translation into an article, the second translation of the "release."

Note: the exported. Pot Language Pack files, when opened using the POEdit software, you will find that there are two translation entries: Post[a post] and post[to post], so you can translate the two meanings separately.

_EX () is different from _x (), and _e () is different from __ (). The former is a string used to print output to HTML directly, which is used to return strings for other functions to call, without printing output.

_n ()

_n () is used for single and complex compilation . More common in the WordPress comment function module. For example, the following two sets of code output the same content:

  1. <?php  
  2. If(Get_comments_number(1    
  3.     _e);   
  4. {  
  5.     _e);   
  6. }  
  7. ?>

    1. <?php  
    2. echo _n, Get_comments_number()    
    3. ?>

The first set of code is judged by if, if the number of comments is 1, the output "there is a comment", otherwise the output "there is comments", because it is directly output, so use _e (); The second group uses the Echo function output and then uses the _n () To differentiate the display of 1 comments and multiple reviews.

_n () has 3 parameters. The first is a singular string, the second is a plural string, and the third is a quoted number . In this example, Get_comments_number () is used to get the number of comments that are available to _n ().

Translations containing variables

What should we do if we include an extra function or variable in the translated string? For example, the following:

    1. <?php  
    2. = The_color();    
    3. _e);   
    4. ?>

If you make a. pot file through POEdit, you will get an error. Because $color this variable is included in the translated string. So, how to circumvent this problem? Here are 2 ways to do this:

Split content
    1. <?php  
    2. = The_color();    
    3. echo __. __)   
    4. ?>

Look carefully at the 3rd line of code, using the Echo function, so that the output of $color variables can be guaranteed, while the variables at both ends of the content of the compilation, the middle of the use of the dot "." Connected. That way, you just need to translate the content on both ends.

Use printf()Or sprintf()

The above-mentioned "split content" method, although it can achieve the effect we want, but it divides the content into a few paragraphs, we need to translate separately, how much more cumbersome. In fact, we can use printf() or sprintf() to solve this problem. Look at the following code:

  1. <?php  
  2. Use the sprintf () notation
  3. sprintf(__, The_color();    
  4. printf(__, The_color()     
  5. ?>

As you can see,%s is used in the translated content, and the translated content and variable functions are wrapped in printf() or sprintf() functions. In this example, only one variable is used, what if more functions are changed? This requires you to understand or both printf() sprintf() of these functions.

printf()The difference from sprintf() usage is the same as the difference between _e () and __ ().

Usage and difference of __ (), _e (), _x, _EX and _n in WordPress translation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.