Multi language site


#1

Is there somewhere a good tutorial on how to build a multi-language site (in cakephp 3.x) ?
There are several questions about this topic but no real answer.

Thank you for helping.


#2

Can we help you with some specific problem?


#3

Well, I do not have a specific problem.
I have been building web apps / sites with cakephp since 1.2.x but only single language (french).
A new client is asking for several languages in a new website.
What I would like to know is what are the best practices ?
Should I create everything 2, 3 or more times ? Should I use a joining table ?
How to localize things that are customized ?

I just don’t know where to begin ?


#4

not sure if this is what you looking for, here’s CakePHP3 translation and localization official documentation.

https://book.cakephp.org/3.0/en/core-libraries/internationalization-and-localization.html
https://book.cakephp.org/3.0/en/orm/behaviors/translate.html

hope it might help for you to start


#5

try this: https://github.com/ADmad/cakephp-i18n


#6

Thank you for replying.
donnidani : the first link is about translating strings and I already read this page. It’s not about methods and organization. The second link seems interesting and I missed it. I’ll take a look.

Thanks again all.


#7

Hello!


There is my example simple multilanguage system (full source).

Define native language in config/app.conf <- ‘defaultLocale’ => env(‘APP_DEFAULT_LOCALE’, ‘pl_PL’), (polish is nativa lang of app).

In Template->Laypout->Default place switches with flags, for example:

<?php //if (isset($lang) && ($lang == 'pl_PL')) { echo '
  • ' . $this->Html->link($this->Html->image('PL.png'), ['action' => 'changeLang', 'pl_PL'], array('escape' => false)) .'
  • '; echo '
  • ' . $this->Html->link($this->Html->image('GB.png'), ['action' => 'changeLang', 'en_US'], array('escape' => false)) . '
  • '; echo '
  • ' . $this->Html->link($this->Html->image('DE.png'), ['action' => 'changeLang', 'de_DE'], array('escape' => false)) . '
  • '; ?>

    In src/locale (create if not exist), place localisations folders, for example: en_US, de_DE, pl_PL
    create single file named: default.po

    <?php msgid "Środowisko serwera" <- native app lang msgstr "Environment" <- translate msgid "Operator" msgstr "Employer" ?>

    in Controllers, Views: __(‘Środowisko serwera’) <- this can be translate to “Environment”.

    in src/Controller/AppController.php create 2 functions:
    public function changeLang($lang = ‘pl_PL’)
    {
    $this->Cookie->write(‘lang’, $lang);
    return $this->redirect($this->request->referer());
    }
    public function getLang($lang = ‘pl_PL’)
    {
    $lang = $this->Cookie->read(‘lang’);
    if (empty($lang)) { return ‘pl_PL’; } else return $lang;
    }

    Regards
    Leszek


    #8

    i did some multilanguage sites before and there are few tips i can give you:

    1. add context to every string - use __x() and __dx()
    2. add description in context to agruments ie. “{0} - city name” OR use named argument {cityName}
    3. try to keep domains in some logic, ie. emails, logs
    4. some of translation can expand text length by huge margin - can break your layout
    5. some languages are gendered so its know beforehand if you want to expand translation to it
    6. i add language to URL as route part ie example.com/en_GB/home but root path example.com is using HTTP_ACCEPT_LANGUAGE request header and redirects to best fit language
    7. longer text are better in separate templates instead of using __() - ie. ToS_en_GB.ctp

    #9

    SKyer, Graziel
    Thank you very much for sharing.
    This is very helpful.