You are here:Home»KB»Programming»PHP»Gettext translation system
Friday, 09 June 2017 09:07

Gettext translation system

Written by

gettext() is a translation system written by GNU people and is present on many systems natively. This makes it a good standard to use. Gettext does have some issues especially on PHP and I will outline what I have found here with possible solutions

I now use motranslator which is based on the same technology as gettext because of the following:

  • gettext translation file (.mo) is cached by the server and is never refreshed until you restart server, so changes you make will not be seen untill then. Not practical for shared webhosting.
  • On Windows gettext will not allow translations to other than default language - There are some untested workarounds but they are messy (Custom Code, Thread Safe?, PHP as FastCGI not as a module)
  • On Windows gettext will only read the directory that belongs to the default language meaning all translations need to go in the default local directory to allow translations.

General Gettext

Gettext Tutorials

Gettext Documentation

Gettext software

Misc documents

Issues I have found with gettext

It is not straight forward to getting gettext() to workout of the box, especially when using xammp on windows. HEre I will address the issues I came across while trying to get translation to work. These issues probably also appear on diffrent setups.

Apache Gettext Caching issue

When i was developing QWcrm on my windows xammp setup I would make changes to translation strings in th .PO file (via POedit) and these would not get reflected in my software. After a lot of research I discovered that when you run PHP as an Apache module the the getttext translation files upon first load are cached 'premanently' or for a time which I can not figure out. So to get around this I needed to find a way of refreshing the cache or turning it of for my development site.

Solutions I found

  • change the .MO filenames loaded when ever you make changes - this works but is a bit of a mess and I would not recommend it for production sites.
  • use PHP as Fast CGI. When PHP is loaded as a module the .MO files are cached 'permantely' or for an undisclosed time I can not figure out but this apparently is not the case when using PHP as Fast CGI
  • The only true way to clear the cache it to restart apache. I think if you are running PHP as an Apache module, you have to restart Apache for the catalog to be reloaded.
  • clearstatcache() - i could not get this to work

I will now outline my research below

Emptying of MO gettext cache without restarting apache links

Gettext not working on windows / xampp

These following issues can cause a real problem when devloping with gettext on windows. The main issue I came across is the localisation handling.

Issue

  • gettext will work but only with the default language/region
    • The reason is that the language/region is set for the whole computer and cannot be changed for individual processes. I believe that this is why there are 2 types of apache/php thread safe and non thread safe. I am not 100% how to apply the 2 variants and I dont know of any work aruonds to change the settings just for xampp.
    • Windows uses different region codes thant those used by gettext (gettext = de_DE whereas windows = deu)

Solutions

  • if you use the standard gettext file layout windows will always be able to access the default language folder. This gives to unique features
    • it allows translation to work normally by using the default language (at least you can test gettext is working)
    • you can add in the other language translation files into the default language folder and then they can also be used (this is a workaround)
  • not confirmed - is this because I do not have the other languages installed on my windows PC - see this thread

You can write code to utilise the workaround outline above by address the locale issue. When trying to fix this do not forget about the caching issue above.

Links

xgettext will not scan within double quotes / remmed lines

xgettext is very particular about what it scans for, it seems to be syntax aware rather than just searching for the strings. i.e. if a line is remmed out it will ignore it.

Scanning source files for translations strings

once you have your project done you need to grab all of the strings and translate them, doing this manually would be difficult and you would miss strings. I outline the options I have found below:

  • Use software to extract the gettext strings
  • Scan .tpl files with specialised scripts
  • Compile all smarty templates and then just scan them with POedit. It is not ideal but will work

Applications

  • POEdit- will scan your PHP files for standard gettext() strings. It will not scann for {t}..{/t} strings in the .tpl files
  • POedit + a custom extractor that will scan the .tpl files. I dont currently have one.
  • Eazy Po - Eazy Po is a translation editor and a catalog manager for Gettext translation files.

Smarty TPL Scanning software

Poedit Links

Smarty Poedit Parsers (software) / and other parsers than can potentially be used

Poedit notes

  • in version 2 all of the parsers have bve removed and put into 1 extractor which you cannot edit or turn off so you should use version v1.83
  • because there are no extractors to see how they are configure i will give you one below. All of the parsers (except TWIG) use xgettext. the only different between the settings are the language name
    • Language:    Perl
    • List of extensions:    ‪‪*.pl;*.PL;*.pm;*.perl;
    • Command to extract translations:    ‪‪xgettext --language=Perl --add-comments=TRANSLATORS: --force-po -o %o %C %K %F
    • An item in keywords list:    ‪-k%k
    • An item in input files list:    ‪%f
    • Source code charset:    ‪‪--from-code=%c
  • if you run xgettext --help on the command line you will see all of the switches listed
  • in ngettext -o is an alias of --output=FILE  and all other switches are similiar.

Compiling all smarty templates

Translating in Smarty

This section is involve in actually translating the strings in the smarty software.

Smarty translation links (various)

Smarty Software

tsmarty2c.php Command line examples

setup the software as per these instructions - configure the software, you dont need the poedit software for this to work.

  • php "D:\websites\php\gettext\tsmarty2c.php" D:\test.pot %f | "C:\Program Files (x86)\Poedit\GettextTools\bin\xgettext -LC --add-comments --no-location -"
    • this involves sending it to xgettext and is not needed here
    • i have just added to show you can send output to another program with a  pipe (|) character 
  • php "D:\websites\php\gettext\tsmarty2c.php" -o test.pot "D:\websites\htdocs\develop\qwcrm\themes"
    • this will scan the 'themes' directory recursively for all .tpl files and parse them for the {t}...{/t} strings
  • php -q tsmarty2c.php mytemplate.tpl > mydomain.c
    • this will output to a file
    • you could just use the -o switch

 

Read 2855 times Last modified on Sunday, 10 December 2017 15:54