Due to the initiative to create separate dedicated Annotation (#1966246: [meta] Introduce specific annotations for each plugin type)
the question came up: How do we document that a certain key needs a wrapped @Translation.

Some suggestions:

1)

  /**
   * The description of the plugin.
   *
   * The string should be wrapped in a @Translatable()
   *
   * @var string
   */
  public $description;

2)

  /**
   * The description of the plugin.
   *
   * @see \Drupal\Core\Annotation\Translation
   *
   * @var string
   */
  public $description;

3)

  /**
   * The description of the plugin.
   *
   * @var \Drupal\Core\Annotation\Translation
   */
  public $description;

4)

  /**
   * The description of the plugin.
   *
   * @var \Drupal\Core\Annotation\Translation|string
   */
  public $description;
Files: 
CommentFileSizeAuthor
#57 fix-translation-annotation-docs-v3.patch1.02 KBGábor Hojtsy
PASSED: [[SimpleTest]]: [MySQL] 58,491 pass(es).
[ View ]
#55 fix-translation-annotation-docs-v2.patch1.16 KBGábor Hojtsy
PASSED: [[SimpleTest]]: [MySQL] 58,141 pass(es).
[ View ]
#53 fix-translation-annotation-docs.patch1.03 KBGábor Hojtsy
PASSED: [[SimpleTest]]: [MySQL] 58,142 pass(es).
[ View ]
#44 drupal-1969970-44.patch2.11 KBdawehner
PASSED: [[SimpleTest]]: [MySQL] 55,341 pass(es).
[ View ]
#44 interdiff.txt2.11 KBdawehner
#42 drupal-1969970-42.patch2.12 KBdawehner
FAILED: [[SimpleTest]]: [MySQL] 55,514 pass(es), 1 fail(s), and 0 exception(s).
[ View ]
#42 interdiff.txt758 bytesdawehner
#37 drupal-1969970-37.patch1.99 KBdawehner
PASSED: [[SimpleTest]]: [MySQL] 55,450 pass(es).
[ View ]
#37 interdiff.txt2.11 KBdawehner
#35 drupal-1969970-35.patch1.86 KBdawehner
PASSED: [[SimpleTest]]: [MySQL] 55,374 pass(es).
[ View ]
#33 drupal-1969970-33.patch1.66 KBdawehner
PASSED: [[SimpleTest]]: [MySQL] 55,275 pass(es).
[ View ]
#15 drupal-1969970-15.patch779 bytesdawehner
PASSED: [[SimpleTest]]: [MySQL] 54,537 pass(es).
[ View ]

Comments

Personally I would go with @var ... because it makes it as clear as possible.

Issue summary:View changes

numbering the options

I vote for anything but #1.
I think #2 is the most conservative option

#3 is what we want people to do, but I'm not sure if its more confusing.

#4 is technically correct, because Drupal will accept either, even though we want it to be translated.

I vote for 3.

Eclipse

Component:plugin system» documentation

Moving to documentation so jhodgdon can take a look.

Well, I don't really understand this issue so it is hard to comment... What does it mean "a certain key needs a wrapped @Translation."? What does a developer need to do differently between "keys" (whatever those are) that do and don't need a "wrapped @Translation"? And where are you proposing putting this documentation (on the base class or something?)

Right.

So in Drupal 8, there are these "plugins" that are defined by "annotations" (special array syntax in PHP comments) like:

<?php
/**
* Provides a 'Book navigation' block.
*
* @Plugin(
*   id = "book_navigation",
*   admin_label = @Translation("Book navigation"),
*   module = "book"
* )
*/
class BookNavigationBlock extends BlockBase {
?>

These effectively replace "info" hooks, and aim to describe the thing they're defining right above the thing, rather than in a different place.

@Translation() effectively works like t() and marks something as a human-readable label that is output in the admin interface.

Many developers pushed back (rightly so) on these annotations because there was absolutely no way to know what possible keys could go into one without getting neck-deep in the code.

So, we've started a movement where we create effectively "typed" Plugins, like for example @EntityType instead of @Plugin, which can be thoroughly documented as to what they include.

This creates a class like the following:

+class EntityType extends Plugin {
+
+  /**
+   * The name of the module providing the type.
+   *
+   * @var string
+   */
+  public $module;
+
+  /**
+   * The name of the entity type class.
+   *
+   * This is not provided manually, it will be added by the discovery mechanism.
+   *
+   * @var string
+   */
+  public $class;
+
...
+  /**
+   * The human-readable name of the type.
+   *
+   * @var string
+   */
+  public $label;
+

So the question is, "In the documentation for EntityType, how do we differentiate between a human-readable string that must be wrapped in @Translation() in the annotation syntax, vs. just a normal everyday string?"

Does that make sense at all?

Thanks for the explanation!

I've always favored adding the words/phrase "human-readable" or "translated" in function/hook docs for parameters and return values that should be translated.... but this is a bit different...

So... My proposal would be that we try just being explicit:

  /**
   * The human-readable name of the type.
   *
   * In your plugin annotation, this value must be wrapped in @Translation().
   *
   * @var string
   */
  public $label;

And by the way... We have some documentation on plugin annotation using @Plugin and also the API module has been adapted to handle this specific annotation (it basically treats it as a @code/@endcode block). Are you saying now that it could be @BlockEntity, @SomeOtherClass, etc. rather than literally @Plugin? If so, how is the API module supposed to differentiate between this and other tags? Any ideas? (and should this go on another issue?)

Hm. That should probably go in another issue, probably in the API queue. That's a head-scratcher, for sure. :\ Maybe trigger off whether it's @CapsCaps instead of @lowercase?

I guess the API module could/should understand the concept of plugins and use doctrine, which gives us this annotation syntax, to scan these things?

Title:How do document needed @Translation on dedicated How do document needed @Translation on dedicated annotation classes

Title:How do document needed @Translation on dedicated annotation classesHow do document needed @Translation on dedicated

We also need to update the standards page on plugin annotations, so I filed
#1970900: Update standards doc on plugin annotations; add @defgroup annotation
in Drupal Core for the moment; will file separate issue in the API module once I know what it should do. :)

So back to this issue... thoughts on #17's idea for this topic?

Well, I think all of these top level classes that are replacing @Plugin are going to be extending it (if that's helpful to api.module).

Title:How do document needed @Translation on dedicated How do document needed @Translation on dedicated annotation classes

cross-post, restore title

Let's move the discussion about the @Plugin stuff to the other issue.

Status:Active» Needs review
StatusFileSize
new779 bytes
PASSED: [[SimpleTest]]: [MySQL] 54,537 pass(es).
[ View ]

Here is a patch to document this in EntityType().

+1 for #15. I think it's clear. Is it too much of a burden on core developers to do this?

Oh... But I think we also need to have the @Translation() escaped in some way or the API module could be confused into thinking this is plugin annotation if the doc block ended in a ) for some reason such as an @see tag.... So either put a space between the @ and Translation, or prefix @ by a backslash. This will not help clarity. :(

I don't see this as being particularly onerous.

Well, it's a lot of text. And it's text we'd want to make consistent across probably 500 properties. That's why I'd prefer something more like #3 in the OP which is easier to copy/paste.

I preferred 3 as well, and it fixes the issue in #17

So... Are these classes you are defining real classes that store real values on them? And if they are, is (for example) the $label property in EntityType actually an instantiated value of type Translation, or is it a plain-old PHP string value that is somehow magically translated with t()?

If all that is correct, then I agree that putting @var \Drupal\Core\Annotation\Translation makes sense on the EntityType class's $label property (option #3 in the issue report currently), and hopefully someone will also document somewhere that this means you need to put @Translation() in your @EntityType annotation too.

I don't know the answer to #21 (I suspect not), but another thought is just a simple "@ingroup Translatable" ... that's how we do some similar things elsewhere.

+1 on that idea (@ingroup something), assuming we add some documentation to the @defgroup.

They should be the actual class as a value and then when @EntityType::get() is called, the Translation::get() is called on that property and the translated value for the string is returned to the @EntityType class's representation of the annotation which is ultimately an array.

Eclipse

So... #24 didn't actually make sense to me.

But let's go back to #22. Can we just make a defgroup that explains the need to put @Translation() in the annotation for certain of your plugin properties?

And ... oops, probably a separate issue, but are the plugin extension classes themselves part of a group/topic as well? If not, can we make them be so they are discoverable by developers, the same way as we have @ingroup hooks and @ingroup themeable?

I have no idea what you're asking in your last paragraph of 25. That being said, if I understood #22, I don't have any issue with the proposal.

Eclipse

Several people at #1966246: [meta] Introduce specific annotations for each plugin type seem to believe that issue could result in @Translation() no longer being necessary...

I don't know if that will be the case or not, but it might not be worth expending too much effort here until that discussion is settled?

It looks like the last comment there (just after #28 here was posted) pointed out we still need @Translation() so that localize.drupal.org can pick out the strings that need to be translated, or at least that it makes localization string identification much simpler if it's there.

Title:How do document needed @Translation on dedicated annotation classesHow to document needed @Translation on dedicated annotation classes

Yup, I think we should continue forward on this issue.

Status:Needs review» Needs work

OK, I think we've all agreed that the previous patch is not all that workable. Can we get a patch that:
a) Adds a @defgroup translatable that describes this phenomenon
b) Adds @ingroup translatable to some translatable strings on EntityType

Status:Needs work» Needs review
StatusFileSize
new1.66 KB
PASSED: [[SimpleTest]]: [MySQL] 55,275 pass(es).
[ View ]

So here is a start.

Status:Needs review» Needs work

You cannot put a @defgroup inside of Class documentation. It needs to be in its own doc block by itself. Putting it into the file for the Translation class does seem like a good idea though. Here's the doc on how to write @defgroups:
http://drupal.org/coding-standards/docs#defgroup
Hm. Maybe that isn't clearly stated there... I will edit the page so it is.

Also, keep in mind that the Topic page will have the @defgroup text at the top, and a list of everything marked as being in the group below it. So saying "this class" won't work (you won't be able to tell which of the items listed below on the topic page it refers to). So, that documentation needs to be rewritten.

I also think that the topic text (the @defgroup documentation) should describe how to make a property translatable with annotation.

Finally, in the actual properties, the @ingroup should come at the end of the doc block.
http://drupal.org/coding-standards/docs#order

Status:Needs work» Needs review
StatusFileSize
new1.86 KB
PASSED: [[SimpleTest]]: [MySQL] 55,374 pass(es).
[ View ]

So what about something more in the direction of that?

I switched the documentation group, because it just feels wrong to block such a generic term with plugin annotation translations.

Status:Needs review» Needs work

Better... One note: if you are using @something() in a doc block as documentation, you need to leave a space between the @ and the something, or the doc parser will get confused and think it is annotation.

And can I offer a suggested docs edit (which may still some work):

When providing plugin annotation, properties whose values are displayed in the user interface should be made translatable. Much the same as how user interface text elsewhere is wrapped in t() to make it translatable, in plugin annotation, wrap translatable strings in the @ Translation() annotation. For example:
@code
title = @ Translation("Title of the plugin"),
@endcode
You will also need to make sure that your class file includes the line
@code
use Translation;
@endcode

Also, can we make the defgroup machine name a bit shorter? How about plugin_translatable?

Status:Needs work» Needs review
StatusFileSize
new2.11 KB
new1.99 KB
PASSED: [[SimpleTest]]: [MySQL] 55,450 pass(es).
[ View ]

The reason why it doesn't fail is because it's never scanned.
By default only files in the Plugins directories are scanned, so we are on the safe site.

I was talking about the API module that scans docs for api.drupal.org. I am pretty sure that it will get confused if you leave @Translation() in the docs without putting in a space. I think we need to have a space inside @code as well to avoid this problem, and probably we should put a note in the text about removing the space. We did something similar elsewhere... but I'm not remembering where (or perhaps it was a previous patch on this same issue?).

Category:task» feature

Category:feature» task

.

Ah thanks. Yes, here's the resulting api.drupal.org page from that other issue, showing the spaces after @:
PagerPluginBase class

StatusFileSize
new758 bytes
new2.12 KB
FAILED: [[SimpleTest]]: [MySQL] 55,514 pass(es), 1 fail(s), and 0 exception(s).
[ View ]

So let's remove the space, that the api module is not confused.

Status:Needs review» Needs work

This looks good to me, except that the @defgroup and @ingroup machine names do not agree in the Translation class itself. Oops! :)

So, I basically wrote the current docs test... dawehner do you agree that it's accurate, complete, etc. so we can call it duly reviewed? I of course think it's fine. :)

Status:Needs work» Needs review
StatusFileSize
new2.11 KB
new2.11 KB
PASSED: [[SimpleTest]]: [MySQL] 55,341 pass(es).
[ View ]

Oops.

The text is certainly nice to read, so I hope it's understandable for everyone. Primarily I just want to get rid of this blocker.

Assigned:Unassigned» jhodgdon
Status:Needs review» Reviewed & tested by the community

OK, then, since two of us like it, let's get it committed. I'll get to it shorltly unless another committer beats me to it. Thanks!

Awesome. Thank you very much. This unblocks a ton of other issues.

Assigned:jhodgdon» Unassigned
Status:Reviewed & tested by the community» Fixed

This is committed to 8.x. Thanks all!

Status:Fixed» Closed (fixed)

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

Status:Closed (fixed)» Needs work

+++ b/core/lib/Drupal/Core/Annotation/Translation.php
@@ -10,6 +10,28 @@
/**
+ * @defgroup plugin_translatable Translatable plugin metadata
+ *
+ * @{
+ * When providing plugin annotation, properties whose values are displayed in
+ * the user interface should be made translatable. Much the same as how user
+ * interface text elsewhere is wrapped in t() to make it translatable, in plugin
+ * annotation, wrap translatable strings in the @ Translation() annotation.
+ * For example:
+ * @code
+ *   title = @ Translation("Title of the plugin"),
+ * @endcode
+ * Remove spaces after @ in your actual plugin - these are put into this sample
+ * code so that it is not recognized as annotation.
+ *
+ * You will also need to make sure that your class file includes the line:
+ * @code
+ *   use Drupal\Core\Annotation\Translation;
+ * @endcode
+ * @}
+ */
+

This did not document the possibility to include context information in the annotation. Code like http://drupalcode.org/project/drupal.git/blob/HEAD:/core/lib/Drupal/Core... uses that and since this was not considered when the D8 API compatibility code was written for localize.drupal.org, those strings are now extracted as Bundle", context = "Validation Yeah :/

It would be great to document context here and provide an example.

Gabor: I think we should open a new issue for this rather than trying to reopen this one? I am not sure what you are asking for either -- seems like you are also reporting a bug?

The documentation added here was incomplete / misleading. The bug with parsing is already fixed in potx module.

OK... I'm unsure what needs to be done then.

Status:Needs work» Needs review
StatusFileSize
new1.03 KB
PASSED: [[SimpleTest]]: [MySQL] 58,142 pass(es).
[ View ]

The first part of the docs explains @Translation as a limited thing (no context), while the second part of the docs explains @Translation as more powerful than it is (with full support for t() stuff). So both need fixing. A proposal may be telling better what I'm talking about?! What about this?

Status:Needs review» Needs work

Yes, a patch is worth 1000 explanations, to paraphrase. :)

So:
- I think the new context example should be moved up into the first paragraph instead of being part of the second paragraph.
- I think the wording should be "It is also possible to provide a context with the text, similar to t():".
- The rest looks good to me.

Status:Needs work» Needs review
StatusFileSize
new1.16 KB
PASSED: [[SimpleTest]]: [MySQL] 58,141 pass(es).
[ View ]

Applied those suggestions. I felt the use Drupal\... stuff would have been closer to the simpler example, so people will not stop reading in the middle of the explanation. My idea was that the context stuff is "advanced" info while the use Drupal\... stuff is basic info. But I'm fine with this setup as well.

Good point. Maybe we should make it a separate, stand-alone 3rd paragraph then?

StatusFileSize
new1.02 KB
PASSED: [[SimpleTest]]: [MySQL] 58,491 pass(es).
[ View ]

This one?

Status:Needs review» Reviewed & tested by the community

Looks good to me!

Status:Reviewed & tested by the community» Fixed

Committed to 8.x.

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

Issue summary:View changes

a possibly stupid 4th idea