peterpeterson
peterpeterson

Reputation: 1315

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) ;

Upvotes: 25

Views: 1848

Answers (1)

asiby
asiby

Reputation: 3389

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

Upvotes: 1

Related Questions