Welcome Guest, Not a member yet? Register   Sign In
Translated Routes
#1

(This post was last modified: 07-19-2020, 01:18 PM by kilden.)

Hello,

I've got a question about localization and translated routes :
I wonder if there is a solution to combine  'named routes' and the 'lang() function' to translate URL and keep the app very simple to update for future developments.

First I made my translations in Routes.php like this :


PHP Code:
/*In my template-view header.php*/ 
echo '<a class="menu" href="'.$base.route_to('studio_'.$locale).'">'.lang('App.menu.studio').'</a>';  

/* In Routes.php (translations in Routes for all languages and all routes... :-/ )*/ 
$routes->get('{locale}/the_studio''Home::index', ['as' => 'studio_en']); 
$routes->get('{locale}/l_atelier''Home::index', ['as' => 'studio_fr']);  

/*In app/Language/en/app.php (only for hyperlink)*/ 
return [  'menu'  => [    'studio' => 'the studio'  ] ]; 


If possible I would like to put all translations in app/Language... I've tried this without success :


PHP Code:
/*In my template-view header.php*/ 
echo '<a class="menu" href="'.$base.route_to('studio').'">'.lang('App.menu.studio').'</a>';  

/* In Routes.php */ 
$routes
->get('{locale}/'.lang('App.url.studio'), 'Home::index', ['as' => 'studio']);  

/*In app/Language/en/app.php */ 
return [  
   
'url'  => [    'studio' => "the_studio"  ],  
   
'menu'  => [    'studio' => 'the studio'  ]
]; 


It almost works, because Routes.php reads a part of the translation... but just the default language. there is no update of the locale with {locale}... Do you know a way to do it ?

Thank you for any help !
Reply
#2

(This post was last modified: 07-21-2020, 02:57 AM by kilden.)

So, Is there no solution to avoid translations into routes.php ?

The best I could do (because the function 'lang' in Routes.php doesn't recognize current locale) looks like this :

PHP Code:
/*in Routes.php*/
$routes->get('fr/'.lang('App.url.art_direction_fr'), 'Pages::art_direction', ['as' => 'art_direction_fr']);
$routes->get('en/'.lang('App.url.art_direction_en'), 'Pages::art_direction', ['as' => 'art_direction_en']);

/*in view header.php*/
echo '<li><a class="menu" href="'.$base.route_to('art_direction_'.$locale).'">'.lang('App.menu.art_direction').'</a></li>';

/*in app/Language/fr/app.php ('fr' is the default language)*/
return [
  'url'  => [
    'art_direction_fr' => 'direction_artistique',
    'art_direction_en' => 'art_direction',  ],

  'menu'  => [ 'art_direction' => 'direction artistique';  ]
]; 
But with that solution, it's not very clean...
Routes.php doesn't recognize current $locale, so I need two lines... And all translations of urls are in app/Language/fr/app.php which is pretty dumb and illogical.

Reply
#3

ups no... Undecided lang() inside Routes.php really doesn't work at all... My last post is wrong
Reply
#4

(This post was last modified: 07-21-2020, 06:38 AM by nicojmb.)

(07-21-2020, 04:38 AM)kilden Wrote: ups no... Undecided lang() inside Routes.php really doesn't work at all... My last post is wrong

try with reverse routes and put lang() in routes PHP

https://codeigniter4.github.io/userguide...se-routing

or extend core class to support this function

https://codeigniter4.github.io/userguide...re-classes
Reply
#5

(This post was last modified: 07-24-2020, 05:17 AM by kilden.)

According to your advice I've tried a lot of ways but always without success.
Even with reverse routes, I think you can't put lang() in routes...
Reply




Theme © iAndrew 2016 - Forum software by © MyBB