Translate url parameters as place holder
Asked Answered
M

1

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) ;
Merca answered 3/12, 2016 at 17:15 Comment(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.Selfsupporting
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)Merca
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.Selfsupporting
There is the usability benefit, the end user will be able to remember urls if is properly translated, and for SEO too.Merca
Not familiar with ZF2 but would it be possible to sprintf('%0$s-school-%1$s', $language_link, $city_link) in a seperate step?Shivaree
not really unfortunatelyMerca
Are you using ZF2' translate module? Or do you need some native code that do this translation for you?Strick
What about using TranslatorAwareTreeRouteStack with "locale aware" child_routes? Or lazy-loaded routes based on the first segment? There is also: github.com/redokun/zf-linguo Would need an individual route segment: /:lang/ though.Poison
I am using TranslatorAwareTreeRouteStack with locale aware, that's the main issue, if I use the key with parameters it is not being replacedMerca
Please post the route configuration in the question.Wiebmer
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.Aspa
Have you seen github.com/zendframework/zendframework/pull/5885Lacustrine
S
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
Setiform answered 5/8, 2020 at 5:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.