Formatted fields in language translation strings: Difference between revisions
From Joomla! Documentation
AlexSmirnov (talk | contribs) No edit summary |
Several markup changes. |
||
Line 3: | Line 3: | ||
<translate> | <translate> | ||
<!--T:1--> | <!--T:1--> | ||
Sometimes it is necessary to include specially formatted fields within a string to be translated. This usually happens where numbers are involved but can occur for dates and times or when precise formatting instructions are required. If the strings were not to be translated, the standard PHP functions ''printf'' and ''sprintf'' could be used. The ''printf'' function outputs a string formatted using embedded formatting instructions | Sometimes it is necessary to include specially formatted fields within a string to be translated. This usually happens where numbers are involved but can occur for dates and times or when precise formatting instructions are required. If the strings were not to be translated, the standard PHP functions ''printf'' and ''sprintf'' could be used. The ''printf'' function outputs a string formatted using embedded formatting instructions. The ''sprintf'' function returns a string formatted using the same embedded formatting instructions. | ||
</translate> | </translate> | ||
<translate> | <translate> | ||
<!--T:2--> | <!--T:2--> | ||
The JText class provides wrapper methods for the ''printf'' and ''sprintf'' functions allowing static text to be translated while also allowing formatted fields to be embedded using the same syntax as the PHP functions. | The ''JText'' class provides wrapper methods for the ''printf'' and ''sprintf'' functions allowing static text to be translated while also allowing formatted fields to be embedded using the same syntax as the PHP functions. | ||
</translate> | </translate> | ||
<translate> | <translate> | ||
<!--T:3--> | <!--T:3--> | ||
For example, suppose you have the string | For example, suppose you have the string ''Donations of 12.45 GBP have been received'' where the amount comes from a variable, ''$donations''. You could split the string into two: | ||
</translate> | </translate> | ||
< | <syntaxhighlight lang="php"> | ||
JText::_( 'Donations of' ) . “ $donations GBP “ . JText::_( 'have been received' ) | JText::_( 'Donations of' ) . “ $donations GBP “ . JText::_( 'have been received' ) | ||
</ | </syntaxhighlight> | ||
<translate> | <translate> | ||
Line 25: | Line 25: | ||
</translate> | </translate> | ||
< | <syntaxhighlight lang="ini"> | ||
DONATIONS OF=Donations of | DONATIONS OF=Donations of | ||
HAVE BEEN RECEIVED=have been received | HAVE BEEN RECEIVED=have been received | ||
</ | </syntaxhighlight> | ||
<translate> | <translate> | ||
<!--T:5--> | <!--T:5--> | ||
This does not work well in languages where the embedded data is not in a similar place in the translated string. Instead use the ''sprintf'' method: | |||
</translate> | </translate> | ||
< | <syntaxhighlight lang="php"> | ||
JText::sprintf( 'Donations have been received', $donations ) | JText::sprintf( 'Donations have been received', $donations ) | ||
</ | </syntaxhighlight> | ||
<translate> | <translate> | ||
Line 44: | Line 44: | ||
</translate> | </translate> | ||
< | <syntaxhighlight lang="ini"> | ||
DONATIONS HAVE BEEN RECEIVED=Donations of %.2f GBP have been received | DONATIONS HAVE BEEN RECEIVED=Donations of %.2f GBP have been received | ||
</ | </syntaxhighlight> | ||
<translate> | <translate> | ||
<!--T:7--> | <!--T:7--> | ||
You can include more than one format specifier in a translation string. Substitutions are carried out in order so this works as expected | You can include more than one format specifier in a translation string. Substitutions are carried out in order so this works as expected: | ||
</translate> | </translate> | ||
< | <syntaxhighlight lang="php"> | ||
JText::sprintf( 'String with numbers in it', $num1, $num2, $num3 ) | JText::sprintf( 'String with numbers in it', $num1, $num2, $num3 ) | ||
</ | </syntaxhighlight> | ||
<translate> | <translate> | ||
Line 62: | Line 62: | ||
</translate> | </translate> | ||
< | <syntaxhighlight lang="ini"> | ||
STRING WITH NUMBERS IN IT=First %d, second %d, third %d | STRING WITH NUMBERS IN IT=First %d, second %d, third %d | ||
</ | </syntaxhighlight> | ||
<translate>=== Syntax of | <translate>=== Syntax of Format Specifiers === <!--T:9--></translate> | ||
<translate> | <translate> | ||
<!--T:10--> | <!--T:10--> | ||
The format specifier consists of a percent sign (''%'') | The format specifier consists of a percent sign (''%'') followed by one or more of these elements, in order: | ||
</translate> | </translate> | ||
{| class="wikitable" | {| class="wikitable" | ||
Line 102: | Line 102: | ||
<!--T:56--> | <!--T:56--> | ||
<nowiki>or | <nowiki>or <char></nowiki></translate> | ||
| <translate><!--T:20--> | | <translate><!--T:20--> | ||
Optional. Character to be used for padding the results to the correct string size. May be a space character or a ''0'' (zero character). The default is to pad with spaces. An alternative padding character can be specified by prefixing it with a single quote | Optional. Character to be used for padding the results to the correct string size. May be a space character or a ''0'' (zero character). The default is to pad with spaces. An alternative padding character can be specified by prefixing it with a single quote character.</translate> | ||
|- | |- | ||
Line 114: | Line 115: | ||
or -</nowiki></translate> | or -</nowiki></translate> | ||
| <translate><!--T:22--> | | <translate><!--T:22--> | ||
Optional. Determines if the result should be left-justified or right-justified. The default is right-justified; a ''-'' character here will make it left-justified.</translate> | Optional. Determines if the result should be left-justified or right-justified. The default is right-justified; a ''-'' character here will make it left-justified.</translate> | ||
|- | |- | ||
Line 123: | Line 124: | ||
Number</translate> | Number</translate> | ||
| <translate><!--T:25--> | | <translate><!--T:25--> | ||
Optional. Number of characters (minimum) that the conversion should result in.</translate> | Optional. Number of characters (minimum) that the conversion should result in.</translate> | ||
|- | |- | ||
Line 132: | Line 133: | ||
Number</translate> | Number</translate> | ||
| <translate><!--T:28--> | | <translate><!--T:28--> | ||
Optional. Number of decimal digits that should be displayed for floating-point numbers. When using this specifier on a string, it acts as a cutoff point, setting a maximum character limit | Optional. Number of decimal digits that should be displayed for floating-point numbers. When using this specifier on a string, it acts as a cutoff point, setting a maximum character limit for the string.</translate> | ||
|- | |- | ||
Line 144: | Line 145: | ||
| <center>%</center> | | <center>%</center> | ||
| <translate><!--T:31--> | | <translate><!--T:31--> | ||
A | A percent character. No argument is required</translate> | ||
|- | |- | ||
Line 203: | Line 204: | ||
|} | |} | ||
<translate> | <translate> | ||
=== Format | === Format Argument Swapping === <!--T:43--> | ||
</translate> | </translate> | ||
<translate> | <translate> | ||
Line 210: | Line 211: | ||
<!--T:45--> | <!--T:45--> | ||
For example, suppose we have | For example, suppose we have: | ||
</translate> | </translate> | ||
< | <syntaxhighlight lang="php"> | ||
echo JText::sprintf( 'Balls in the bucket', $number, $location ); | echo JText::sprintf( 'Balls in the bucket', $number, $location ); | ||
</ | </syntaxhighlight> | ||
<translate> | <translate> | ||
Line 222: | Line 223: | ||
</translate> | </translate> | ||
< | <syntaxhighlight lang="ini"> | ||
BALLS IN THE BUCKET=There are %d balls in the %s | BALLS IN THE BUCKET=There are %d balls in the %s | ||
</ | </syntaxhighlight> | ||
<translate> | <translate> | ||
Line 231: | Line 232: | ||
</translate> | </translate> | ||
< | <syntaxhighlight lang="php"> | ||
$number = 3 | $number = 3 | ||
$location = 'hat' | $location = 'hat' | ||
</ | </syntaxhighlight> | ||
<translate> | <translate> | ||
<!--T:48--> | <!--T:48--> | ||
this would output | this would output ''There are 3 balls in the hat''. But consider if you wanted to change the translation to: | ||
</translate> | </translate> | ||
< | <syntaxhighlight lang="ini"> | ||
BALLS IN THE BUCKET=The %s contains %d balls | BALLS IN THE BUCKET=The %s contains %d balls | ||
</ | </syntaxhighlight> | ||
<translate> | <translate> | ||
<!--T:49--> | <!--T:49--> | ||
This would now output | This would now output ''The 3 contains hat balls'' which is clearly nonsense. Rather than change the code, you can indicate in the translation string which argument each of the placeholders refer to. Change the translation to: | ||
</translate> | </translate> | ||
< | <syntaxhighlight lang="ini"> | ||
BALLS IN THE BUCKET=The %2$s contains %1$d balls | BALLS IN THE BUCKET=The %2$s contains %1$d balls | ||
</ | </syntaxhighlight> | ||
<translate> | <translate> | ||
<!--T:50--> | <!--T:50--> | ||
and the output becomes | and the output becomes ''The hat contains 3 balls'' as expected. | ||
<!--T:51--> | <!--T:51--> | ||
An added benefit of being able to number the arguments is that you can repeat the placeholders without adding more arguments in the code. For example, change the translation to | An added benefit of being able to number the arguments is that you can repeat the placeholders without adding more arguments in the code. For example, change the translation to: | ||
</translate> | </translate> | ||
< | <syntaxhighlight lang="ini"> | ||
BALLS IN THE BUCKET=The %2$s contains %1$d balls, so there are %1$d balls in the %2$s | BALLS IN THE BUCKET=The %2$s contains %1$d balls, so there are %1$d balls in the %2$s | ||
</ | </syntaxhighlight> | ||
<translate> | <translate> | ||
<!--T:52--> | <!--T:52--> | ||
and this will correctly output | and this will correctly output ''The hat contains 3 balls, so there are 3 balls in the hat''. | ||
</translate> | </translate> | ||
Latest revision as of 23:03, 20 October 2022
Sometimes it is necessary to include specially formatted fields within a string to be translated. This usually happens where numbers are involved but can occur for dates and times or when precise formatting instructions are required. If the strings were not to be translated, the standard PHP functions printf and sprintf could be used. The printf function outputs a string formatted using embedded formatting instructions. The sprintf function returns a string formatted using the same embedded formatting instructions.
The JText class provides wrapper methods for the printf and sprintf functions allowing static text to be translated while also allowing formatted fields to be embedded using the same syntax as the PHP functions.
For example, suppose you have the string Donations of 12.45 GBP have been received where the amount comes from a variable, $donations. You could split the string into two:
JText::_( 'Donations of' ) . “ $donations GBP “ . JText::_( 'have been received' )
with language definition strings
DONATIONS OF=Donations of
HAVE BEEN RECEIVED=have been received
This does not work well in languages where the embedded data is not in a similar place in the translated string. Instead use the sprintf method:
JText::sprintf( 'Donations have been received', $donations )
with language definition string
DONATIONS HAVE BEEN RECEIVED=Donations of %.2f GBP have been received
You can include more than one format specifier in a translation string. Substitutions are carried out in order so this works as expected:
JText::sprintf( 'String with numbers in it', $num1, $num2, $num3 )
with language definition string
STRING WITH NUMBERS IN IT=First %d, second %d, third %d
Syntax of Format Specifiers
The format specifier consists of a percent sign (%) followed by one or more of these elements, in order:
Type | Values | ||
Sign | + or - | Optional. Forces a sign (+ or -) to be used on a number. By default, only the – sign is used on a number if it's negative. This specifier forces positive numbers to have the + sign attached as well. | |
Padding | <space>
or 0 or <char> |
Optional. Character to be used for padding the results to the correct string size. May be a space character or a 0 (zero character). The default is to pad with spaces. An alternative padding character can be specified by prefixing it with a single quote character. | |
Alignment | <null> or - | Optional. Determines if the result should be left-justified or right-justified. The default is right-justified; a - character here will make it left-justified. | |
Width | Number | Optional. Number of characters (minimum) that the conversion should result in. | |
Precision | Number | Optional. Number of decimal digits that should be displayed for floating-point numbers. When using this specifier on a string, it acts as a cutoff point, setting a maximum character limit for the string. | |
Type | Mandatory. The type of the argument data. Possible types are: | ||
A percent character. No argument is required | |||
The argument is treated as an integer and presented as a binary number. | |||
The argument is treated as an integer and presented as the character with that ASCII value. | |||
The argument is treated as an integer and presented as a signed decimal number. | |||
The argument is treated as scientific notation (e.g. 1.2e+2). The precision specifier stands for the number of digits after the decimal point since PHP 5.2.1. In earlier versions, it was taken as the number of significant digits (one less). | |||
The argument is treated as an integer and presented as an unsigned decimal number. | |||
The argument is treated as a float and presented as a floating-point number (locale aware). | |||
The argument is treated as a float and presented as a floating-point number (non-locale aware). | |||
The argument is treated as an integer and presented as an octal number. | |||
The argument is treated and presented as a string. | |||
The argument is treated as an integer and presented as a hexadecimal number (with lowercase letters). | |||
The argument is treated as an integer and presented as a hexadecimal number (with uppercase letters). |
Format Argument Swapping
The format string supports argument numbering and even swapping. This is useful where two or more data items must be embedded in a string but differences in language structure means that the order of use of the data items is not the same.
For example, suppose we have:
echo JText::sprintf( 'Balls in the bucket', $number, $location );
with this language translation string
BALLS IN THE BUCKET=There are %d balls in the %s
Then if
$number = 3
$location = 'hat'
this would output There are 3 balls in the hat. But consider if you wanted to change the translation to:
BALLS IN THE BUCKET=The %s contains %d balls
This would now output The 3 contains hat balls which is clearly nonsense. Rather than change the code, you can indicate in the translation string which argument each of the placeholders refer to. Change the translation to:
BALLS IN THE BUCKET=The %2$s contains %1$d balls
and the output becomes The hat contains 3 balls as expected.
An added benefit of being able to number the arguments is that you can repeat the placeholders without adding more arguments in the code. For example, change the translation to:
BALLS IN THE BUCKET=The %2$s contains %1$d balls, so there are %1$d balls in the %2$s
and this will correctly output The hat contains 3 balls, so there are 3 balls in the hat.