i18n.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // [z-paging]i18n模块
  2. import { initVueI18n } from '@dcloudio/uni-i18n'
  3. import messages from '../../i18n/index.js'
  4. const { t } = initVueI18n(messages)
  5. import u from '.././z-paging-utils'
  6. import c from '.././z-paging-constant'
  7. import interceptor from '../z-paging-interceptor'
  8. export default {
  9. computed: {
  10. finalLanguage() {
  11. try {
  12. const local = uni.getLocale();
  13. const language = this.systemInfo.appLanguage;
  14. return local === 'auto' ? interceptor._handleLanguage2Local(language, this._language2Local(language)) : local;
  15. } catch (e) {
  16. // 如果获取系统本地语言异常,则默认返回中文,uni.getLocale在部分低版本HX或者cli中可能报找不到的问题
  17. return 'zh-Hans';
  18. }
  19. },
  20. // 最终的下拉刷新默认状态的文字
  21. finalRefresherDefaultText() {
  22. return this._getI18nText('zp.refresher.default', this.refresherDefaultText);
  23. },
  24. // 最终的下拉刷新下拉中的文字
  25. finalRefresherPullingText() {
  26. return this._getI18nText('zp.refresher.pulling', this.refresherPullingText);
  27. },
  28. // 最终的下拉刷新中文字
  29. finalRefresherRefreshingText() {
  30. return this._getI18nText('zp.refresher.refreshing', this.refresherRefreshingText);
  31. },
  32. // 最终的下拉刷新完成文字
  33. finalRefresherCompleteText() {
  34. return this._getI18nText('zp.refresher.complete', this.refresherCompleteText);
  35. },
  36. // 最终的下拉刷新上次更新时间文字
  37. finalRefresherUpdateTimeTextMap() {
  38. return {
  39. title: t('zp.refresherUpdateTime.title'),
  40. none: t('zp.refresherUpdateTime.none'),
  41. today: t('zp.refresherUpdateTime.today'),
  42. yesterday: t('zp.refresherUpdateTime.yesterday')
  43. };
  44. },
  45. // 最终的继续下拉进入二楼文字
  46. finalRefresherGoF2Text() {
  47. return this._getI18nText('zp.refresher.f2', this.refresherGoF2Text);
  48. },
  49. // 最终的底部加载更多默认状态文字
  50. finalLoadingMoreDefaultText() {
  51. return this._getI18nText('zp.loadingMore.default', this.loadingMoreDefaultText);
  52. },
  53. // 最终的底部加载更多加载中文字
  54. finalLoadingMoreLoadingText() {
  55. return this._getI18nText('zp.loadingMore.loading', this.loadingMoreLoadingText);
  56. },
  57. // 最终的底部加载更多没有更多数据文字
  58. finalLoadingMoreNoMoreText() {
  59. return this._getI18nText('zp.loadingMore.noMore', this.loadingMoreNoMoreText);
  60. },
  61. // 最终的底部加载更多加载失败文字
  62. finalLoadingMoreFailText() {
  63. return this._getI18nText('zp.loadingMore.fail', this.loadingMoreFailText);
  64. },
  65. // 最终的空数据图title
  66. finalEmptyViewText() {
  67. return this.isLoadFailed ? this.finalEmptyViewErrorText : this._getI18nText('zp.emptyView.title', this.emptyViewText);
  68. },
  69. // 最终的空数据图reload title
  70. finalEmptyViewReloadText() {
  71. return this._getI18nText('zp.emptyView.reload', this.emptyViewReloadText);
  72. },
  73. // 最终的空数据图加载失败文字
  74. finalEmptyViewErrorText() {
  75. return this.customerEmptyViewErrorText || this._getI18nText('zp.emptyView.error', this.emptyViewErrorText);
  76. },
  77. // 最终的系统loading title
  78. finalSystemLoadingText() {
  79. return this._getI18nText('zp.systemLoading.title', this.systemLoadingText);
  80. },
  81. },
  82. methods: {
  83. // 获取当前z-paging的语言
  84. getLanguage() {
  85. return this.finalLanguage;
  86. },
  87. // 获取国际化转换后的文本
  88. _getI18nText(key, value) {
  89. const dataType = Object.prototype.toString.call(value);
  90. if (dataType === '[object Object]') {
  91. const nextValue = value[this.finalLanguage];
  92. if (nextValue) return nextValue;
  93. } else if (dataType === '[object String]') {
  94. return value;
  95. }
  96. return t(key);
  97. },
  98. // 系统language转i18n local
  99. _language2Local(language) {
  100. const formatedLanguage = language.toLowerCase().replace(new RegExp('_', ''), '-');
  101. if (formatedLanguage.indexOf('zh') !== -1) {
  102. if (formatedLanguage === 'zh' || formatedLanguage === 'zh-cn' || formatedLanguage.indexOf('zh-hans') !== -1) {
  103. return 'zh-Hans';
  104. }
  105. return 'zh-Hant';
  106. }
  107. if (formatedLanguage.indexOf('en') !== -1) return 'en';
  108. return language;
  109. }
  110. }
  111. }