PHP Smarty: a modifier for internationalization tool gettext

2013-07-19 00:00:00 IN1 , 2023-04-10 14:16:28 IN1


This Smarty modifier helps instantly translating Strings into other Languages using the PHP Extension 'gettext'

Overview

Usage Examples

In all Examples, the File "backend" (backend.mo) will be consulted for Translation.

Simple Output of a String:

{'Desktop'|gettext:backend}

Output using sprintf to set values dynamically:

{'(changed Menutitel; Original is `%s`)'|gettext:backend|sprintf:'123'}
{'Page %s of %s Pages'|gettext:backend|sprintf:'7':'100'}

Output with Translation into a certain Language:

{'Desktop'|gettext:backend:it_IT}

Requirements

You need to name the Place of Translationtables. That is the place where your Translationfolders reside. You can either do this inside the function (see bindtextdomain($sDomain, 'PATH_TO_MY_LANGUAGES_FOLDER');) or do it outside by defining the appropriate constant, e.g.:

define('PATH_TO_MY_LANGUAGES_FOLDER', '/var/www/App/languages');

The structure of the Translationtables must follow the official declaration, e.g.: php gettext translation-files structure

Script

Save this as modifier.getText.php and place it e.g. into the Smarty plugins folder.

You will also find this Script as a Repository on my github Account.

/**
 * @author Guido K.B.W. Üffing `<info ueffing net>`
 * @license GNU GENERAL PUBLIC LICENSE Version 3
 * 
 * Smarty plugin
 * Type: modifier
 * Name: smarty_modifier_gettext
 * Version: 1
 * Date: 2013-07-19
 * @see https://blog.ueffing.net/post/2013/07/19/php-smarty-a-modifier-for-internationalization-tool-gettext/
 *
 * @access public
 * @var $sString String to be translated
 * @var $sDomain e.g. "backend"; means the File (backend.mo) which will be consulted for Translation
 * @var $sLang Translation into a certain Language, e.g. "de_DE"
 * @return string translated String
 */
function smarty_modifier_getText ($sString, $sDomain = 'backend', $sLang = 'de_DE')
{       
    if (empty($sString))
    {
        return gettype ($sString);
    }

    // requires installation of php module: php{Version}-intl (and maybe libicu52)
    // 
    // This function needs a "BCP 47 compliant language tag"
    // what is per definition, using a dash instead of an underscore
    // @see http://www.php.net/manual/de/locale.setdefault.php 
    //      http://en.wikipedia.org/wiki/IETF_language_tag
    \Locale::setDefault(str_replace('_', '-', $sLang));

    // Setting the proper Codeset
    // here, don't use a dash '-' 
    $sCodeset = 'UTF8';  

    putenv('LANG=' . $sLang . '.' . $sCodeset);
    putenv('LANGUAGE=' . $sLang . '.' . $sCodeset); 

    // set Codeset
    bind_textdomain_codeset($sDomain, $sCodeset);       

    // name the Place of Translationtables
    // That is where your Translationfolders reside
    bindtextdomain($sDomain, '/tmp'); # flush first
    bindtextdomain($sDomain, 'PATH_TO_MY_LANGUAGES_FOLDER');

    // set locale
    setlocale(LC_MESSAGES, ""); # flush first
    setlocale(LC_MESSAGES, $sLang.'.'.$sCodeset); 

    // Translation will be loaded from
    // e.g.: /var/www/App/languages/de_DE/LC_MESSAGES/backend.mo
    textdomain($sDomain);

    // return, so that further modifiers could handle it
    return gettext($sString);
}

Links

This website uses Cookies to provide you with the best possible service. Please see our Privacy Policy for more information. Click the check box below to accept cookies. Then confirm with a click on "Save".