Skip to content

Commit

Permalink
Resources: set a cacheCode that is language-specific, fixes AngularJS…
Browse files Browse the repository at this point in the history
… translation with multilingual
  • Loading branch information
mlutfy committed Dec 13, 2021
1 parent c46601a commit 8b547c5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
6 changes: 4 additions & 2 deletions CRM/Core/Resources.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand Down Expand Up @@ -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();
}

/**
Expand Down
23 changes: 13 additions & 10 deletions tests/phpunit/CRM/Core/ResourcesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ public function testAddScriptFile() {
$actual = $smarty->fetch('string:{crmRegion name=testAddScriptFile}{/crmRegion}');
// stable ordering: alphabetical by (snippet.weight,snippet.name)
$expected = ""
. "<script type=\"text/javascript\" src=\"http://core-app/foo%20bar.js?r=resTest\">\n</script>\n"
. "<script type=\"text/javascript\" src=\"http://ext-dir/com.example.ext/foo%20bar.js?r=resTest\">\n</script>\n";
. "<script type=\"text/javascript\" src=\"http://core-app/foo%20bar.js?r=resTesten_US\">\n</script>\n"
. "<script type=\"text/javascript\" src=\"http://ext-dir/com.example.ext/foo%20bar.js?r=resTesten_US\">\n</script>\n";
$this->assertEquals($expected, $actual);
}

Expand Down Expand Up @@ -288,7 +288,7 @@ public function testCrmJS() {
$actual = $smarty->fetch('string:{crmRegion name=testCrmJS}{/crmRegion}');
// stable ordering: alphabetical by (snippet.weight,snippet.name)
$expected = ""
. "<script type=\"text/javascript\" src=\"http://ext-dir/com.example.ext/foo%20bar.js?r=resTest\">\n</script>\n"
. "<script type=\"text/javascript\" src=\"http://ext-dir/com.example.ext/foo%20bar.js?r=resTesten_US\">\n</script>\n"
. "<script type=\"text/javascript\" src=\"/whiz/foo%20bar.js\">\n</script>\n";
$this->assertEquals($expected, $actual);
}
Expand All @@ -304,8 +304,8 @@ public function testAddStyleFile() {
$actual = $smarty->fetch('string:{crmRegion name=testAddStyleFile}{/crmRegion}');
// stable ordering: alphabetical by (snippet.weight,snippet.name)
$expected = ""
. "<link href=\"http://core-app/foo%20bar.css?r=resTest\" rel=\"stylesheet\" type=\"text/css\"/>\n"
. "<link href=\"http://ext-dir/com.example.ext/foo%20bar.css?r=resTest\" rel=\"stylesheet\" type=\"text/css\"/>\n";
. "<link href=\"http://core-app/foo%20bar.css?r=resTesten_US\" rel=\"stylesheet\" type=\"text/css\"/>\n"
. "<link href=\"http://ext-dir/com.example.ext/foo%20bar.css?r=resTesten_US\" rel=\"stylesheet\" type=\"text/css\"/>\n";
$this->assertEquals($expected, $actual);
}

Expand Down Expand Up @@ -350,7 +350,7 @@ public function testCrmCSS() {
$actual = $smarty->fetch('string:{crmRegion name=testCrmCSS}{/crmRegion}');
// stable ordering: alphabetical by (snippet.weight,snippet.name)
$expected = ""
. "<link href=\"http://ext-dir/com.example.ext/foo%20bar.css?r=resTest\" rel=\"stylesheet\" type=\"text/css\"/>\n"
. "<link href=\"http://ext-dir/com.example.ext/foo%20bar.css?r=resTesten_US\" rel=\"stylesheet\" type=\"text/css\"/>\n"
. "<link href=\"/whiz/foo%20bar.css\" rel=\"stylesheet\" type=\"text/css\"/>\n";
$this->assertEquals($expected, $actual);
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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,
],
];
}
Expand Down

0 comments on commit 8b547c5

Please sign in to comment.