1

I'm trying to translate my site into different languages, thanks to Yii::t() I have translated all texts. But I'm using pretty urls and it should translate them for better user experience.

I'm using the component. translate-manager by lajax to make translations

I have tried to add Yii::t() keys of the rules, I can translate them, but when you change language ignores translation and Yii use the original rule.

This is my urlManager:

'urlManager' => [
    'enablePrettyUrl' => true,
    'showScriptName' => false,
    'enableStrictParsing' => false,
    'rules' => [
        ['pattern' => '<id:rss>', 'route' => 'rss/default/index', 'suffix' => '.xml'],
        ['pattern' => '<id:rss>', 'route' => 'rss/default/index'],
        ['pattern' => 'sitemap', 'route' => 'sitemap', 'suffix' => '.xml'],
        '/' => 'site/index',
        Yii::t('config main', '<site:(about|contact|login|logout|signup|policy|conditions|blog)>') => 'site/<site>',
        Yii::t('config main', '<device:\w+>/compare-price') => 'items/compareprices',
        Yii::t('config main', 'versus/<brand1:\w+>-<model1:[\w\-]+>-<brand2:\w+>-<model2:[\w\-]+>') => 'items/versus',
        Yii::t('config main', 'versus/<brand1:\w+>-<model1:[\w\-]+>-<brand2:\w+>-<model2:[\w\-]+>-<brand3:\w+>-<model3:[\w\-]+>') => 'items/versus',
        Yii::t('config main', 'versus/<brand1:\w+>-<model1:[\w\-]+>-<brand2:\w+>-<model2:[\w\-]+>-<brand3:\w+>-<model3:[\w\-]+>-<brand4:\w+>-<model4:[\w\-]+>') => 'items/versus',
        Yii::t('config main', '<device: \w+>/advanced-search') => 'items/advancedsearch',
     ],
],

Exemple:

domain.com/conditions will be domain.com/condiciones in spanish

How do I have to implement internationalization in the urls?

Sageth
  • 1,102
  • 1
  • 15
  • 30
  • Check http://stackoverflow.com/questions/30620404/yii2-create-translated-urls –  Apr 07 '16 at 08:38
  • @stig-js The answer to that question is not what I try to do, they propose to differentiate the language by adding en/de/es/fr, I want to translate the url – Sageth Apr 07 '16 at 08:47
  • I've replyied for my working solution on: http://stackoverflow.com/questions/30620404/yii2-create-translated-urls/40776550#40776550 – nacesprin Nov 24 '16 at 11:38

1 Answers1

2

First of all it's not a good decision, but i'm using this approach at the moment, while i'm trying to figure out exactly the same problem:

I'm using the component codemix/yii2-localeurls for language management & language code inserted in URL.

    'urlManager' => [
        'class' => 'codemix\localeurls\UrlManager',
        'languages' => ['ru', 'en'],
        'showScriptName' => false,
        'enablePrettyUrl' => true,
        'enableLanguageDetection' => true,
        'rules' => array(
            'отдых-в-греции-цены|vacation-in-greece-prices' => 'site/prices',
        ),
    ],

I have 'отдых-в-греции-цены' in translations and my link in site is like:

<?= Html::a(Yii::t('app', 'Цены'), ['/'.Yii::t('app', 'отдых-в-греции-цены')]) ?>

As i said it's not a best practice but at the moment a can't find a better approach. Also the site is very small and i don't need complex routes like yours, but i think that it can be implemented.

Regards