使用 Javascript 更改动态内容的语言
我的项目应该支持不同语言的 GUI(西班牙语、英语、德语等)。我正在使用 CodeIgniter,因此使用语言类的 PHP 视图没有任何问题。我可以在 PHP 上加载一些先前定义的变量来创建不同语言的视图。
问题来了:
一些功能(实际上很多)使用Javascript:某些项目的个性化上下文菜单,动态创建的不同DIV等。大多数功能都是动态创建的,所以我不知道选择的语言(我可以创建很多重复的代码,每种语言一个,但这太多余了)。
我需要找到一种方法将此内容翻译成用户之前选择的新语言。
例如:
用户右键单击,上下文菜单有以下选项(使用 Javascript 动态创建):
- Hi
- Goodbye
当用户将页面语言更改为“西班牙语”时,上下文菜单应显示:
- Hola
- Adios
有没有办法保存一些具有不同语言的所有名称的变量,然后加载以创建菜单?
抱歉发了这么大的帖子。希望大家能够帮助我或者给我一些小建议。
My project should support different languages for the GUI (spanish, english, german, etc). I'm using CodeIgniter, so I haven't got any problem with PHP views using Language Class. I can load some previously defined vars on PHP to create views in different languages.
The problem comes here:
Some features (a lot of them actually) use Javascript: personalized context menu for some items, differents DIVs created dynamically, etc. The most of this features are created dynamically, so I can't know the language selected (I can create a lot of duplicated code, one for each language, but this is too redundant).
I need to find a way to translate this content into the new language previously selected by user.
For example:
The user right-click and the context menu have the following options (created dynamically using Javascript):
- Hi
- Goodbye
When the user change the page language to 'Spanish', the context menu should show:
- Hola
- Adios
Is there any way to save some variables with all the names in different languages, and then load to create the menus?
Sorry for the big post. I hope everyone can help me or bring me a little tip.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您希望 i18n(“国际化”的缩写)在服务器端语言和客户端 Javascript 中工作时,可以使用多种系统。
这是我为 PHP 和 JavaScript 找到的示例: http://doc.silverstripe.org/ sapphire/en/topics/i18n
我已经用 PHP、Ruby 和 Python 完成了这个工作。一般来说,有一种方法可以做到这一点。您创建一个文本库。这个库有指向该文本特定部分的指针。在您的代码中,您使用该指针来引用该文本片段。上面的示例为您提供了一种在 PHP 中创建库的方法,该库可以编译为可以在 JavaScript 中调用的 JavaScript 等效项。基本上,它将文案和其他文本与代码完全分开。
我希望这对您有所帮助。
there are several systems to use when you want i18n (short for "internationalization") to work in a server side language as well as in client side Javascript.
this is an example I found for PHP and JavaScript: http://doc.silverstripe.org/sapphire/en/topics/i18n
I've done this in PHP, Ruby and Python. In general there is one way this is done. You create a library of text. this library has pointers to specific pieces of that text. In your code you use that pointer to refer to that piece of text. What the example above does provide you a way to create a library in PHP that can be compiled to a JavaScript equivalent that you can call in JavaScript. Basically it completely separates copywriting and other text from the code.
I hope this helps you on your way.
我想您是在问是否可以保存这些用户首选项?
如果是这样,将其存储为用户计算机上的 cookie
如果您不是这个意思,并且您想存储所有语言变体,那么您可以将其存储在可由 javascript 加载的数组中
I assume you're asking if you can save these user preferences?
If so store it as a cookie on the user's computer
If you don't mean this, and you want to store all of the variations of languages, then you could store it in an array which can be loaded by javascript