From 8b547c56ba34e17a8051fb59d0b37459a9057d5e Mon Sep 17 00:00:00 2001 From: Mathieu Lutfy Date: Thu, 9 Dec 2021 14:13:55 -0500 Subject: [PATCH] Resources: set a cacheCode that is language-specific, fixes AngularJS translation with multilingual --- CRM/Core/Resources.php | 6 ++++-- tests/phpunit/CRM/Core/ResourcesTest.php | 23 +++++++++++++---------- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/CRM/Core/Resources.php b/CRM/Core/Resources.php index 0175befd74fa..0e4485c6adfd 100644 --- a/CRM/Core/Resources.php +++ b/CRM/Core/Resources.php @@ -336,7 +336,9 @@ public function glob($ext, $patterns, $flags = NULL) { * @return string */ public function getCacheCode() { - return $this->cacheCode; + // Ex: AngularJS json partials are language-specific because they ship with the strings + // for the current language. + return $this->cacheCode . CRM_Core_I18n::getLocale(); } /** @@ -563,7 +565,7 @@ public function addCacheCode($url) { $hasQuery = strpos($url, '?') !== FALSE; $operator = $hasQuery ? '&' : '?'; - return $url . $operator . 'r=' . $this->cacheCode; + return $url . $operator . 'r=' . $this->getCacheCode(); } /** diff --git a/tests/phpunit/CRM/Core/ResourcesTest.php b/tests/phpunit/CRM/Core/ResourcesTest.php index f6d00a1c92ca..f9d2ebaabd80 100644 --- a/tests/phpunit/CRM/Core/ResourcesTest.php +++ b/tests/phpunit/CRM/Core/ResourcesTest.php @@ -136,8 +136,8 @@ public function testAddScriptFile() { $actual = $smarty->fetch('string:{crmRegion name=testAddScriptFile}{/crmRegion}'); // stable ordering: alphabetical by (snippet.weight,snippet.name) $expected = "" - . "\n" - . "\n"; + . "\n" + . "\n"; $this->assertEquals($expected, $actual); } @@ -288,7 +288,7 @@ public function testCrmJS() { $actual = $smarty->fetch('string:{crmRegion name=testCrmJS}{/crmRegion}'); // stable ordering: alphabetical by (snippet.weight,snippet.name) $expected = "" - . "\n" + . "\n" . "\n"; $this->assertEquals($expected, $actual); } @@ -304,8 +304,8 @@ public function testAddStyleFile() { $actual = $smarty->fetch('string:{crmRegion name=testAddStyleFile}{/crmRegion}'); // stable ordering: alphabetical by (snippet.weight,snippet.name) $expected = "" - . "\n" - . "\n"; + . "\n" + . "\n"; $this->assertEquals($expected, $actual); } @@ -350,7 +350,7 @@ public function testCrmCSS() { $actual = $smarty->fetch('string:{crmRegion name=testCrmCSS}{/crmRegion}'); // stable ordering: alphabetical by (snippet.weight,snippet.name) $expected = "" - . "\n" + . "\n" . "\n"; $this->assertEquals($expected, $actual); } @@ -381,7 +381,7 @@ public function testCrmResURL() { $this->assertEquals('http://ext-dir/com.example.ext/foo%20bar.png', $actual); $actual = $smarty->fetch('string:{crmResURL ext=com.example.ext file=foo%20bar.png addCacheCode=1}'); - $this->assertEquals('http://ext-dir/com.example.ext/foo%20bar.png?r=resTest', $actual); + $this->assertEquals('http://ext-dir/com.example.ext/foo%20bar.png?r=resTesten_US', $actual); $actual = $smarty->fetch('string:{crmResURL ext=com.example.ext}'); $this->assertEquals('http://ext-dir/com.example.ext/', $actual); @@ -459,18 +459,21 @@ public function testAddingCacheCode($url, $expected) { * @return array */ public function urlForCacheCodeProvider() { + $cacheBusterString = Civi::resources() + ->setCacheCode($this->cacheBusterString) + ->getCacheCode(); return [ [ 'http://www.civicrm.org', - 'http://www.civicrm.org?r=' . $this->cacheBusterString, + 'http://www.civicrm.org?r=' . $cacheBusterString, ], [ 'www.civicrm.org/custom.css?foo=bar', - 'www.civicrm.org/custom.css?foo=bar&r=' . $this->cacheBusterString, + 'www.civicrm.org/custom.css?foo=bar&r=' . $cacheBusterString, ], [ 'civicrm.org/custom.css?car=blue&foo=bar', - 'civicrm.org/custom.css?car=blue&foo=bar&r=' . $this->cacheBusterString, + 'civicrm.org/custom.css?car=blue&foo=bar&r=' . $cacheBusterString, ], ]; }