mvc 输出字符串

tech2023-12-03  34

mvc 输出字符串

Today, I am going to show you how to efficiently localize your Javascript strings within your PHP Framework. You can download a working implementation of this tutorial here.

今天,我将向您展示如何在PHP Framework中有效地本地化Javascript字符串。 您可以在此处下载本教程的有效实施。

There are actually several methods to localize Javascript strings in a PHP environment. Technically speaking it is possible to duplicate your Javascript file, naming it after the target language, and loading the needed version each time the user selects a new language on site. But this surely represents a method that one could hardly call good practice, even though it would ‘work’.

实际上,有几种方法可以在PHP环境中本地化Javascript字符串。 从技术上讲,可以复制Javascript文件,将其命名为目标语言,并在用户每次在网站上选择新语言时加载所需的版本。 但是,这肯定代表了一种方法,即使它可以“奏效”,也几乎不能称之为良好实践。

The main disadvantage of using a method like this is that every time you will need to modify your JavaScript code then you will have to perform the modification for each language. This is only prone to error, if not bringing you extra unwanted work.

使用这种方法的主要缺点是,每次您需要修改JavaScript代码时,都必须对每种语言进行修改。 如果不给您带来额外的不必要工作,这只会出错。

There is also the possibility to have your literal strings directly called by means of PHP variables embedded within your JavaScript code, but depending on your framework architecture, this is not always an option that’s available.

也有可能通过嵌入JavaScript代码中PHP变量直接调用文字字符串,但是根据您的框架体系结构,这并不总是可用的选项。

So I’m going to show you a method that will work for sure and which will be easy to maintain as well.

因此,我将向您展示一种肯定会有效并且易于维护的方法。

Remember, you can download a working example right here.

请记住,您可以在此处下载一个有效的示例。

So let’s start…

让我们开始吧...

In the example attached to this tutorial, I have set up a button that triggers a Javascript function called trigger_msg():

在本教程所附的示例中,我设置了一个按钮,该按钮触发了一个称为trigger_msg()JavaScript函数:

echo '<input type="button" value="'.$t_launch_message.'" class="trigger" onclick="return trigger_msg();" />';

The trigger_msg() function is found in /public/JS/main.js :

在/public/JS/main.js中可以找到trigger_msg()函数:

function trigger_msg(){ return alert(translate(LOCALIZATION.here_is_how_it_works)); }

– We call the translate() function found in /languages/translate.js and pass, as a parameter, the name of the element we need that’s contained in the array named LOCALIZATION. – We’re using the syntax translate(name_of_language_array.name_of_element, extra_parameter1, extra_paramater2, etc…) thus using commas only to separate extra parameters. – Parameters may of course be literals if surrounded by quotes

–我们调用在/languages/translate.js中找到的translate()函数,并将包含在名为LOCALIZATION的数组中的所需元素的名称作为参数传递。 –我们正在使用语法translation(name_of_language_array.name_of_element,extra_parameter1,extra_paramater2等…),因此仅使用逗号分隔其他参数。 –如果用引号引起来,参数当然可以是文字

Before we take a deeper look at the translate() function, here’s what the LOCALIZATION array found in /languages/current_language/JS/current_language.js looks like:

在深入研究translate()函数之前,这是在/languages/current_language/JS/current_language.js中找到的LOCALIZATION数组的样子:

var LOCALIZATION = { here_is_how_it_works : 'Voici comment cela fonctionne.\nAppuyez sur le bouton suivant afin de voir comment ça se passe avec des paramètres.', who_does_not_know_are_and_that_the_sky_is : 'Qui ne sait pas que %s x %s font %s,\net que le ciel est %s?', blue : 'bleu' };

Within our array element definitions you can see ‘%s’ is being used, that is the expression we use to hold our extra parameters. We will come to that a little later though. Note that you may insert HTML style tags, eg. <b>, etc., within your array element definitions if you use a custom dialog box and it will work nicely.

在我们的数组元素定义中,您可以看到正在使用'%s',即我们用来保存额外参数的表达式。 我们稍后再讨论。 请注意,您可以插入HTML样式标签,例如。 如果您使用自定义对话框,请在数组元素定义中输入<b>等,它将很好地工作。

Now time to concentrate on our translate() function:

现在该专注于我们的translate()函数:

(function () { if (!window.translate){ window.translate = function(){ var html = [ ]; var arguments = arguments; var string = arguments[0]; var objIndex = 0; var reg = /%s/; var parts = [ ]; for ( var m = reg.exec(string); m; m = reg.exec(string) ) { // m[0][0] gives undefined in IE parts.push(string.substr(0, m[0][0] === "%" ? m.index : m.index)); parts.push("%s"); string = string.substr( m.index+m[0].length ); } parts.push(string); for (var i = 0; i &lt; parts.length; ++i){ var part = parts[i]; if (part &amp;&amp; part == "%s"){ var object = arguments[++objIndex]; if (object == undefined) { html.push("%s"); }else{ html.push(object); }; }else{ html.push(part); } } return html.join(''); } }; })();

This function below constitutes the core of our JavaScript localization scheme.

下面的函数构成了我们JavaScript本地化方案的核心。

Basically in the variable called string we store the arguments captured from the trigger_msg() function, parse them in our first for loop, filter them using a regular expression that’s held in the variable named reg, and push the resulting parts in an array called parts[]. Then we reassemble those parts into an array called html[] that our function returns.

基本上,在名为string的变量中,我们存储从trigger_msg()函数捕获的参数,在第一个for循环中解析它们,使用包含在名为reg的变量中的正则表达式对其进行过滤,然后将生成的零件推入称为parts[]的数组parts[] 。 然后,我们将这些部分重新组装到函数返回的名为html[]的数组中。

The variable named reg holds a simple regular expression ‘/%s/’, the %s being the syntax we have chosen to use to define our parameters as discussed above.

名为reg的变量包含一个简单的正则表达式'/%s /',%s是我们选择用来定义参数的语法,如上所述。

The trigger_msg_with_params() function in /public/JS/main.js shows just how to use parameters when localizing your strings. Indeed, there are times in a system when a literal string that needs to be translated may contain values which will depend on user input, and this function comes in handy by allowing to not having to re-use our code so much:

/public/JS/main.js中的trigger_msg_with_params()函数显示了在本地化字符串时如何使用参数。 确实,在系统中有时候需要翻译的文字字符串可能包含取决于用户输入的值,并且该函数由于不必重复使用我们的代码而变得很方便:

function trigger_msg_with_params(){ var param1 = 5382; var param2 = 9408; var param3 = param1 * param2; var param4 = translate(LOCALIZATION.blue); return alert(translate(LOCALIZATION.who_does_not_know_are_and_that_the_sky_is, param1, param2, param3, param4)); }

You can see each defined parameter, eg. var param1, could well have been a parameter passed to the function call too. Defined parameters can also be actual calls to the translate() function. All of which, again, turns out to be very helpful.

您可以看到每个定义的参数,例如。 var param1 ,也很可能也是传递给函数调用的参数。 定义的参数也可以是对translate()函数的实际调用。 再次证明,所有这些都非常有帮助。

That’s all there is to it. This system represents an efficient and reliable way to translate your JavaScript strings across your PHP Framework and allows for a great deal of suppleness.

这里的所有都是它的。 该系统代表了在PHP框架中翻译JavaScript字符串的有效且可靠的方法,并允许大量的柔顺性。

You are more than welcome to leave your comments or questions. Stay tuned for more tutorials.

非常欢迎您留下您的评论或问题。 请继续关注更多教程。

翻译自: https://www.sitepoint.com/localizing-javascript-strings-php-mvc-framework/

mvc 输出字符串

最新回复(0)