-
Notifications
You must be signed in to change notification settings - Fork 56
Internationalization
H2o supports internationalized templates, and translation message extraction and compilation support for h2o templates in the bundled i18n extension.
- php-gettext extension
- gettext (optional ) this is only required you want to use i18n extension to collect and compile translatable strings from your template, Linux/BSD/Mac users can ignore this since gettext should be part of your Operating System. Windows users please read following instructions.
Note: Windows users
Windows user needs to manually install gettext library and a few dependent scripts, I have packaged a gettext package for windows users so template parsing and compiling can be done
1. Download Gettext binary for Windows
2. Windows user will need to specify location of gettext binary you download
<?php require 'h2o/h2o.php'; $template = new H2o('templates/index.html', array( 'i18n' => array('gettext_path' => 'gettext/bin') )); echo $template->render(); ?>
There are two ways to enable i18n extension, you load the extension either in php or your template.
h2o
{% load 'i18n' %}
{% trans "hello world" %}
PHP
<?php
require 'h2o/h2o.php';
h2o::load('i18n');
echo h2o('{% trans "hello world" %}')->render();
?>
Easy uh ?
{% trans "hello world" %}
{% blocktrans %}
In computing, internationalization and localization are means of adapting computer
software to different languages and regional differences.
{% endblocktrans %}
This is a alternative syntax to place translatable messages in your template, so translation messages can be used as an variable. So
- filters can be applied
- used as part of argument
Hello world
{{ _("Hello world") }}
Translatable string using filters.
{{ _("Google") | links_to "http://google.com" }}
// as part of argument
{{ ‘logo.gif’ | image_tag height:350, width:240, alt: _(“Company logo”) }}
h2o-i18n extension require a locale/
folder in your searchpath, create a directory using language code, translatable collection library will automatic create the correct folder structure for you.
myawesome_app/ templates/ layouts/ index.html locale/ fr/ LC_MESSAGES/ messages.po de/
blocktrans tag supports translation on singular or pluralized form.
{% blocktrans count=user.tasks.length %}
{{ user.name }} has {{ count }} task
{% plural %}
{{ user.name }} has {{ count }} tasks
{% endblocktrans %}
Note: variable in the body of blocktrans doesn’t support filtering, but you can create alias of filtered value.{% blocktrans count=user.tasks.length, name = user.name|capitalize %} {{ user.name }} has {{ count }} task {% plural %} {{ user.name }} has {{ count }} tasks {% endblocktrans %}
Setting language variable to template
<?php
require 'h2o/h2o.php';
$template = new H2o('templates/index.html', array(
'i18n' => array('locale' => 'de')
));
echo $template->render();
?>
extract messages from template to po file
<?php
require 'h2o/h2o.php';
$template = new H2o('templates/index.html', array(
'i18n' => array('locale' => 'de', 'extract_message'=> true)
));
echo $template->render();
?>
compile po file into translation binary (extension .mo) file
<?php
require 'h2o/h2o.php';
$template = new H2o('templates/index.html', array(
'i18n' => array('locale' => 'de', 'compile_message'=> true)
));
echo $template→render();
?>