The discussion at http://groups.drupal.org/node/15177, Addressing misuse of t(), identified several common misuses of the t() function and suggested some solutions.

The documentation on t() already includes some discussion of correct and incorrect usage. Based on that discussion, here is a patch adding further guidance, particularly addressing the complex issue of passing variables through t().

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

markus_petrux’s picture

+1

maartenvg’s picture

Status: Needs review » Needs work

great addition to the documentation!

One tiny remark: 'occured' is spelled as 'occurred', which happened at least twice.

Dries’s picture

Status: Needs work » Needs review

- "these data" has to be "this data"?

- The .ini file example was OK but confused me for a while. I had to read it 2-3 times. It wouldn't hurt to set the stage a bit more. I don't think I know of a single instance of using .ini files for that so it wasn't until I saw all the code snippets that I understood what you were after with your example.

- When you say something is 'incorrect', it is best to explain _why_ it is incorrect or _what_ the consequences are. I recommend adding some additional detail about the 'why' and 'what' -- we want people to understand why something is bad. If you don't understand why it is bad, it is really hard to remember it is bad.

Otherwise, this is a _much_ needed patch. :-)

nedjo’s picture

Thanks for the suggestions! I'll work up a new version.

Re data: technically this word is plural, but we often use it as if it were singular. I'm not sure if we should stick with what's technically correct or use the more common usage.

I realized we don't have anything here on st() and get_t(). I'll add a brief note.

Gábor Hojtsy’s picture

Well, I sit down and wrote this handbook section from the ground up mid-October: http://drupal.org/book/export/html/322729 There are lots of things to talk about, and while Dries calls for extension of docs in the patch, it might be a good idea to link to the online docs as well? (Maybe give it some alias and link to it that way).

Gábor Hojtsy’s picture

BTW the "Let's do better then Drupal core" part of http://drupal.org/node/322732 gives a few tips on how Drupal core could improve on the use of placeholders and translatable text :) So if someone has the time to pick that up and run with it, that would be great as well.

markus_petrux’s picture

@Gábor: These documents are great, but maybe documentation on t() should be linked to the Conding standards docs. My reasoning for this is that knowning how to use t() is essential, and maybe not everyone reads the localization API docs. And it would be nice to see your document in #5 in the handbooks (it is not visible to anonymous users now, access denied).

nedjo’s picture

FileSize
4.69 KB

New patch with the following changes to address comments and suggestions:

* Fixed spelling of occurred.

* Used data in the singular. Evidently I was out of date and this usage is now accepted.

* Changed the example for using a dummy function from .info files to external PHP applications, which may be more common and easier to follow.

* Provided more explanation of the problems from t() misuse. This part could use review. I had a hard time trying to capture these fairly subtle issues in a way that is both succinct and compelling.

* Noted that contrib modules provide helpers for working with the locale system. I have in mind the i18n_strings module here. In practice, most module developers will want to use i18n_strings, as it's very challenging otherwise to introduce locale support. But I don't think we specify contrib modules in core.

* Added a note at the end about when to use st() and get_t(). (Removed "All" from the beginning of the docs to cover the case that sometimes you need to use st() instead of t().)

Dries’s picture

I think this is a clear improvement over the previous version of the patch. Well done, nedjo. I'll let someone else review it as well before I commit it.

Dries’s picture

Status: Needs review » Fixed

No additional reviews for 4 days so I decided to commit this to CVS HEAD based on my own review on December 28. Given that this is a documentation patch, we can easily follow-up on this in future patches. Thanks nedjo!

Dave Reid’s picture

Status: Fixed » Needs review
FileSize
7.22 KB

This patch had Windows Line endings (\r\n), which we do not use in core (\n). The testing bot only had 101 passes...weird. Patch converts the line endings to UNIX-style line endings.

nedjo’s picture

Apologies. I'm on ubuntu, but I copied this into and out of Open Office to check spelling, evidently introducing the line ending problem in the process. Next time I'll know better.

This line looks like it has an extra return:


+ *    since it is a mix of
+ actual interface strings and various user input strings of

Dave Reid’s picture

Looks like that was from the original patch as well, so this patch fixes the line endings plus the line wrapping pointed out in #12.

markus_petrux’s picture

Would it be possible to apply this patch to DRUPAL-6 as well?

In case a new release of D6 is made we could count on good information about t() there too. I could try to roll the patch if that helps.

Dries’s picture

Status: Needs review » Fixed

I've committed the line-endings patch to CVS HEAD, and committed the documentation patch (and line-endings patch) to DRUPAL-6 as well. Thanks.

Gábor Hojtsy’s picture

Yay, thanks. It is great to see such improvement on Drupal 6 as well.

markus_petrux’s picture

Sure, thank you. :-D

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.