25

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) ;
12
  • So your parameters are: :language_link-schools- and :city_link which both of them need to be translated? Not really clear as you use "_" and "-" within the same parameter? Could you show a bit more of your routing as it would be nice to see how you've set it up aswell as the the part were you're translating within an actual example.
    – Kwido
    Dec 4, 2016 at 11:54
  • no, the parameters are :language_link and :city_link, so the idea is that in one language the 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 using sprintf('My name is %s', $name) Dec 7, 2016 at 14:29
  • 2
    So what is the benefits of the order of the parameters for the enduser? You're most likely to build links to let the enduser navigate around within your application. But too bad I've no clue how you want to achieve the ordering of your parameters. PS: update your question with what you enlightened in the comment with the order of your parameters and show both ways you want the output.
    – Kwido
    Dec 7, 2016 at 14:40
  • There is the usability benefit, the end user will be able to remember urls if is properly translated, and for SEO too. Dec 7, 2016 at 14:55
  • 4
    I don't thing it's possible in that way. In your examples, {schools} route url part also schould be a placeholder. But in that case, :language_link should be hard-coded. If you know, how many languages do you have using, just create set of rules for each one with the same controller and action. But if not, you should provide external configuration for that and build it with using factory (and maybe cache for increase performance). Othet way you could create custom route rule, register it, and depend from your configuration resolves parameters in correct way.
    – VirCom
    May 7, 2017 at 12:55

1 Answer 1

1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.