📜 ⬆️ ⬇️

I18n without URL

Recently at work I encountered an interesting, in my opinion, task. The developed site implied the use of three languages ​​- Ukrainian, Russian, English. The task was solved using the symfony 1.2 framework. In principle, the standard, except for one item. The Russian version of the URL should look like this: frontend_dev.php / articles / about English and Ukrainian, respectively, frontend_dev.php / eng / articles / about frontend_dev.php / ukr / articles / about.

Solved the problem in this way. First of all, we create two paths in the routing.yml file — one for the default language version, which does not display the language in the URL, the other for all the others. It looks like this:
page:
url: /:sf_culture/:first/:second/:third
requirements: { sf_culture: (?:rus|eng|ukr) }
param: { module: page, action: list, first: none, second: none, third: none}

page_no_lang:
url: /:first/:second/:third
param: { module: page, action: pageNoLang, first: none, second: none, third: none}



Obviously, we create two Actions, in one of them pageNoLangSuccess () we insert the translation string into the required language:
$culture = $this->getUser()->setCulture('rus');
// -


In the second, we simply perform the actions on the data as usual. However, since the actions are the same for both Actions, I made them into a separate function - so as not to repeat.
Links to switch language look almost standard:
<?php echo @link_to('rus', '@page_no_lang?sf_culture=rus'.$link, '') ?> |
<?php echo @link_to('en', '@page?sf_culture=eng'.$link) ?> |
<?php echo @link_to('ukr', '@page?sf_culture=ukr'.$link) ?>

but the transition to the default language is done through the path @page_no_lang
The only unpleasant moment - links to internal structures. Then I had to abandon link_to and manually register for the Russian language:
<?php if($sf_user->getCulture() == 'rus' ): ?>
a href="frontend_dev.php/articles">

<? php else:?>
<? php echo link_to ('Link', '@articles); ?>
<? php endif; ?>
')

That seems to be all. The solution, in my opinion, is not very elegant, but it works. If someone has an idea how to make it more beautiful, thanks in advance.

Source: https://habr.com/ru/post/57672/


All Articles