Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Translate url parameters as place holder

I wondering how to translate a URL in ZF2 that has a parameter on it.

For example:

/{:language_link-schools-:city_link}

The reason why I don't do:

/:language_link-{schools}-:city_link

It is because in some languages, for example, Spanish, the order of the words will change.

I am using PhpArray, and when I translate it, the parameters are not replaced, therefore the URL is rendered as (example in Spanish):

/:language_link-escuela-:city_link

Instead of the expected behaviour:

/ingles-escuela-miami

Edit:

The parameters are :language_link and :city_link

So the idea is that in one language the rendered URL could be:

 /:language_link-schools-:city_link 

and in another language it could be:

/:language_link-:city_link-school

Similarly as it is done when you translate a statement doing:

sprintf($this->translate('My name is %s'), $name) ;
like image 632
peterpeterson Avatar asked Dec 03 '16 17:12

peterpeterson


1 Answers

There is a function in PHP called strtr. It allows translating any pattern into values.

With your example, we can do the following:

If the string is like this: /:language_link-escuela-:city_link

Then you can do the following

<?php
$rawUrl = "/:language_link-escuela-:city_link";

$processedUrl = strtr($rawUrl, [
  ':language_link' => 'es',
  ':city_link' => 'barcelona',
]);

echo $processedUrl; // Output: /es-escuela-barcelona
like image 191
asiby Avatar answered Nov 15 '22 20:11

asiby