Deciding What Text to Convert
Now comes the hard part: figuring out what needs to be converted. My recommendation would be not to go overboard and try to translate everything. However, there are some text elements that should be translated; these include:
- Admin bar menu and sub-menu titles and corresponding admin page titles
- Admin page menu tab titles
- Error messages
Determining Text for Translation
There are two principal PHP functions that are involved in translating text: ’ __( )’ and ‘_e( )’. Any text that is to be translated is passed to one of the two functions and the translated text is returned.
- ‘__( )’ translates the text and returns the translated text as the result of the function call.
- ‘_e( )’ translates the text and echoes the translated text as per the PHP ‘echo’ statement
Both of these functions have two parameters: the first is the text to be translated and the second if the namespace defined in the textdomain function call.
1 | __( ‘Settings’, ‘avp-unicode-charkbd’ ) |
The ‘__( )’ function here is used as part of a larger statement, a function call where the returned translated text is passed to the parent function call.
1 | _e( ‘Font’, ‘avp-unicode-charkbd’ ); |
The ‘_e( )’ function here is used to echo the translated text directly to the output channel and is used in combination with other ‘echo’ statements or, when surrounded by ‘<?php >?’ statements, directly within the HTML code inside the PHP file.
Dealing with Plural Entries
In situations where singular or plural text messages are possible, there is an additional function call to handle them, as in some languages there can be considerable difference between them. This is where the ‘_n( )’ function is used.
The function ‘_n( )’ has four parameters: the first two are the text to be translated, singular text and plural text, in that order; the next parameter is the count value which determines whether to present the singular or plural text; and the final parameter is the namespace.
1 | _n( ‘Character set’, ‘Character sets’, count( $Array ), ‘avp-unicode-charkbd’ ); |
In the example above, whether the singular or plural version of the translated text is determined by the value ‘count( )’ function, which counts the number of character sets in the variable $Array.
Personal Links