Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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().
Comment | File | Size | Author |
---|---|---|---|
#13 | 336115-followup-line-endings.patch | 7.43 KB | Dave Reid |
#11 | 336115-followup-line-endings.patch | 7.22 KB | Dave Reid |
#8 | t-docs.patch | 4.69 KB | nedjo |
t-docs.patch | 2.79 KB | nedjo | |
Comments
Comment #1
markus_petrux CreditAttribution: markus_petrux commented+1
Comment #2
maartenvg CreditAttribution: maartenvg commentedgreat addition to the documentation!
One tiny remark: 'occured' is spelled as 'occurred', which happened at least twice.
Comment #3
Dries CreditAttribution: Dries commented- "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. :-)
Comment #4
nedjoThanks 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()
andget_t()
. I'll add a brief note.Comment #5
Gábor HojtsyWell, 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).
Comment #6
Gábor HojtsyBTW 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.
Comment #7
markus_petrux CreditAttribution: markus_petrux commented@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).
Comment #8
nedjoNew 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().)
Comment #9
Dries CreditAttribution: Dries commentedI 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.
Comment #10
Dries CreditAttribution: Dries commentedNo 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!
Comment #11
Dave ReidThis 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.
Comment #12
nedjoApologies. 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:
Comment #13
Dave ReidLooks like that was from the original patch as well, so this patch fixes the line endings plus the line wrapping pointed out in #12.
Comment #14
markus_petrux CreditAttribution: markus_petrux commentedWould 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.
Comment #15
Dries CreditAttribution: Dries commentedI've committed the line-endings patch to CVS HEAD, and committed the documentation patch (and line-endings patch) to DRUPAL-6 as well. Thanks.
Comment #16
Gábor HojtsyYay, thanks. It is great to see such improvement on Drupal 6 as well.
Comment #17
markus_petrux CreditAttribution: markus_petrux commentedSure, thank you. :-D