t() cannot be used in a define() statement - document that fact.

CommentFileSizeAuthor
#22 338443_t_define.patch11.32 KBgrendzy
#20 338443_t_define.patch11.32 KBgrendzy
#15 338443reroll.patch10.99 KBjhodgdon
#10 t_rewrite.patch10.97 KBjhodgdon
#6 caution_t.patch800 bytesCitizenKane
t_doc.patch781 bytesAnonymous (not verified)
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

maartenvg’s picture

I've been bitten by this at least once, so I agree that this should be in the documentation.

Perhaps we want to add a line saying what developers should do instead, because that isn't straight forward. Because t(DEFINED_CONSTANT) is evil as well. We can refer to the documentation in #336115: Additional documentation for t(), which covers translation of variables.

catch’s picture

Status: Needs review » Needs work

An extra line explaining what you should do sounds good. Marking to CNW for that.

Anonymous’s picture

Status: Needs work » Needs review

@maartenvg: Then we need to fix the translation parser to understand the define and class const declarations so that it knows how to translate the strings. Having to specify the string constant in more than one place make it a horrid fix to make changes to the text. I therefore disagree that t(MY_DEFINED_CONST) is wrong and should be the desired way to create strings.

@catch: The proper use is discussed further in the document. The patch is a caution of where not to use the t() function.

nedjo’s picture

Re $locale in the comment: do you mean $language?

Anonymous’s picture

Status: Needs review » Needs work

Seems really obvious. I was pointed to the word locale from a version 5 api function for locale(). It changed in 6.

CitizenKane’s picture

Status: Needs work » Needs review
Issue tags: +tcdocsprint09
FileSize
800 bytes

Attached a patch which changes $local to $language.

Status: Needs review » Needs work

The last submitted patch failed testing.

jhodgdon’s picture

Status: Needs work » Needs review

Looks like the test bot was broken that day.

axyjo’s picture

Status: Needs review » Needs work
+++ includes/common.inc	24 Nov 2008 13:04:06 -0000
@@ -904,6 +904,9 @@ function fix_gpc_magic() {
+ * CAUTION: Do not use t() in a constant define() since $language is unknown at

Would the words 'not defined' possibly be a better alternative to the word 'unknown'? Also, I think the word 'constant' isn't needed. Besides that, I think this patch is nice and simple.

I'm on crack. Are you, too?

jhodgdon’s picture

Status: Needs work » Needs review
FileSize
10.97 KB

Oh gosh.

That text didn't really belong where it was inserted in the last patch. In trying to find a better place for it, I ended up reorganizing and rewriting much of the doc, in order to be able to add a short paragraph about not using t() in a define() statement, and having it all make sense.

grendzy’s picture

Status: Needs review » Needs work

patch no longer applies to HEAD.

jhodgdon’s picture

Status: Needs work » Needs review
Issue tags: -tcdocsprint09

#10: t_rewrite.patch queued for re-testing.

Status: Needs review » Needs work
Issue tags: +tcdocsprint09

The last submitted patch, t_rewrite.patch, failed testing.

jhodgdon’s picture

Looks like this patch does indeed need to be redone.

jhodgdon’s picture

Category: task » bug
Status: Needs work » Needs review
FileSize
10.99 KB

Apparently the t() function moved to a different file. Here is a reroll of the patch above.

grendzy’s picture

Status: Needs review » Reviewed & tested by the community

looks good, nice style cleanups in addition to the define() documentation.

Heine’s picture

+ * Also, you cannot use t() in a PHP define() statement. The reason is that
+ * the language variables will not be initialized yet at the time t() is called,
+ * so the string will not be translated into the correct language.

This is not limited to define statements; any t() call before the language is defined will result in

1) an incorrect string
2) atrocious performance (at least this was so in the past).

grendzy’s picture

Status: Reviewed & tested by the community » Needs work

ok... more specifically can we say that "before the language is defined" means that before DRUPAL_BOOTSTRAP_LANGUAGE is reached? How about this as an alternative?

/**
 * Also, you cannot use t() early in the bootstrap process, prior to the
 * DRUPAL_BOOTSTRAP_LANGUAGE phase. The language variables will not be
 * initialized yet, so the string will not be translated into the correct
 * language. Examples of places where t() cannot be used include:
 * - In a PHP define() statement.
 * - In a hook_boot() implementation.
 */
jhodgdon’s picture

I like the suggestion in #18. Would you like to roll it into the patch?

grendzy’s picture

Status: Needs work » Needs review
FileSize
11.32 KB

Only change is the block from #18:

jhodgdon’s picture

Status: Needs review » Needs work

Oh gracious. I should really be shot for this one, and it was in my patch above.

The first line should start with a verb in 3rd person: "translates" rather than "translate".

Otherwise, I'm happy with this latest patch. What say you Heine?

grendzy’s picture

Status: Needs work » Needs review
FileSize
11.32 KB

ok, corrected first word. Feedback on the technical accuracy about DRUPAL_BOOTSTRAP_LANGUAGE would be appreciated.

jhodgdon’s picture

Thanks for making that correction... I am happy with that patch. Perhaps Heine can comment on the technical accuracy question, since that is who brought up the latest round of re-patches?

grendzy’s picture

Discussed with bdragon during the drupalcon sprint. We verified that the change proposed in #18 is accurate.

Patch still applies, so this should be ready.

jhodgdon’s picture

Do you want to mark it RTBC?

grendzy’s picture

yep!

grendzy’s picture

Status: Needs review » Reviewed & tested by the community
webchick’s picture

Status: Reviewed & tested by the community » Fixed

Geez. ;) At least re-title these issues to something like "Fix everything in the world wrong with t()" so I know what I'm getting myself into. ;)

Anyway, took a spin through the changes and looked good overall.

Committed to HEAD!

Status: Fixed » Closed (fixed)
Issue tags: -tcdocsprint09

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