Hi,

I'm running the potx coder review on a number of modules and am getting a lot of "Invalid marker content" errors. I was wondering if you could provide a more detailed error message because it won't always be clear to the developer what needs to be changed in their code.

Also, I too am not totally clear on the different situations where "invalid marker content" could arise. Take for example:

  $message =  "Security warning: Couldn't write .htaccess file. Please create a .htaccess file in your %directory directory which contains the following lines: !htaccess";
  $repl =  array('%directory' => $directory, '!htaccess' => '<br />'. nl2br(check_plain($htaccess_lines)));
  form_set_error($form_item, t($message, $repl));
  watchdog('security', $message, $repl, WATCHDOG_ERROR);

The above code looks okay to me. The $message string is correctly formatted and the variables are provided in an array in $repl. Is the "invalid marker content" error received just because we're passing t() the $message variable rather than the string directly?

Also, does this error signify that potx was unable to extract the string? If so, does it mean the string can't be translated?

Doug and I are putting together an internationalization review for coder (see http://groups.drupal.org/node/15230) so if this is something coder could check for, let us know. Or if you have any additional ideas for checks, send them on too! :)

Cheers,
Stella

Comments

gábor hojtsy’s picture

Title: potx coder reviews » Better error messages for potx errors / code reviews.
Status: Active » Needs review
StatusFileSize
new4.9 KB

I think it is best if you suggest people to use the potx coder review to those who would like to do localization code checks and develop additional review items in coder or elsewhere. Since this code is and will be used for extraction and it already knows about the error conditions, it is best not to develop another review tool separately, while the potx tool itself can be used to do the review, so there is no question that it is up to date to the latest parsing modes, it is not required to keep two tools in sync :) I admit we can make the error messages better, we can put in more hints for coder to display, etc, I am totally open to these things!

As far as the actual error cited goes, t() needs the string literally, not through a variable. Marker errors are basically anything but a literal string used inside of t().

Since http://drupal.org/node/309875 is already about making the code review inside potx better (please do help out there!), let's make this an issue on better error messages from potx. I've made up a quick patch against the Drupal 5.x.-1.x branch. Since this module runs on four branches, this would need to be ported to four places, so it would be good to check whether this would be useful for your needs and then I can port the changes over. Although you could not try this patch out given the Drupal 5 branch does not have coder integration, I am hopeful you can review and provide feedback on the patch as-is.

gábor hojtsy’s picture

Part of the feedback I am looking for is whether we should separate the error message itself and its explanation, so coder can report the error and provide the explanation on demand.

gábor hojtsy’s picture

StatusFileSize
new4.92 KB

Ok, it was not hard to apply the same changes to the Drupal 6.2 version, so you can actually try it out with coder. Also fixed a style error, so now _potx_marker_error() generates the same all-on-one-line string as the other explanations. Note that http://drupal.org/node/309875 fixes coder integration issues due to the updated coder, so you might need to test the two in tandem.

I am eager for feedback.

gábor hojtsy’s picture

StatusFileSize
new4.97 KB

Doh, already found an omission. _potx_find_t_calls() is used to catch $t(), st(), and in Drupal 5 even watchdog() calls, since these have the same rules as t(). So better use the $function_name used at that moment in the error message as well. Updated the error to reflect this. Should be good for review now.

stella’s picture

There are some things I think the potx review can't check for (assuming it just parses the strings), for example missing calls to t(). It's not intended to replace or duplicate any of the potx reviews. See http://groups.drupal.org/node/15230 for the current list of checks done or planned. Though perhaps these two reviews should be merged.

Will try out your patch now. :)

Cheers,
Stella

gábor hojtsy’s picture

Yeah, two of the already implemented checks at the page you linked to are duplicates of what potx already does. Commented on it at http://groups.drupal.org/node/15230#comment-51218

Checking t() in format_plural(), an issue you opened would again be a duplicate, so commented on it at http://drupal.org/node/312510#comment-1027254

I agree code reviews for missing t() calls is not done in potx (and it might even be a good idea to do it right here), but bogus calls to locale functionality is checked and reported.

stella’s picture

Status: Needs review » Needs work
StatusFileSize
new39.46 KB

No afraid this doesn't work. I get "please report this" error. See attached screenshot.

I'm used the latest version of coder and potx from CVS. It doesn't happen for any of the other reviews.

Cheers,
Stella

gábor hojtsy’s picture

Have you also applied http://drupal.org/node/309875 as I've suggested (I am looking for some fixing of the patch there, but it should work on an English site with Garland)?

stella’s picture

No, I missed that bit.

So I installed a new version from CVS again. Applied the patch from #309875 - it succeeded but with one offset:

Hunk #1 succeeded at 850 (offset 9 lines).

Then applied the patch from this issue and it failed (applies successfully without the other patch):

Hunk #7 FAILED at 852.
1 out of 8 hunks FAILED -- saving rejects to file potx.inc.rej

I looked at the patches, and removing the 1st change from the 309875.patch fixes the issue. Both patches modified the same line in much the same way.

However, I am still faced with the same issue. I did run update.php and cleared the cache tables too (just to be sure!) but to no avail. :(

Oh, I am using Garland theme and running coder while viewing the site in English.

gábor hojtsy’s picture

Hm, well, I've used the potx coder review successfully with coder quite a few times before. I did not find good docs on how to write custom code reviews, so reverse engineered to find out how should I plug in to there. I think you might be more qualified to look into remaining issues with the integration. This bug is definitely tracked at http://drupal.org/node/309875 and should be fixed to go forward. That might also mark current potx to be useable only with older coder versions while post-commit with newer coder versions (or I did bad reverse engineering and the new stuff will work with both old and new).

stella’s picture

Status: Needs work » Needs review
StatusFileSize
new4.83 KB

Ok, it's working now, don't ask me why cos I didn't change anything! I think it might be that coder's new cache_coder table isn't getting cleared. Will look into it.

The error messages are definitely more helpful now. However, they do seem slightly long. I think it might be better to shorten them and then add a link to point the user at the more detailed explanation. Look at any of the coder/includes/coder_6x.inc reviews for examples on how to do that. First, however, we need a documentation to point them to. I would be in favour of adding a page to the coding standards area of the handbook with "dos and don'ts" for internationalization handling in modules. (As the expert in this, I don't suppose you want to volunteer? ;) )

I would also change the wording on some of the messages a bit. The attached patch does that for most of the messages. I think the message in relation to hook_perm() also needs to be changed but not sure what to.

Do you really need to specify the file name, line number and function for each error? Coder handles all of that, so there's no need for it in a coder review, but I realise this information may be needed if these errors are also printed when using potx to extract a file.

stella’s picture

Also, if we can, I'd remove the Invalid marker content t($rule['#warning']) from the coder review messages. Coder already includes the snippet of code, so that's not really needed. "Invalid marker content" means nothing to me, but maybe it makes sense to leave it in, not sure.

gábor hojtsy’s picture

Yay, great! We might be able to make the errors shorter with some text rework (some great modifications you have already done. We can omit the "invalid marker content" part and focus on more tailored concrete error messages. We can also do some refactoring here to not transfer file name and line number in the error message to coder, but only as meta info, which would lead to some refactoring of potx_status().

When used outside of coder module, we still need the file and line number for errors. When potx is used form the command line, we print errors to the command line, when potx is used under l10n_server, the l10n_server logs the errors in the database, when potx is used from the web interface, the errors are printed as drupal error messages. So there are at least three other options where we need the line number. This file name / line number refacoring could very well be part of http://drupal.org/node/309875 where this is sort of happening already although via reverse engineering the error message :)

stella’s picture

Excellent! Well I can help out with reviewing / testing of the updated #309875 patch whenever it's available.

gábor hojtsy’s picture

Ok, refactoring patch up on http://drupal.org/node/309875

stella’s picture

StatusFileSize
new4.4 KB

Ok rerolled patch due to changes in #309875. There were 4 conflicts (hunks 2, 6, 7, 8), so there's no harm in reviewing those again more closely.

Cheers,
Stella

gábor hojtsy’s picture

StatusFileSize
new4.33 KB

Ok, more to the point messages in this patch. The generic "Invalid localization code" part is now removed. Also, for the JS checks, we could know if t or formatPlural is the culprit, so we can split and use the right error message instead of a long one. This also has better error message for _perm() hooks, using the hook implementation function name found instead of the generic error.

Let's get this tested and reviewed.

stella’s picture

Status: Needs review » Needs work

I think we need to work on this slightly more. All of the error messages have the code snippet still attached. So when you run this in coder you get the snippet twice, for example:

Line 152: The first paramter to t() should be a literal string. There should be no variables, concatenation, constants or other non-literal strings there: t('@'.'file description should be on the following line').
    '#warning' => t('@'.'file description should be on the following line'),

However, the changes to the error messages themselves are much better and clearer. It also applies cleanly and works.

stella’s picture

Coder review of the patch throws up 2 styling issues. Neither of them are in the modified lines, but rather these styling issues were present in the original code:

potx.inc : @@ -545,8 +545,10 @@
  *  Line 10: There should be no trailing spaces
potx.inc : @@ -1102,7 +1104,9 @@
  *  Line 4: Control statements should have one space between the control keyword and opening parenthesis
          foreach($faulty_matches as $index => $match) {

Cheers,
Stella

gábor hojtsy’s picture

Yeah, well, my assumption with including the code snippet in the error is that there might be multiple t() calls on one line, eg:

$value = $condition ? t('This is good and looonggg...') : t('This '. 'is'. ' bad');

and in this case, it might help to have the exact bogus call at hand, so you don't need to dig up the line. Granted, lines should be short and all, so we might as well skip the concrete error to another structured item in the array and not pass that to coder. What do you think?

stella’s picture

I still think it's useful to have in other situations, just not when using coder. So yeah, that sounds good.

gábor hojtsy’s picture

Ok, will try to find some time to work on this soon. I am making a note that http://drupal.org/node/194962 is open to add a developer page which we could point to for more explanation on "how to write translatable source code" in the coder error messages. That page is still to be written.

hass’s picture

sub

hass’s picture

Please, add periods to sentences. An also often ignored and possibly unwritten rule...

stella’s picture

Periods at the end of sentences might be a better rule to have in coder's internationalization checks rather than in potx's I think.

hass’s picture

Well, but I wrote the above as I saw missing periods in the above patch. So "patch needs work" is correct.

gábor hojtsy’s picture

Hass, periods are already at the end of all sentences in all messages produced by this patch. Just run the patch and you will see.

hass’s picture

This is incorrect. Here is only one example from the patch (3 or 4 of this bugs are inside):

Wrong:

t('The first paramter to @function() should be a literal string. There should be no variables, concatenation, constants or other non-literal strings there', array('@function' => $function_name)));

Correct:

t('The first paramter to @function() should be a literal string. There should be no variables, concatenation, constants or other non-literal strings there.', array('@function' => $function_name)));
gábor hojtsy’s picture

Hass, let me repeat: Just run the patch and you will see. We are adding up the token information at the end of the message. We could of course just add a placeholder in the message and and it in ': %error_found.', but stella already requested to remove this from the message, so we are heading to where you think this patch should have been already. The messages when running out of coder module will be slightly longer this way though.

hass’s picture

You may be correct that there comes a period in the output, but then we may have a context sensitive bug. If the period of a sentence written inside t() is outside of t() we have a context sensitive bug. I haven't read all the above... only taken a look to the patch and this one seems to have context sensitive bugs. No need to run the patch to find such bugs :-).

gábor hojtsy’s picture

Status: Needs work » Needs review

Anyway, here is a new patch, which to coder's need introduces the concept of the code excerpt. The code excerpt is now rendered outside of the helpful error message, so complete sentences for helpful error messages are a given now. The location info may contain the excerpt now if available. There are four different combination possibilities for location information details, so all have their t() strings and compositions, and the information ends up at the end of the string in 3 of the four modes. The structured mode stores the excerpt info, but the coder code throws it away, since it does not need that.

@stella, what do you think of this one?

Ps. I did find some code style issues in the module otherwise as well. Let's clean them up after we improved the coder integration!

gábor hojtsy’s picture

StatusFileSize
new7.46 KB

Doh, the patch.

stella’s picture

Status: Needs review » Reviewed & tested by the community

I've tested the patch both via the coder review and by extracting some strings at admin/build/translate/extract and it works well in both scenarios. I think this one is good to go! :)

Cheers,
Stella

gábor hojtsy’s picture

Version: 6.x-2.x-dev » 5.x-2.x-dev
Status: Reviewed & tested by the community » Patch (to be ported)

Once I fixed a glitch in line numbering and committed a follow up to http://drupal.org/node/309875 (see that issue), this looked great to me as well. :) Committed to 6.x-2.x.

Inspired by your above comments, I've also fixed coder errors on this branch: http://drupal.org/node/313789

Once we have docs page on best practices, we can expand on this by adding a read more link to the docs.

gábor hojtsy’s picture

Category: support » task
stella’s picture

Status: Patch (to be ported) » Needs work

I'm not sure that line numbering patch for the coder module is correct. For example, if I run the coder internationalization, upgrade to D6 and potx reviews for the invite module, I get:

sites/sony/modules/contrib-6--2/invite/invite.module:
 +146: Invalid menu 'title' definition found in invite_menu(). Title and description keys of the menu array should be literal strings.
 +147: Menu item titles and descriptions should now no longer be wrapped in t() calls.
 +147: Menu item titles and descriptions should NOT be enclosed within t().

The line number for the result returned by the potx review is incorrect.

Cheers,
Stella

gábor hojtsy’s picture

Well, I was checking the code excerpt, not the line number. The code excerpt was one off since it used the line number as an array index. So should coder get a $lineno which starts from 1, but its array for the source code lines should be indexed from 0?

gábor hojtsy’s picture

What do you think Stella?

gábor hojtsy’s picture

Status: Needs work » Patch (to be ported)
StatusFileSize
new96.55 KB
new103.29 KB

Double checked this. I've added two artificial hook_menu() errors to l10n_client module and then looked for potx coder review and built in coder review output. Looks like coder and potx reporting errors on the same line. Coder says t() should not be in hook_menu(), while potx says it should be a literal string. Both errors reported on the same line. I consider this works right with the current code.

Screenshots attached.

stella’s picture

Which version of coder are you using out of interest? I'm using the 2.x-dev version, maybe this is caused by a coder change. Will need to get Doug to check.

gábor hojtsy’s picture

Version: 5.x-2.x-dev » 6.x-2.x-dev
Status: Patch (to be ported) » Fixed

Since 5.x is not maintained, moving back to fixed. If the lines happen to be an actual problem, we can open a separate issue to fix. I remember checking line numbers with the 7.x port with coder, and that worked right.

Status: Fixed » Closed (fixed)

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