Problem/Motivation

The "separator" used in daterange fields is not translatable. It should be.

Proposed resolution

Make it translatable. I believe the proper way to do this is to add "translatable" and "translation context" to the schemas for the separator formatter settings (someone please correct this if it is wrong).

Remaining tasks

User interface changes

API changes

Data model changes

Comments

mpdonadio created an issue. See original summary.

mylies’s picture

Status: Active » Needs review
StatusFileSize
new3.65 KB

And here a patch
I added a translatable config to schema, so for correct usage, need to reinstall module datetime_range

mpdonadio’s picture

Status: Needs review » Needs work

    Thanks for working on this!

  1. +++ b/core/modules/datetime_range/config/schema/datetime_range.schema.yml
    @@ -34,6 +34,7 @@ field.formatter.settings.daterange_default:
    +      translatable: true
    

    We should provide a translation context here. How about 'Date range separator' ? Happens few times in the patch.

  2. +++ b/core/modules/datetime_range/src/Plugin/Field/FieldFormatter/DateRangeCustomFormatter.php
    @@ -51,7 +51,7 @@ public function viewElements(FieldItemListInterface $items, $langcode) {
    -            'separator' => ['#plain_text' => ' ' . $separator . ' '],
    +            'separator' => ['#plain_text' => ' ' . t($separator) . ' '],
    

    Since this is a config translation, we don't need this. t() is just for real strings provided by core.

I don't think we need test coverage of this since we have coverage of config schema translation in general.

If we add an empty update_N hook to the module, it will do a rebuild which should pick up the schema change.

mpdonadio’s picture

Status: Needs work » Needs review
StatusFileSize
new1.79 KB
new4.38 KB

I think this is how to do this, but I can't find this anywhere in admin/config/regional/config-translation

vijaycs85’s picture

Status: Needs review » Needs work
+++ b/core/modules/datetime_range/config/schema/datetime_range.schema.yml
@@ -34,6 +34,8 @@ field.formatter.settings.daterange_default:
       type: string

type: label is equivalent of string + translatable.

mpdonadio’s picture

Status: Needs work » Needs review
StatusFileSize
new1.84 KB
new1.29 KB

Seeing if we have an issue about translating field formatter settings. They aren't in the UI.

mpdonadio’s picture

vijaycs85’s picture

Status: Needs review » Reviewed & tested by the community

Looks good...

mpdonadio’s picture

Adding @vijaycs85 to the commit list b/c he spent a while in IRC helping me dig through the UI issue (which I had totally forgotten about).

xjm’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +rc eligible, +Needs tests

Actually adding credit for @vijaycs85. (Only committers can actually save values in the Credits & Committing section).

Patch looks good to me, and is RC-eligible since it only touches experimental code.

However, I have a question about the related issue, #2546212: Entity view/form mode formatter/widget settings have no translation UI. Does that prevent the separator we want to add here from being translated at all? If so maybe we should postpone on that issue, because we also need test coverage for this. If there's another way to provide a translation without needing the UI, then the test could exercise that method instead. @vijaycs85, any ideas?

vijaycs85’s picture

Reg #9: thanks @mpdonadio :)

Reg #10: I don't think we have any other option to add/update the translation without the related issue getting in. We can see the translation UI using Configuration Inspector, but no way to add/update.

vijaycs85’s picture

StatusFileSize
new59.25 KB
new56.92 KB
new121.3 KB

Just FYI, I applied the latest patch in #2546212: Entity view/form mode formatter/widget settings have no translation UI and the patch in #6 of this issue and able to see the translation.

penyaskito’s picture

We can provide the language override in a test using the API. Is that better than postponing it?

gábor hojtsy’s picture

I agree the fix looks good. If we want to test it, we can add an override with the API, the language manager makes it possible, @penyaskito and I have a session on this: http://www.slideshare.net/gabor.hojtsy/drupal-8-multilingual-apis slide 69 ;)

We do not yet have a UI to translate these due to #2546212: Entity view/form mode formatter/widget settings have no translation UI but I would consider the two bugs independent, since we did not make the rest of the entity mode translatable settings postponed either on not having a UI for them.

jhedstrom’s picture

Status: Needs work » Needs review
StatusFileSize
new4.32 KB
new6.17 KB

This adds a test, although I haven't worked much with config translation tests, so am unsure if this is what @Gábor Hojtsy was referring to.

Status: Needs review » Needs work

The last submitted patch, 15: 2796151-15.patch, failed testing.

The last submitted patch, 15: 2796151-15.patch, failed testing.

penyaskito’s picture

Issue tags: -Needs tests

Thanks!

  1. +++ b/core/modules/datetime_range/tests/src/Kernel/SeparatorTranslationTest.php
    @@ -0,0 +1,130 @@
    +    $language_manager = $this->container->get('language_manager');
    +    $language_manager->getLanguageConfigOverride('nl', 'core.entity_view_display.entity_test.entity_test.default')
    +      ->set('content.' . $this->fieldStorage->getName() . '.settings.separator', 'NL_TRANSLATED!')
    +      ->save();
    

    Yes, this is exactly what Gábor and I were referring to.

  2. +++ b/core/modules/datetime_range/tests/src/Kernel/SeparatorTranslationTest.php
    @@ -0,0 +1,130 @@
    +      ->setlanguage(new Language(['id' => 'nl']));
    

    Should be setLanguage (see uppercase L)

  3. +++ b/core/modules/datetime_range/tests/src/Kernel/SeparatorTranslationTest.php
    @@ -0,0 +1,130 @@
    +    $this->verbose($output);
    

    I guess we should delete this debug line.

  4. +++ b/core/modules/datetime_range/tests/src/Kernel/SeparatorTranslationTest.php
    @@ -0,0 +1,130 @@
    +    $this->assertContains('NL_TRANSLATED', (string) $output);
    

    Just cosmetic, but let's assert with the exclamation sign if we put that in the translation.

mpdonadio’s picture

#18-3, since this is a kernel test, I would leave this so that you can see the output from the UI, to be similar to what would happen from a drupalGet() ?

jhedstrom’s picture

Status: Needs work » Needs review
StatusFileSize
new1.03 KB
new6.17 KB

This addresses #18.2 and #18.4. I've left the verbose output as it is similar to other kernel tests in core as mentioned in #19.

Not sure why this was passing locally with that lower case 'l'.

Status: Needs review » Needs work

The last submitted patch, 20: 2796151-20.patch, failed testing.

The last submitted patch, 20: 2796151-20.patch, failed testing.

mpdonadio’s picture

Status: Needs work » Needs review
StatusFileSize
new6.17 KB

Not sure why this was passing locally with that lower case 'l'.

Because PHP: http://www.php.net/manual/en/functions.user-defined.php :

Note: Function names are case-insensitive, though it is usually good form to call functions as they appear in their declaration.

Reattaching #20 to see if it will run. Patch seems fine locally.

Status: Needs review » Needs work

The last submitted patch, 23: 2796151-20.patch, failed testing.

mpdonadio’s picture

Status: Needs work » Needs review
StatusFileSize
new6.15 KB
new824 bytes

Let's try adding a @group, https://www.drupal.org/node/2680713

vijaycs85’s picture

Looks good. +1 to RTBC

Minor rewrite to avoid those 4 lines of duplicate code. Don't think worth a re-roll just for this fix, but if we find some other issue, we can add it:


diff --git a/core/modules/datetime_range/tests/src/Kernel/SeparatorTranslationTest.php b/core/modules/datetime_range/tests/src/Kernel/SeparatorTranslationTest.php
index 155bb61..f6cd4fd 100644
--- a/core/modules/datetime_range/tests/src/Kernel/SeparatorTranslationTest.php
+++ b/core/modules/datetime_range/tests/src/Kernel/SeparatorTranslationTest.php
@@ -34,6 +34,13 @@ class SeparatorTranslationTest extends KernelTestBase {
   protected $field;
 
   /**
+   * The field used in this test class.
+   *
+   * @var \Drupal\Core\Entity\EntityInterface
+   */
+  protected $entity;
+
+  /**
    * {@inheritdoc}
    */
   public static $modules = [
@@ -88,27 +95,24 @@ protected function setUp() {
       'status' => TRUE,
     ])->setComponent($this->fieldStorage->getName(), $display_options)
       ->save();
-  }
 
-  /**
-   * Tests the translation of the range separator.
-   */
-  public function testSeparatorTranslation() {
     // Create an entity.
-    $entity = EntityTest::create([
+    $this->entity = EntityTest::create([
       'name' => $this->randomString(),
       $this->fieldStorage->getName() => [
         'value' => '2016-09-20',
         'end_value' => '2016-09-21',
       ],
     ]);
+  }
+
+  /**
+   * Tests the translation of the range separator.
+   */
+  public function testSeparatorTranslation() {
 
     // Verify the untranslated separator.
-    $display = EntityViewDisplay::collectRenderDisplay($entity, 'default');
-    $build = $display->build($entity);
-    $output = $this->container->get('renderer')->renderRoot($build);
-    $this->verbose($output);
-    $this->assertContains('UNTRANSLATED', (string) $output);
+    $this->assertSeparatorTranslation('UNTRANSLATED');
 
     // Translate the separator.
     ConfigurableLanguage::createFromLangcode('nl')->save();
@@ -118,14 +122,28 @@ public function testSeparatorTranslation() {
       ->set('content.' . $this->fieldStorage->getName() . '.settings.separator', 'NL_TRANSLATED!')
       ->save();
 
+    // Change language and clear translation cache.
     $this->container->get('language.config_factory_override')
       ->setLanguage(new Language(['id' => 'nl']));
-    $this->container->get('cache_tags.invalidator')->invalidateTags($entity->getCacheTags());
-    $display = EntityViewDisplay::collectRenderDisplay($entity, 'default');
-    $build = $display->build($entity);
+    $this->container->get('cache_tags.invalidator')->invalidateTags($this->entity->getCacheTags());
+
+    $this->assertSeparatorTranslation('NL_TRANSLATED!');
+  }
+
+  /**
+   * Helper to assert translation.
+   *
+   * @param string $text
+   *   String translation.
+   *
+   * @throws \Throwable
+   */
+  protected function assertSeparatorTranslation($text) {
+    $display = EntityViewDisplay::collectRenderDisplay($this->entity, 'default');
+    $build = $display->build($this->entity);
     $output = $this->container->get('renderer')->renderRoot($build);
     $this->verbose($output);
-    $this->assertContains('NL_TRANSLATED!', (string) $output);
+    $this->assertContains($text, (string) $output);
   }
 
 }
penyaskito’s picture

Status: Needs review » Reviewed & tested by the community

#25 looks RTBC to me. I actually find it more readable without the patch at #26.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: -rc eligible
+++ b/core/modules/datetime_range/config/schema/datetime_range.schema.yml
@@ -32,24 +32,27 @@ field.formatter.settings.daterange_default:
+      type: label
...
+      type: label
...
+      type: label

It's not really a semantically a label. Of the existing core types the best fit is type: text.

I'm not sure whether this should be fixed in 8.2.x or is only eligible for 8.3.x because (a) new translatable strings (b) the hook_update_N

alexpott’s picture

+++ b/core/modules/datetime_range/datetime_range.install
@@ -0,0 +1,22 @@
+  \Drupal::service('plugin.manager.field.formatter')->clearCachedDefinitions();

This can be an empty update because a cache flush occurs if there is an empty function.

alexpott’s picture

Version: 8.2.x-dev » 8.3.x-dev

Let's fix in 8.3.x first and discuss 8.2.x eligibility once we've got this in.

gábor hojtsy’s picture

It's not really a semantically a label. Of the existing core types the best fit is type: text.

Well, our handling of text is as a multiline text widget, and this is not that, is it? :) We can introduce yet another single line translatable string type to have a semantically more correct name for it for these cases, but would that simplify the choice of the type for single line translatable strings or complicate it?

mpdonadio’s picture

Assigned: Unassigned » mpdonadio

There is some IRC conversation going on right now about this. Once it is finished, I will post a new patch.

alexpott’s picture

As pointed out in IRC we've (ab)used label for this already so ignore #28 - we still need to address #29 since clearing the cache twice is unnecessary.

mpdonadio’s picture

Status: Needs work » Needs review
StatusFileSize
new810 bytes
new6.09 KB

Here is the adjustment to the update hook.

Once this is in 8.3.x, it would just need a quick change to the update hook to make it work for 8.2.x. I don't see why it wouldn't be eligible since datetime_range is still officially experimental, and even with that we are only adding a translatable string, not changing any.

mpdonadio’s picture

Assigned: mpdonadio » Unassigned
gábor hojtsy’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +sprint

Yay, thanks!

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/modules/datetime_range/datetime_range.install
@@ -0,0 +1,22 @@
+/**
+ * @addtogroup updates-8.3.x
+ * @{
+ */
+
+/**
+ * Clear caches to ensure schema changes are read.
+ */
+function datetime_range_update_8301() {
+  // Empty update to cause a cache rebuild.
+}
+
+/**
+ * @} End of "addtogroup updates-8.3.x".
+ */

Ah of course this is experimental... this should be an 8.2.x update - we can't change update numbers like that. So we need to do it before committing to 8.3.x. And thne we can cherry-pick for 8.2.x

lomasr’s picture

StatusFileSize
new84.79 KB

Please correct me if I am wrong : With a date range field I am getting date like "Tue, 10/11/2016 - 12:00 - Mon, 10/31/2016 - 12:00 " . Is it expected to come 'and' instead of '-' . Please see the screen. if '-' is good to show . Then I think we don't need to translate.

mpdonadio’s picture

Status: Needs work » Needs review
StatusFileSize
new653 bytes
new6.09 KB

OK, update hook renamed.

#38, since this is a configurable setting, a user is free to set to to anything they want. If they choose a word or phrase, this will allow it to be translated when needed. It is also helpful to allow it to be translatable in case where the default '-' may not make sense for a particular locale.

jhedstrom’s picture

Status: Needs review » Reviewed & tested by the community

I think this is back to RTBC.

anish.a’s picture

The steps for reproducing the issue need to be updated. I couldn't reproduce the exact issue as of now.

catch’s picture

Assigned: Unassigned » alexpott

Since this is experimental I think it's better if it goes into 8.2.x - I'm assuming @alexpott forgot to change the version in #37 but in case there was an explicit reason to leave it against 8.3.x, assigning for a chance to comment.

catch’s picture

Version: 8.3.x-dev » 8.2.x-dev
Status: Reviewed & tested by the community » Needs work

Sorry only realised this after posting the review and stepping afk.

We shouldn't use hook_update_N() for empty updates to trigger a cache flush - it adds all the potential complexity of schema versions between minor branches, conflicts with other patches, inconsistencies if there's a revert etc. hook_post_update_NAME() achieves the same thing without most of that trouble.

mpdonadio’s picture

Status: Needs work » Needs review
StatusFileSize
new6.14 KB
new877 bytes

OK, here we go.

catch’s picture

Status: Needs review » Reviewed & tested by the community

That's great, thanks!

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed and pushed a55967d to 8.3.x and da66644 to 8.2.x. Thanks!

  • alexpott committed a55967d on 8.3.x
    Issue #2796151 by mpdonadio, jhedstrom, myLies, vijaycs85, lomasr,...

  • alexpott committed da66644 on 8.2.x
    Issue #2796151 by mpdonadio, jhedstrom, myLies, vijaycs85, lomasr,...

Status: Fixed » Closed (fixed)

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

gábor hojtsy’s picture

Issue tags: -sprint