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) ;
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With