Jump to content

Use of Translation in Smarty Templates


Andreas Augustin
 Share

Recommended Posts

Hi Andreas,

Here is one way of how I'm doing it in combination with the TemplateEngineFactory + TemplateEngineSmarty modules. Basically I'm storing all my translation keys in a php file "strings.php" using this file as textdomain. Values are translated in the ProcessWire backend. Then I'm using an autoload module which adds a translation function to smarty:

<?php

class SmartyTranslationExtension extends WireData implements Module
{

    /**
     * getModuleInfo is a module required by all modules to tell ProcessWire about them
     *
     * @return array
     *
     */
    public static function getModuleInfo()
    {
        return array(
            'title' => '',
            'version' => 100,
            'summary' => '',
            'singular' => true,
            'autoload' => true,
        );
    }


    /**
     * Initialize the module
     *
     * ProcessWire calls this when the module is loaded. For 'autoload' modules, this will be called
     * when ProcessWire's API is ready. As a result, this is a good place to attach hooks.
     *
     */
    public function init()
    {
        $this->addHookAfter('TemplateEngineSmarty::initSmarty', $this, 'hookSmarty');
    }


    /**
     * Wrapper to return a translation from a given translation file
     * Default keys are in /site/templates/translations/strings.php
     * Note: The translation is returned in the current language of the user object
     *
     * @param string $key
     * @param string $file
     * @return string
     */
    public function txt($key, $file = 'strings')
    {
        $string = __($key, "/site/templates/translations/$file.php");
        // We remove any html encodings to allow HTML tags in our translations
        return wire('sanitizer')->unentities($string);
    }

    public function hookSmarty(HookEvent $event)
    {
        $smarty = $event->arguments('smarty');
        $smarty->registerPlugin('function', 'txt', array($this, 'txtSmarty'));
    }

    public function txtSmarty(array $params, $smarty)
    {
        $file = isset($params['file']) ? $params['file'] : 'strings';
        $key = isset($params['key']) ? $params['key'] : '<MISSING TRANSLATION KEY>';
        return $this->txt($key, $file);
    }

}

 

Now, I can output my translations in a smarty template like this:

{txt key="hello_world"} --> Outputs translation value (DE, EN, FR...) corersponding to hello_world key

Note that this only works if you are using the TemplateEngineFactory module together with smarty as engine.

Hope it helps!

Cheers

  • Like 2
Link to comment
Share on other sites

It is a PHP file containing the translation keys. You could also enter the translation values for the default language, but I prefer to work with keys on code level so I can translate them to different values for each language in the ProcessWire backend

<?php
__('show_video'); // Video ansehen
__('more'); // mehr
__('case_studies'); // Case-Studies
__('call_us_for_advise'); // Rufen Sie uns für eine unverbindliche Beratung an!
__('map'); // Anfahrt
__('contact_form'); // Kontaktformular
__('overview'); // Übersicht

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...