note 120159 added to ref.gettext

From: Date: Mon, 14 Nov 2016 11:28:33 +0000
Subject: note 120159 added to ref.gettext
Groups: php.notes 
Request: Send a blank email to php-notes+get-207095@lists.php.net to get a copy of this message
Gettext caches the machine readable *.mo files. This makes it pretty fast but also impossible to change a .mo file while running an application. Since PHP is running as a server, you cannot update your .mo file and see your new translation immediately. There are two ways to overcome this caching problem: 1. Restart your webserver. For example by a systemctl restart apache2. This will kill already running php processes and reload them afterwards. 2. If you don't have rights to restart a webserver (for example you are using a shared host), you can change the domain. Use for example this PHP code to hard link the new domain to the changed .mo files: $locale = "nl_NL.utf8"; //set this to your language setlocale(LC_ALL, $locale); putenv('LANG=' . $locale); $mtime = 0; $translationDir = "locale"; //or where ever you store your translations $originals = glob("$translationDir/*/LC_MESSAGES/messages.mo"); foreach ($originals as $file) $mtime = max($mtime, filemtime($file)); $domain = "messages_$mtime"; $current = glob("$translationDir/*/LC_MESSAGES/$domain.mo"); if (count($originals) != count($current)) { foreach (glob("$translationDir/*/LC_MESSAGES/messages_*.mo") as $file) unlink($file); foreach ($originals as $file) link($file, substr($file, 0, -3)."_$mtime.mo"); } bindtextdomain($domain, $translationDir); textdomain($domain); What this script does is check whether at least one of the translations is updated and if so remove all messages_*.so and hard link all messages.po to corresponding messages_$mtime.po, where $mtime is set to the latest modified translation file. Since it uses a new domain ("messages_$mtime") it is not cached. ---- Server IP: 72.52.91.14 Probable Submitter: 2001:1c06:90f:fd00:8cd2:63bb:643a:e99 ---- Manual Page -- http://php.net/manual/en/ref.gettext.php Edit -- https://master.php.net/note/edit/120159 Del: integrated -- https://master.php.net/note/delete/120159/integrated Del: useless -- https://master.php.net/note/delete/120159/useless Del: bad code -- https://master.php.net/note/delete/120159/bad+code Del: spam -- https://master.php.net/note/delete/120159/spam Del: non-english -- https://master.php.net/note/delete/120159/non-english Del: in docs -- https://master.php.net/note/delete/120159/in+docs Del: other reasons-- https://master.php.net/note/delete/120159 Reject -- https://master.php.net/note/reject/120159 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#207095) next »