i18n.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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((title ? title + '-' : '') + i18n.t('common.platform') + '-' + i18n.t('common.company'))
  18. },
  19. /**
  20. * 设置标题
  21. * 单页应用在iOS系统下部分APP的webview中 标题不能通过 document.title = xxx 的方式修改 该插件只为解决该问题而生(兼容安卓)
  22. * @param {*} title
  23. * @param {*} img
  24. */
  25. setBrowserTitle: (title, img) => {
  26. if (title === undefined || window.document.title === title) {
  27. return
  28. }
  29. document.title = title
  30. const userAgent = navigator.userAgent.toLowerCase()
  31. if (/iphone|ipad|ipod/.test(userAgent)) {
  32. const iframe = document.createElement('iframe')
  33. iframe.style.display = 'none'
  34. // 替换成站标favicon路径或者任意存在的较小的图片即可
  35. iframe.setAttribute('src', img || '/favicon.ico')
  36. const iframeCallback = function() {
  37. setTimeout(function() {
  38. iframe.removeEventListener('load', iframeCallback)
  39. document.body.removeChild(iframe)
  40. }, 0)
  41. }
  42. iframe.addEventListener('load', iframeCallback)
  43. document.body.appendChild(iframe)
  44. }
  45. },
  46. /**
  47. * 翻译router.meta.title,用于面包屑(breadcrumb)、侧边栏(sidebar)、tagsview
  48. * @param {*} path
  49. * @param {*} title
  50. */
  51. generateTitle: (name, title, ...values) => {
  52. if (!name && !title) return
  53. const hasKey = lang.te('route.' + name)
  54. if (hasKey) {
  55. return lang.t('route.' + name, ...values) // $t :this method from vue-i18n, inject in @/lang/index.js
  56. }
  57. if (title) { return title }
  58. name = name.substring(name.lastIndexOf('.') + 1, name.length)
  59. return name
  60. },
  61. /**
  62. * 跟vue-i8n的 t 类似
  63. * 如果不存在则取最后一个 值
  64. * @param {*} path
  65. * @param {*} values
  66. */
  67. t: (path, ...values) => {
  68. const hasKey = lang.te(path)
  69. if (hasKey) {
  70. return lang.t(path, ...values)
  71. }
  72. path = path.substring(path.lastIndexOf('.') + 1, path.length)
  73. return path
  74. },
  75. /**
  76. * 跟vue-i8n的 te 一致
  77. * @param {*} path
  78. */
  79. te: (path) => {
  80. return lang.te(path)
  81. },
  82. getLanguage: () => {
  83. return lang.locale || 'zh-CN'
  84. }
  85. }
  86. export default i18n