index.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. (function () {
  2. //** */辨别移动终端类型
  3. var Terminal = {
  4. platform: function () {
  5. var u = navigator.userAgent,
  6. app = navigator.appVersion;
  7. return {
  8. //IE内核
  9. windows: u.indexOf('Windows') > -1,
  10. //opera内核
  11. presto: u.indexOf('Presto') > -1,
  12. //苹果、谷歌内核
  13. webKit: u.indexOf('AppleWebKit') > -1,
  14. //火狐内核
  15. gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1,
  16. //是否为移动终端
  17. mobile: !!u.match(/AppleWebKit.*Mobile.*/) || !!u.match(/AppleWebKit/),
  18. //ios终端
  19. ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/),
  20. //android终端或者uc浏览器
  21. android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1,
  22. //是否iPad
  23. iPad: u.indexOf('iPad') > -1,
  24. //是否为iPhone或者QQHD浏览器
  25. iPhone: u.indexOf('iPhone') > -1,
  26. //是否为mac系统
  27. Mac: u.indexOf('Macintosh') > -1,
  28. //是否web应该程序,没有头部与底部
  29. webApp: u.indexOf('Safari') == -1
  30. };
  31. }(),
  32. // 辨别移动终端的语言:zh-cn、en-us、ko-kr、ja-jp...
  33. language: (navigator.browserLanguage || navigator.language).toLowerCase(),
  34. init: function () {
  35. var sysName;
  36. var lagName;
  37. if (Terminal.platform.android) {
  38. sysName = "android";
  39. } else if (Terminal.platform.iPhone) {
  40. sysName = "iPhone";
  41. } else if (Terminal.platform.iPad) {
  42. sysName = "iPad";
  43. } else if (Terminal.platform.windows) {
  44. sysName = "windows";
  45. } else if (Terminal.platform.Mac) {
  46. sysName = "Mac";
  47. } else if (Terminal.platform.gecko) {
  48. sysName = "fireFox";
  49. } else if (Terminal.platform.mobile) {
  50. sysName = "mobile";
  51. } else {
  52. sysName = "unknown";
  53. }
  54. switch (Terminal.language) {
  55. case 'en':
  56. lagName = "en";
  57. break;
  58. default:
  59. lagName = "zh_CN";
  60. }
  61. return {
  62. sysName:sysName,
  63. lagName:lagName
  64. }
  65. }
  66. }
  67. if(!localStorage.getItem("lag")){
  68. localStorage.setItem("lag", Terminal.init().lagName);
  69. }
  70. var plgLanguage = {
  71. debug: false,
  72. ns: ['translation','message'],
  73. lng:localStorage.getItem("lag"),
  74. fallbackLng:"zh_CN",
  75. defaultNS:'translation',
  76. backend: {
  77. loadPath: '/ops/javascripts/language/{{lng}}/{{ns}}.json',
  78. crossDomain: false
  79. },
  80. missingInterpolationHandler:function(text, value){
  81. // console.log(text)
  82. // console.log(value)
  83. },
  84. parseMissingKeyHandler:function(key){
  85. // console.log(key)
  86. if(key.indexOf(".") ==-1){
  87. return key;
  88. }
  89. var nk = key.substr(key.lastIndexOf(".")+1);
  90. if(i18next.exists(nk)){
  91. return i18next.t(nk);
  92. }
  93. return nk;
  94. }
  95. }
  96. jqueryI18next.init(i18next, $);
  97. i18next
  98. .use(i18nextXHRBackend)
  99. .use(i18nextBrowserLanguageDetector)
  100. .init(plgLanguage,function () {
  101. console.log(i18next);
  102. //updateContent()
  103. });
  104. function updateContent() {
  105. $(function () {
  106. //版本号version
  107. $('[name="plg-version"]').append("-v");
  108. $(document).localize();
  109. })
  110. }
  111. i18next.on('languageChanged', function () {
  112. //Object.getOwnPropertyNames(vipspa).length > 0 && vipspa.reset()
  113. updateContent();
  114. //console.log(i18next.getFixedT(i18next.language)("提示"))
  115. });
  116. })()