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) ;
Shehan Dhaleesha
  • 627
  • 1
  • 10
  • 30
peterpeterson
  • 1,315
  • 2
  • 14
  • 38
  • 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 04 '16 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) – peterpeterson Dec 07 '16 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 07 '16 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. – peterpeterson Dec 07 '16 at 14:55
  • Not familiar with ZF2 but would it be possible to `sprintf('%0$s-school-%1$s', $language_link, $city_link)` in a seperate step? – Kris Dec 13 '16 at 14:08
  • not really unfortunately – peterpeterson Dec 13 '16 at 14:34
  • Are you using ZF2' translate module? Or do you need some native code that do this translation for you? – Valour Dec 14 '16 at 12:22
  • What about using `TranslatorAwareTreeRouteStack` with "locale aware" child_routes? Or lazy-loaded routes based on the first segment? There is also: https://github.com/redokun/zf-linguo Would need an individual route segment: `/:lang/` though. – Jens A. Koch Dec 14 '16 at 17:23
  • I am using TranslatorAwareTreeRouteStack with locale aware, that's the main issue, if I use the key with parameters it is not being replaced – peterpeterson Dec 19 '16 at 15:03
  • Please post the route configuration in the question. – Edson Horacio Junior Jan 10 '17 at 20:06
  • 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 07 '17 at 12:55
  • Have you seen https://github.com/zendframework/zendframework/pull/5885 – rexfordkelly Jul 24 '20 at 06:56

1 Answers1

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
asiby
  • 3,229
  • 29
  • 32