CodeIgniter:解析位于JavaScript中的动态语言字幕 [英] CodeIgniter: Parse dynamic language captions located in javascript
问题描述
我有一个需要本地化的JavaScript代码。
I have a javascript code that needs localisation, ie.
function js_proc() {
var some_data = 'this text needs to be translated dynamically at runtime';
}
所以我这样重写:
function js_proc() {
var some_data = <?php echo $this->lang->line('some_data_id'); ?>;
}
在视图中,我写了这样的js链接:
In the view, I wrote the js link like this:
<script type="text/javascript" src="www.domain.com/codeigniter/get_js/file-1/"></script>
调用控制器中的函数get_js想法是让get_js()函数读取js文件并翻译语言字符串...
which calls the function get_js() in the controller. The idea is to have the get_js() function read the js file(s) and translate the language strings...
有太多的js字符串要翻译,所以我不能通过每个字符串
作为一个变量。理想情况下,我想通过codeigniter语言文件。
There are way too many js strings to be translated, so I can't pass every string
as a variable. Ideally I'd like to make this work through the codeigniter language files.
我的问题是:有一种方法来解析php部分的js文件&执行(=翻译)它们?即
class App extends CI_Controller
{
function get_js {
$content = file_get_contents($js_file);
echo parse_php($content);
}
...
}
p>
推荐答案
根据语言文件的大小,允许javascript访问整个语言数组的一种快速方法是加载数组into a global javascript array;
Depending on how big the language file is, a quick way to allow javascript to access your entire language array could be to load the array into a global javascript array;
<script>
var globalLang = <?php echo json_encode($this->lang->language); ?>;
</script>
然后使用您的javascript访问;
Then access in your javascript like this;
globalLang['some_lang_key']
这篇关于CodeIgniter:解析位于JavaScript中的动态语言字幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!