简体   繁体   English

将 url 参数翻译为占位符

[英]Translate url parameters as place holder

I wondering how to translate a URL in ZF2 that has a parameter on it.我想知道如何翻译 ZF2 中带有参数的 URL。

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):我正在使用 PhpArray,当我翻译它时,参数不会被替换,因此 URL 呈现为(例如西班牙语):

/:language_link-escuela-:city_link

Instead of the expected behaviour:而不是预期的行为:

/ingles-escuela-miami

Edit:编辑:

The parameters are :language_link and :city_link参数是:language_link:city_link

So the idea is that in one language the rendered URL could be:所以这个想法是在一种语言中呈现的 URL 可以是:

 /: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 . PHP 中有一个名为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如果字符串是这样的: /: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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM