Fork me on GitHub

Translation: Usage

This page shows an example of how translation can be used.

Single Translation Key

In this example we are displaying a single translation for key hathoora_hello_world.

In the controller, the code looks like this:

                
                    $helloTranslation = $this->getService('translation')->t(
                        'hathoora_hello_world', array('name' => 'World')
                    );
                
            

In this example we are passing token 'name' and the output is:

                
                    Hello, World                
            

Route Based Translation Keys

Above example will make one call to get one translation, if you need to fetch multiple translations you can associate translations to a route and fetch them. This example is using hathoora_route_example_title and hathoora_route_example_body. The controller code looks like this:

                
                    $routeTranslations = $this->getService('translation')->getRouteTranslations('hathoora_translation_route',
                        array(
                            'hathoora_route_example_title' => array(
                                'date' => date('m/d/y H:i:s')
                            ),
                            'hathoora_route_example_body' => array(
                                'link' => 'http://hathoora.org'
                            )
                        )
                    );
                
            

In this example we are getting all translations associated with route hathoora_translation_route. We are also passing tokens date and link to keys hathoora_route_example_title and hathoora_route_example_body respectively.

The output is shown below.

                
                    hathoora_route_example_title : Today is: 07/18/18 21:45:29
                    hathoora_route_example_body: hathoora_route_example_body                
            

Now click here to toggle language to see the difference.

Filters

Using translation inside template and using filters. In this example we are using key hathoora_hello_world_filter.

                
                    echo $this->getService('translation')->t(
                                        'hathoora_hello_world_filter', array('name' => ' Hathoora PHP Framework '));
                
            

The translation key has the following translation for en_US

                
                    Name is trimmed: "{{name|trim}}"<br/>
                    <br/>
                    Custom filter: "{{name|customFilter(3)|trim}}"
                
            

In this example we are using builtin trim filter and a custom filter called and customFilter which takes one parameter.

And we added a custom filter class that contains a static customFilter like so in config.

                
                    # File admin/config/config_prod.yml

                    hathoora:
                        translation:
                            ....

                        # filter used in translation helper
                        detokenizerFilters:
                            - \admin\helper\translationFilter
                
            

The output of result is shown below.

                
                    Name is trimmed: "Hathoora PHP Framework"

Custom filter: "hathoora php framework ---- hathoora php framework ---- hathoora php framework ----"
Page Took:         12.58 msec
Page Memory:   700.29k
admin / translationController / example (200)
Request UUID: 5b4fb4f94ac1f9.00110937
Get
Post
Cookies
Files
Session
Server
Response
No data.
No data.
No data.
No data.
No data.
REDIRECT_HATHOORA_ENV
prod
REDIRECT_STATUS
200
HATHOORA_ENV
prod
HTTP_HOST
www.hathoora.org
HTTP_X_FORWARDED_FOR
54.198.104.202
HTTP_X_REAL_IP
54.198.104.202
HTTP_CONNECTION
close
HTTP_ACCEPT_ENCODING
x-gzip, gzip, deflate
HTTP_USER_AGENT
CCBot/2.0 (http://commoncrawl.org/faq/)
HTTP_ACCEPT
text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
PATH
/sbin:/usr/sbin:/bin:/usr/bin
SERVER_SIGNATURE
Apache/2.2.15 (CentOS) Server at www.hathoora.org Port 80
SERVER_SOFTWARE
Apache/2.2.15 (CentOS)
SERVER_NAME
www.hathoora.org
SERVER_ADDR
192.168.1.11
SERVER_PORT
80
REMOTE_ADDR
207.126.164.119
DOCUMENT_ROOT
/var/websites/hathoora.org/docroot
SERVER_ADMIN
webmaster@hathoora.org
SCRIPT_FILENAME
/var/websites/hathoora.org/docroot/index.php
REMOTE_PORT
46240
REDIRECT_URL
/admin/translation/example
GATEWAY_INTERFACE
CGI/1.1
SERVER_PROTOCOL
HTTP/1.0
REQUEST_METHOD
GET
QUERY_STRING
REQUEST_URI
/admin/translation/example
SCRIPT_NAME
/index.php
PHP_SELF
/index.php
REQUEST_TIME_FLOAT
1531950329.305
REQUEST_TIME
1531950329
Status: 200
Headers:
Content-Type
text/html; charset=UTF-8
Configutation
Key Value
app.admin
Array
(
    [pattern] => /admin(|/)
    [directory] => hathoora
    [namespace] => hathoora\admin
)
app.docs
Array
(
    [pattern] => /docs(|/)
    [directory] => hathoora
    [dispatcher] => Array
        (
            [class] => customDispatcher
            [method] => dispatch
        )

)
app.site
Array
(
    [default] => 1
)
hathoora.logger
Array
(
    [profiling] => Array
        (
            [enabled] => 1
        )

    [logging] => Array
        (
            [enabled] => 1
            [level] => DEBUG
        )

    [webprofiler] => Array
        (
            [enabled] => 1
            [system] => 
            [show_redirects] => 
        )

)
hathoora.template
Array
(
    [engine] => Array
        (
            [name] => Stuob
        )

)
hathoora.database
Array
(
    [default] => mysql://dbuser:dbpassword@dbhost:3306/dbname
)
hathoora.translation
Array
(
    [enabled] => 1
    [cache_service] => @cache@
    [cache_time] => 86400
    [dsn] => default
    [show_empty] => 1
    [languages] => Array
        (
            [0] => en_US
            [1] => fr_FR
        )

    [default_language] => en_US
    [debug] => Array
        (
            [method] => GET
            [parameter] => tkDebug
        )

)
hathoora.detokenizerFilters
Array
(
    [0] => hathoora\admin\helper\translationFilter
)
hathoora.cache
Array
(
    [pools] => Array
        (
            [common] => Array
                (
                    [driver] => memcache
                    [servers] => Array
                        (
                            [0] => Array
                                (
                                    [host] => localhost
                                    [port] => 11211
                                )

                        )

                )

        )

)
services.cache_base
Array
(
    [class] => \hathoora\cache\cache
    [calls] => Array
        (
            [setContainer] => Array
                (
                    [0] => @container@
                )

        )

    [type] => static
)
services.cache
Array
(
    [factory_service] => @cache_base@
    [factory_method] => pool
    [factory_method_args] => Array
        (
            [0] => common
        )

)
services.container
Array
(
    [class] => \hathoora\container
    [method] => getContainer
)
services.translation
Array
(
    [class] => \hathoora\translation\translator
    [type] => static
    [calls] => Array
        (
            [setTKConfig] => Array
                (
                    [0] => %hathoora.translation%
                )

            [setCacheService] => Array
                (
                    [0] => %hathoora.translation.cache_service%
                )

        )

)
Logging (25)
hathoora.logger.logging.enabled: 1
# Time (msec) Level Memory (KB) Message
1 4.313 DEBUG 320.63 Configuration loaded:
Array
(
    [0] => app_prod.yml
    [1] => config_prod.yml
    [2] => config_prod.yml
)
2 4.376 DEBUG 320.41 Configuration loaded:
Array
(
    [0] => app_prod.yml
    [1] => config_prod.yml
    [2] => config_prod.yml
)
3 4.503 DEBUG 322.30 Service "translation" has been added because of hathoora.translation.enabled.
4 4.700 DEBUG 335.07 Listener "kernel.terminate[webprofiler]" has been added because of hathoora.logger.webprofiler.enabled
5 4.876 DEBUG 338.46 Event kernel.ready has been registered.
6 5.248 DEBUG 372.53 Event kernel.route has been registered.
7 5.368 DEBUG 381.70 Event kernel.controller has been registered.
8 5.413 DEBUG 382.63 Calling controller (\hathoora\admin\controller\translationController::example) with following params
Array
(
)
9 6.664 INFO 496.47 CACHE (pool = common) get: translation:key:hathoora_hello_world:en_US, status: 0
10 7.394 INFO 532.33 mySQL connected to default
11 7.539 INFO 533.46 SELECT translation, tk.translation_id FROM translation_key tk INNER JOIN translation_value tv ON (tk.translation_id = tv.translation_id) WHERE tk.translation_key = "hathoora_hello_world" AND tv.language = "en_US" LIMIT 1
12 8.123 INFO 543.09 CACHE (pool = common) set: translation:key:hathoora_hello_world:en_US, status: 1
13 8.489 INFO 549.46 CACHE (pool = common) get: translation:route:hathoora_translation_route:en_US, status: 0
14 8.618 INFO 551.38 SELECT translation_key, translation FROM translation_key tk INNER JOIN translation_value tv ON (tk.translation_id = tv.translation_id) INNER JOIN translation_route tr ON (tk.translation_id = tr.translation_id) WHERE tr.route = "hathoora_translation_route" AND tv.language = "en_US"
15 9.036 INFO 555.84 CACHE (pool = common) set: translation:route:hathoora_translation_route:en_US, status: 1
16 9.660 INFO 641.19 Template (components/leftPanel.tpl.php) loaded
17 9.957 INFO 642.05 CACHE (pool = common) get: translation:key:hathoora_hello_world_filter:en_US, status: 0
18 10.095 INFO 643.97 SELECT translation, tk.translation_id FROM translation_key tk INNER JOIN translation_value tv ON (tk.translation_id = tv.translation_id) WHERE tk.translation_key = "hathoora_hello_world_filter" AND tv.language = "en_US" LIMIT 1
19 10.514 INFO 648.34 CACHE (pool = common) set: translation:key:hathoora_hello_world_filter:en_US, status: 1
20 10.991 INFO 660.98 Template (/var/websites/hathoora.org/app/hathoora/docs//resources/templates/components/htmlMeta.tpl.php) loaded
21 11.132 INFO 662.16 Template (/var/websites/hathoora.org/app/hathoora/docs//resources/templates/components/flashMessages.tpl.php) loaded
22 12.239 INFO 616.83 Template (translation/example.tpl.php) fetched.
23 12.298 DEBUG 616.25 Event kernel.response has been registered.
24 12.340 DEBUG 617.44 Event kernel.terminate has been registered.
25 12.455 DEBUG 620.66 Event kernel.terminate->webprofiler has been notified.
Profiling
cache
db
template
# Time (msec) Pool Method Key Status Took (msec)
1 6.385 common get translation:key:hathoora_hello_world:en_US 0 0.134
2 7.911 common set translation:key:hathoora_hello_world:en_US 1 0.134
3 8.319 common get translation:route:hathoora_translation_route:en_US 0 0.093
4 8.836 common set translation:route:hathoora_translation_route:en_US 1 0.115
5 9.785 common get translation:key:hathoora_hello_world_filter:en_US 0 0.087
6 10.329 common set translation:key:hathoora_hello_world_filter:en_US 1 0.109
# Time (msec) DSN Query Took (msec)
1 7.441 default SELECT translation, tk.translation_id
FROM translation_key tk
INNER JOIN translation_value tv ON (tk.translation_id = tv.translation_id)
WHERE
tk.translation_key = "hathoora_hello_world" AND
tv.language = "en_US"
LIMIT 1
0.292
(0.413)
2 8.534 default SELECT translation_key, translation
FROM translation_key tk
INNER JOIN translation_value tv ON (tk.translation_id = tv.translation_id)
INNER JOIN translation_route tr ON (tk.translation_id = tr.translation_id)
WHERE
tr.route = "hathoora_translation_route" AND
tv.language = "en_US"
0.230
3 10.012 default SELECT translation, tk.translation_id
FROM translation_key tk
INNER JOIN translation_value tv ON (tk.translation_id = tv.translation_id)
WHERE
tk.translation_key = "hathoora_hello_world_filter" AND
tv.language = "en_US"
LIMIT 1
0.226
(0.269)
# Time (msec) Name Cached Took (msec)
1 9.191 translation/example.tpl.php 0 2.997