i18n.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /**
  2. * 国际化帮助类
  3. * <pre>
  4. * 作者:hugh zhuang
  5. * 邮箱:3378340995@qq.com
  6. * 日期:2018-07-02-下午3:29:34
  7. * 版权:广州流辰信息技术有限公司
  8. * </pre>
  9. */
  10. import lang from '@/i18n' // Internationalization 国际化
  11. const i18n = {
  12. /**
  13. * 设置标题
  14. */
  15. setTitle: (name, title) => {
  16. title = i18n.generateTitle(name, title)
  17. i18n.setBrowserTitle(
  18. (title ? title + '-' : '') +
  19. i18n.t('common.platform') +
  20. '-' +
  21. i18n.t('common.company')
  22. )
  23. },
  24. /**
  25. * 设置标题
  26. * 单页应用在iOS系统下部分APP的webview中 标题不能通过 document.title = xxx 的方式修改 该插件只为解决该问题而生(兼容安卓)
  27. * @param {*} title
  28. * @param {*} img
  29. */
  30. setBrowserTitle: (title, img) => {
  31. if (title === undefined || window.document.title === title) {
  32. return
  33. }
  34. document.title = title
  35. const userAgent = navigator.userAgent.toLowerCase()
  36. if (/iphone|ipad|ipod/.test(userAgent)) {
  37. const iframe = document.createElement('iframe')
  38. iframe.style.display = 'none'
  39. // 替换成站标favicon路径或者任意存在的较小的图片即可
  40. iframe.setAttribute('src', img || '/favicon.ico')
  41. const iframeCallback = function () {
  42. setTimeout(function () {
  43. iframe.removeEventListener('load', iframeCallback)
  44. document.body.removeChild(iframe)
  45. }, 0)
  46. }
  47. iframe.addEventListener('load', iframeCallback)
  48. document.body.appendChild(iframe)
  49. }
  50. },
  51. /**
  52. * 翻译router.meta.title,用于面包屑(breadcrumb)、侧边栏(sidebar)、tagsview
  53. * @param {*} path
  54. * @param {*} title
  55. */
  56. generateTitle: (name, title, ...values) => {
  57. if (!name && !title) return
  58. const hasKey = lang.te('route.' + name)
  59. if (hasKey) {
  60. return lang.t('route.' + name, ...values) // $t :this method from vue-i18n, inject in @/lang/index.js
  61. }
  62. if (title) {
  63. return title
  64. }
  65. name = name.substring(name.lastIndexOf('.') + 1, name.length)
  66. return name
  67. },
  68. /**
  69. * 跟vue-i8n的 t 类似
  70. * 如果不存在则取最后一个 值
  71. * @param {*} path
  72. * @param {*} values
  73. */
  74. t: (path, ...values) => {
  75. const hasKey = lang.te(path)
  76. if (hasKey) {
  77. return lang.t(path, ...values)
  78. }
  79. path = path.substring(path.lastIndexOf('.') + 1, path.length)
  80. return path
  81. },
  82. /**
  83. * 跟vue-i8n的 te 一致
  84. * @param {*} path
  85. */
  86. te: (path) => {
  87. return lang.te(path)
  88. },
  89. getLanguage: () => {
  90. return lang.locale || 'zh-CN'
  91. }
  92. }
  93. export default i18n