From 04f32c6f06dd1df131210be594cc6e80bf85a09b Mon Sep 17 00:00:00 2001
From: Tim Otten <totten@civicrm.org>
Date: Wed, 22 Jan 2020 19:31:17 -0800
Subject: [PATCH] (dev/core#1546) Fix translation of seed data

Overview
--------

This fixes a regression in which seed data is not translated.

Before
------

The `civicrm_data.*.mysql` files are not translated.

After
-----

The `civicrm_data.*.mysql` files are translated.

There's a unit-test to check this.

Comments
--------

This regression stems from #15411, which aimed to allow extensions to define
custom variants of `ts()`.  The crux of the issue is "What happens if you
try to translate a string before the system is bootstrapped - before the
extension is loaded?  What's your fallback behavior?"

In #15411, it used a fallback behavior of "do no translation".  In theory,
you shouldn't really get into this scenario since UIs are pretty much always
generated post-boot.

However, it turns out that there is a situation where you have an un-booted
system and need to translate strings -- i.e. when generating the localized
`civicrm_data.*.mysql` data.  Hence the bug.

This patch preserves most of the changes from #15411, but it changes the
fallback behavior from "do no translation" to "use the built-in/default
translator".
---
 CRM/Core/I18n.php                            |  4 --
 tests/phpunit/E2E/Core/LocalizedDataTest.php | 41 ++++++++++++++++++++
 2 files changed, 41 insertions(+), 4 deletions(-)
 create mode 100644 tests/phpunit/E2E/Core/LocalizedDataTest.php

diff --git a/CRM/Core/I18n.php b/CRM/Core/I18n.php
index 57877c96a5..be23d592ff 100644
--- a/CRM/Core/I18n.php
+++ b/CRM/Core/I18n.php
@@ -774,10 +774,6 @@ function ts($text, $params = []) {
         $function = $config->customTranslateFunction;
       }
     }
-    else {
-      // don't _translate_ anything until bootstrap has progressed enough
-      $params['skip_translation'] = 1;
-    }
   }
 
   $activeLocale = CRM_Core_I18n::getLocale();
diff --git a/tests/phpunit/E2E/Core/LocalizedDataTest.php b/tests/phpunit/E2E/Core/LocalizedDataTest.php
new file mode 100644
index 0000000000..a1d1318971
--- /dev/null
+++ b/tests/phpunit/E2E/Core/LocalizedDataTest.php
@@ -0,0 +1,41 @@
+<?php
+
+namespace E2E\Core;
+
+/**
+ * Class LocalizedDataTest
+ * @package E2E\Core
+ * @group e2e
+ */
+class LocalizedDataTest extends \CiviEndToEndTestCase {
+
+  /**
+   * Smoke test to check that "civicrm_data*.mysql" files contain
+   * translated strings.
+   */
+  public function testLocalizedData() {
+    $getSql = function($locale) {
+      $path = \Civi::paths()->getPath("[civicrm.root]/sql/civicrm_data.{$locale}.mysql");
+      $this->assertFileExists($path);
+      return file_get_contents($path);
+    };
+    $sqls = [
+      'de_DE' => $getSql('de_DE'),
+      'fr_FR' => $getSql('fr_FR'),
+    ];
+    $pats = [
+      'de_DE' => '/new_organization.*Neue Organisation/i',
+      'fr_FR' => '/new_organization.*Nouvelle organisation/i',
+    ];
+
+    $match = function($sqlLocale, $patLocale) use ($pats, $sqls) {
+      return (bool) preg_match($pats[$patLocale], $sqls[$sqlLocale]);
+    };
+
+    $this->assertTrue($match('de_DE', 'de_DE'), 'The German SQL should match the German pattern.');
+    $this->assertTrue($match('fr_FR', 'fr_FR'), 'The French SQL should match the French pattern.');
+    $this->assertFalse($match('de_DE', 'fr_FR'), 'The German SQL should not match the French pattern.');
+    $this->assertFalse($match('fr_FR', 'de_DE'), 'The French SQL should not match the German pattern.');
+  }
+
+}
-- 
2.25.1