Giter Club home page Giter Club logo

fetranslator's Introduction

FeTranslator

ZF2 Module for translating routematches and messages

Installation

  1. ./composer.phar require jdolieslager/fetranslator
  2. Add FeTranslator to your application.config.php

Default Settings

You can override the default language. Add data/fe_translator.local.php.dist to your config/autoload folder

Add translations

To add translations you can add the following section to your config. The translator will search for .php in the given directory. (Example: nl_NL.php or en_US.php)

<?php
return array(
    'fe_translator' => array(
        'translations' => array(
            // Config below will be used to translate messages for given namespace
            array(
                'base_dir'    => __DIR__ . '/../translations/module/',
                'namespace'   => 'application', // Application indicates the namespace
            ),
            // Config below will be used to translate route matches
            array(
                'base_dir'    => __DIR__ . '/../translations/route_match/',
                'namespace'   => FeTranslator\Translator\Translator::NAMESPACE_ROUTE_MATCH,
            )
        ),
    ),
);

Normal translation file example

<?php
return array(
    'head' => array(
        'title' => 'My application title',
    ),
    'level1' => array(
        'level2' => 'My value',
    ),
);

RouteMatch translation file example

<?php
return array(
    'home/application'  => array(
        'controller'    => array(
            'Application\Controller\Gebruiker' => 'Application\Controller\User',
        ),
        '__CONTROLLER__' => array(
            'gebruiker' => 'user',
        ),
        'action'        => array(
            'inloggen'  => 'login',
            'uitloggen' => 'logout',
        ),
    ),
);

Use Translations in controllers and services

You can add a trait for faster access to the Translator service. Requirements is: php >= 5.4 and the class should have a method getServiceLocator()

<?php
class Controller extends AbstractActionController
{
    use \FeTranslator\ServiceManager\ServiceTrait;
    
    public function indexAction()
    {
        $translator = $this->getFeTranslateService();
        
        // Translate a message defined in nl_NL.php as array('level1' => array('level2' => 'value'));
        // The translations are loaded in the application namespace
        $translation = $translator->translate('level1.level2', 'application')
        
        //Get an translated URL
        // Routematch is: application/home with a controller User and with the action Login
        $translatedUrl = $translator->translateUrl(
            'application/home', 
            array('controller' => 'user', 'action' => 'login')
        );
    }
}

Use Translations in templates

    <?php
        $url = $this->feTranslateUrl('application/home', array('controller' => 'User', 'action' => 'login'));
    ?>
    <html>
        <head>
            <title><?= $this->feTranslate('head.title', 'application'); ?></title>
        </head>
        <body>
            <a href="<?= $url; ?>"><?= $this->feTranslate('my_link'); ?></a>
        </body>
    </html>

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.