Warning! This page has the following tags:

  • This page needs to be merged with docbook documentation.

Building a Simple Multilingual Site

This brief tutorial shows the basic configuration for a simple multilingual site. It's a naively simplified example and doesn't use half the fancy stuff Agavi has to offer. The idea is to get the reader familiar with the configuration behind Agavi's i18n components.

Basic Settings

settings.xml

...

    <settings>
       ...
      <setting name="use_translation">true</setting>
    </settings>

...

output_types.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<configurations>
  <configuration>
    <output_types default="html">
      <output_type name="html">
        <renderer class="AgaviPhpRenderer">
          <parameters>
            <parameter name="assigns">
              <parameters>
                <parameter name="routing">ro</parameter>
                <parameter name="request">req</parameter>
                <parameter name="controller">ctl</parameter>
                <parameter name="user">user</parameter>
<!-- Translation manager is automatically assigned to a template variable $tm -->
                <parameter name="translation_manager">tm</parameter>
              </parameters>
            </parameter>
<!-- 
  Templates are going to be named MyActionSuccess.en.php and so on...
  Other mode options are 'prefix' (en.MyActionSuccess.php) and 'subdir' (en/MyActionSuccess.php)
-->
            <parameter name="i18n">
              <parameters>
                <parameter name="mode">postfix</parameter>
                <parameter name="separator">.</parameter>
              </parameters>
            </parameter>
          </parameters>
        </renderer>
        <parameters>
          <parameter name="Content-Type">text/html;  charset=UTF-8</parameter>
        </parameters>
      </output_type>
    </output_types>
  </configuration>
</configurations>

translation.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<configurations>
  <configuration>

    <available_locales default_locale="en">
      <available_locale identifier="en">
        <parameters>
          <parameter name="description">English</parameter>
        </parameters>
      </available_locale>
      
      <available_locale identifier="fi">
        <parameters>
          <parameter name="description">Suomi / Finnish</parameter>
        </parameters>
      </available_locale>
      
    </available_locales>

    <translators default_domain="default">
      <translator domain="default">

        <message_translator class="AgaviSimpleTranslator">
          <parameters>
            <parameter name="menu">
              <parameter name="fi">
                <parameter name="Home">Etusivu</parameter>
                <parameter name="Products">Tuotteet</parameter>
                <parameter name="Links">Linkkejä</parameter>
                <parameter name="Contact">Yhteystiedot</parameter>
              </parameter>
            </parameter>
          </parameters>
        </message_translator>

        <date_formatter>
          <parameters>
            <parameter name="type">date</parameter>
            <parameter name="format">full</parameter>
          </parameters>
        </date_formatter>

      </translator>
    </translators>

  </configuration>

</configurations>

routing.xml

Check out The Agavi Sample App to learn how to use a callback to make sure the selected locale actually exists.

<route pattern="^/({locale:[a-z]{2}(_[A-Z]{2})?})" stop="false" imply="true" cut="true" locale="${locale}" >
  <ignores>
    <ignore>locale</ignore>
  </ignores>
</route>

Templates

Localized templates

MyActionSuccess.en.php

Using Translated Strings

Master.php

...
<body>
  <div id="langmenu">
    <ul>
      <li><a href='<?php echo $ro->gen(null, array('locale' => 'en')); ?>'>English</a></li>
      <li><a href='<?php echo $ro->gen(null, array('locale' => 'fi')); ?>'>Suomeksi</a></li>
    </ul>
  </div>
...
  <div id='mainmenu'>
    <ul>
      <li><a href='<?php echo $ro->gen('home')?>'><?php echo $tm->_('Home', 'default.menu'); ?></a></li>
      <li><a href='<?php echo $ro->gen('products')?>'><?php echo $tm->_('Products', 'default.menu'); ?></a></li>
      <li><a href='<?php echo $ro->gen('links')?>'><?php echo $tm->_('Links', 'default.menu'); ?></a></li>
      <li><a href='<?php echo $ro->gen('contact')?>'><?php echo $tm->_('Contact', 'default.menu'); ?></a></li>
    </ul>
  </div>
...