当前位置:   article > 正文

前端jq实现中英文切换(i18n国际化方式,apicloud打包后也适用)_jq项目怎么实现语言切换

jq项目怎么实现语言切换

1.项目文件及目录

2.准备js

github下载地址:https://github.com/T-baby/jquery.i18n/releases

3.页面代码

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>i18n-json数据切换中英文</title>
  6. </head>
  7. <body>
  8. <span i18n="login.username">账号:</span><input type="text" name="" i18n="login.enter_user" placeholder="请输入账号">
  9. <br />
  10. <span i18n="login.password">密码:</span><input type="password" name="" i18n="login.enter_pass" placeholder="请输入密码">
  11. <br />
  12. <select id="changeLanguage">
  13. <option i18n="login.choice_langu">请选择语言</option>
  14. <option i18n="login.ch">中文</option>
  15. <option i18n="login.en">英语</option>
  16. </select>
  17. <script type="text/javascript" src="js/jquery.js"></script>
  18. <script type="text/javascript" src="js/jquery.i18n.min.js"></script>
  19. <script type="text/javascript">
  20. $(function(){
  21. defaultLanguage();
  22. })
  23. var langStr="cn";
  24. function defaultLanguage(){
  25. // alert(langStr);
  26. $("[i18n]").i18n({
  27. defaultLang: langStr,
  28. filePath: "./i18n/",
  29. filePrefix: "i18n_",
  30. fileSuffix: "",
  31. forever: true,
  32. callback: function(res) {}
  33. });
  34. }
  35. $('#changeLanguage').change(function(){
  36. var language=$('#changeLanguage option:selected').val();
  37. if(language=="英语" || language=="English"){
  38. langStr="en";
  39. }else{
  40. langStr="cn";
  41. }
  42. defaultLanguage();
  43. });
  44. </script>
  45. </body>
  46. </html>

4.json数据

中文:i18n_cn.json

  1. {
  2. "login.username":"账号:",
  3. "login.password":"密码:",
  4. "login.enter_user":"请输入账号",
  5. "login.enter_pass":"请输入密码",
  6. "login.choice_langu":"请选择语言",
  7. "login.ch":"中文",
  8. "login.en":"英语"
  9. }

英文数据:i18n_en.json

  1. {
  2. "login.username":"username:",
  3. "login.password":"password:",
  4. "login.enter_user":"Please enter your username",
  5. "login.enter_pass":"Please enter your password",
  6. "login.choice_langu":"Please select a language",
  7. "login.ch":"Chinese",
  8. "login.en":"English"
  9. }

5.注意事项

a.如果只是静态文件会报如下错误,告诉我们访问数据时需要加载服务器资源,纯静态页面建议使用Hbuilder web服务器方式运行

~~~~直接访问file:///D:/Users/Administrator/Desktop/i18nDemo/index.html文件报错

jquery.js:4 Access to XMLHttpRequest at 'file:///D:/Users/Administrator/Desktop/i18nDemo/i18n/i18n_cn.json' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.

~~~~Hbuilder本地服务器方式正常:http://127.0.0.1:8020/i18nDemo/index.html?__hbt=1563420319456 

b.i18n参数详解

defaultLang

defaultLang: defaultLang,

默认语言名称,插件会自动将 filePrefix+defaultLang+fileSuffix 拼接在一起作为语言文件文件名。

filePath

filePath: "/i18n/",

该参数指定了语言文件所在文件夹在项目中的位置。

filePrefix

filePrefix: "i18n_",

该参数指定了语言文件的命名的前缀。

fileSuffix

fileSuffix: "",

该参数指定了语言文件的命名的后缀。

callback

  1. callback:function(){
  2. //do something
  3. }

6.参考文章:https://juejin.im/entry/58774b70b123db005dd49e62

7.其他方案(四种方式实现前端国际化):https://blog.csdn.net/qq_41485414/article/details/81093999

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/332132?site
推荐阅读
相关标签