Avoid concatenating parts of strings
-
Hi,
While looking at how to translate your plugin to Swedish, I stumbled about a very bad construction that you currently have in trunk:
'display' => __('Every ' . $mchTask->getRunningInterval() . ' seconds'));
- This construction is incorrect. When the value of $mchTask->getRunningInterval() is injected into the string, you won’t get any hit in the translation file anymore. You’ve also forgotten to include the text-domain parameter, which should be equal to your plugin slug (‘ultra-community’).
- You can’t take for granted that other languages use exactly the same structure as English.
- Also: You should avoid having strings that begin or end with spaces. In our translation platform, these spaces are almost impossible to see. But also: If you feel an urge to have strings that begin or end with spaces, that’s a clear signal that you’re doing something wrong.
In this case, you should:
1. Offer this as ONE string, using printf() and a placeable. The string you give for translation would become'Every %d second'/'Every %d seconds'
2. Actually, in this case you should even use _n() so that the interval value can steer the format of the string.So we end up with something like:
sprintf( _n('Every %d second', 'Every %d seconds', $number, 'ultra-community'), $number );
Note that you need to use $number twice. The first occurrence steers which version of the phrase should be used. In Russian, for instance, each _n() is translated into three versions. The “singular” translation is then used for 1, 21, 31, etc; the “dual” translation is used for 2, 3, 4, 22, 23, 24, 33, etc; and the “plural” version is used for 5-20, 25-30, etc. The second occurrence is what gets injected into the phrase instead of %d. Typically, they should of course always be the same.
Suggested further reading: https://ottopress.com/2012/internationalization-youre-probably-doing-it-wrong/
- The topic ‘Avoid concatenating parts of strings’ is closed to new replies.