仅使用JQuery处理多语言 [英] Handling multilanguage with JQuery only

查看:60
本文介绍了仅使用JQuery处理多语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的网络应用程序中使用JQuery,我想知道在这种情况下处理多语言的最佳方法是什么。

我正在考虑创建一个类似的文件:

I use JQuery in my web app and I am wondering what the best way to handle multilanguage in that context is.
I was thinking of creating a file like:

label["login"]["fr"]="Connection"
label["login"]["en"]="Login"

加载文件后,我会(对于每个标签)a:

Once the file is loaded I will then do (for each label) a:

$('#login').text(label["login"][selected_language]);

在HTML中,我会使用:

In HTML I would then use:

<a href="login.html"><span id="login"></span></a>

这是正确的做法吗?

推荐答案

我认为你使用类和一个发明的属性而不是id来控制多语言会更好,因为如果你在同一个页面中有两次相同的文本,则id会破坏它因为它是独一无二的。

I think it would be better you use class and a invented attributed instead of id to control multi-language, because if you have the same text twice in the same page id would broken it because it is unique.

您可以尝试这种方法:

$(function() {
  $(".is_ml").each(function() {
    $(this).html(label[$(this).attr("ml_label")]["en"])
  })

});

多语言文本的所有内容必须包含class =is_ml和ml_label =label例如,label可以是login。你有我的方法吗?

Everything that is multi-language text has to have class="is_ml" and ml_label="label" where label could be "login", for example. Did you got my approach?

这篇关于仅使用JQuery处理多语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆