动态生成的JavaScript在PHP或做AJAX的多语言界面? [英] Dynamically generate javascript in php or do ajax for multilanguage interface?
问题描述
我在几个不同的外语来实现web应用程序界面。我发现了jQuery功能我写取决于什么语言正在实施中。例如:
I have to implement a webapp interface in several different foreign languages. I find that the jquery functions I am writing depend on what language is being implemented. For example:
<?php
require 'nl.php';
/*
if(isset($_SESSION['lang'])){
$lang = $_SESSION['lang'];
if(!empty($lang)){
if($lang == 'en'){
require 'en.php';
}else if($lang = 'nl'){
require 'nl.php';
}
}
}
*/
?>
<html>
<head>
</head>
<body>
<textarea id = "notes"><?php echo $string_lib['ENTER_NOTES'];?></textarea>
<script type="text/javascript" src= "js/jquery.js"></script>
<script type="text/javascript" src= "js/clicknotes.js"></script>
</body>
</html>
因此,这要求一个语言文件,提供了一个数组。注释掉部分只是为了证明我如何确定什么语言来发送。首先需要在它的地方指挥工作。现在,这里的语言文件。
So this calls for a language file that provides an array. the commented out section is just to demonstrate how I am determining what's language to send. the first require command works in its place. Now here are the language files.
nl.php:
<?php
$string_lib = array(
'ENTER_NOTES' => 'Vul hier uw notities.',
'ENTER_PASSWORD' => 'Vul hier uw wachtwoord in.',
);
和en.php
<?php
$string_lib = array(
'ENTER_NOTES' => 'Enter your notes here.',
'ENTER_PASSWORD' => 'Enter your password here.',
);
?>
所以,现在的问题时,我开始做一些事情在JavaScript中,这样,clicknotes.js出现:
So now, problems arise when I start doing certain things in javascript, like this, clicknotes.js:
$(window).load(function(){
$(document).ready(function(){
$('#notes').focusin(function(){
if($(this).text() == 'Enter your notes here.'){
$(this).text('');
}
//alert($(this).text());
});
});
});
所以,我要么使用我使用的HTML同样的方法,这样可以动态生成与PHP的JavaScript:
So I either have to dynamically generate the javascript with php using the same method I used for the html, like this:
<?php
header('Content-type: text/javascript');
?>
$(window).load(function(){
$(document).ready(function(){
$('#notes').focusin(function(){
if($(this).text() == '<?php echo $string_lib['ENTER_NOTES'];?>'){
$(this).text('');
}
//alert($(this).text());
});
});
});
或者我得把JavaScript的等效阵列和调用javascript的那些变量,即如果($(本))。文字()== string_lib ['NL'] ['ENTER_NOTES'])。或者,我想我可以做一个Ajax请求获得我需要的数组。
or I have to send the equivalent arrays in javascript and call those variables in javascript,i.e. if($(this)).text() == string_lib['nl']['ENTER_NOTES']). OR I was thinking I could do an ajax request to get the arrays i need.
那么,它的这些方法,你会说是最好的可维护性和易于改装的?
So, which of these methods would you say is best for maintainability and ease of modification?
推荐答案
我已经在我的CMS的翻译,很多时候我需要在另外的静态纯文本作为一个使用特定字符串工作JavaScript文件。 我做的:
I have worked with translations in my CMS and many times i require specific strings to be used in an otherwise static plain text as a javascript file. What i do:
en.php
return array(
"id1"=>"translation 1",
"id2"=>"translation 2"
);
HTML
...
$trans = require("en.php");
...
<input type="hidden" id="trans" data-trans='<?php echo json_encode($trans); ?>' />
...
JS
$(document).ready(function() {
var id1 = $("#trans").data("trans")["id1"]; // contains the 'id1' string
});
这样你的JavaScript不需要改变,这导致历史上多次缓存相关的问题为你的客户,因为他们需要刷新。
This way your javascript would not need changing, which results many times in history caching related problems for your clients, as they need refreshing.
这篇关于动态生成的JavaScript在PHP或做AJAX的多语言界面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!