Skip to content

Commit c3d57c2

Browse files
committed
Add support to fix gettext .mo file cache
1 parent 9e58a16 commit c3d57c2

File tree

1 file changed

+35
-3
lines changed

1 file changed

+35
-3
lines changed

src/Translator/GettextTranslator.php

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public function setLocale(string $locale): self
102102
$locale
103103
));
104104
}
105-
105+
$this->locale = $locale;
106106
$localeText = $locale . '.' . $this->getEncoding();
107107
putenv('LC_ALL=' . $localeText);
108108
setlocale(LC_ALL, $localeText);
@@ -153,7 +153,11 @@ public function setDomain(string $domain): self
153153
{
154154
parent::setDomain($domain);
155155

156-
bindtextdomain($domain, $this->config->get('translation_path'));
156+
$versionedPath = $this->getVersionedPath(
157+
$domain,
158+
$this->config->get('translation_path')
159+
);
160+
bindtextdomain($domain, $versionedPath);
157161
bind_textdomain_codeset($domain, $this->getEncoding());
158162

159163
$this->domain = textdomain($domain);
@@ -170,7 +174,12 @@ public function addDomain(string $domain, ?string $path = null): self
170174

171175
$domains = $this->storage->getDomains();
172176

173-
bindtextdomain($domain, $domains[$domain]);
177+
$versionedPath = $this->getVersionedPath(
178+
$domain,
179+
$domains[$domain]
180+
);
181+
182+
bindtextdomain($domain, $versionedPath);
174183
bind_textdomain_codeset($domain, $this->getEncoding());
175184

176185
return $this;
@@ -260,4 +269,27 @@ public function trp(
260269

261270
return $translation;
262271
}
272+
273+
/**
274+
* Return the versioned path of .mo file
275+
* @param string $domain
276+
* @param string $localeDir
277+
* @return string
278+
*/
279+
protected function getVersionedPath(string $domain, string $localeDir): string
280+
{
281+
// Path to .mo file
282+
$moFile = sprintf(
283+
'%s/%s/LC_MESSAGES/%s.mo',
284+
$localeDir,
285+
$this->locale,
286+
$domain
287+
);
288+
289+
// Version based on the modification date of .mo file
290+
$version = file_exists($moFile) ? filemtime($moFile) : time();
291+
292+
// Add version to tha path (to break the cache)
293+
return sprintf('%s?v=%d', $localeDir, $version);
294+
}
263295
}

0 commit comments

Comments
 (0)