index.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import app from '../../app.json';
  2. import I18n from 'react-native-i18n';
  3. import en from './locales/en';
  4. import es from './locales/es';
  5. import de from './locales/de';
  6. import fr from './locales/fr';
  7. import ja from './locales/ja';
  8. import km from './locales/km';
  9. import ko from './locales/ko';
  10. import my from './locales/my';
  11. import th from './locales/th';
  12. import vi from './locales/vi';
  13. import zh from './locales/zh';
  14. import tw from './locales/zh-TW';
  15. import { getStorage } from '../utils/storage';
  16. I18n.fallbacks = true;
  17. //I18n.l("currency", 630, {})
  18. //I18n.t("", {})
  19. const SETTING_KEY = "locale-settings";
  20. const DEBUG = app.debug && !app.product;
  21. const LOCALES = [{
  22. name: en.name,
  23. value: "en",//英语
  24. enable: true
  25. }, {
  26. name: de.name,
  27. value: "de",//德语
  28. enable: DEBUG
  29. }, {
  30. name: es.name,
  31. value: "es",//西语
  32. enable: DEBUG
  33. }, {
  34. name: fr.name,
  35. value: "fr",//法语
  36. enable: DEBUG
  37. }, {
  38. name: ja.name,
  39. value: "ja",//日语
  40. enable: DEBUG
  41. }, {
  42. name: ko.name,
  43. value: "ko",//韩语
  44. enable: DEBUG
  45. }, {
  46. name: km.name,
  47. value: "km",//柬埔寨-高棉语
  48. enable: DEBUG
  49. }, {
  50. name: my.name,
  51. value: "my",//缅甸语
  52. enable: DEBUG
  53. }, {
  54. name: th.name,
  55. value: "th",//泰语
  56. enable: DEBUG
  57. }, {
  58. name: vi.name,
  59. value: "vi",//越南语
  60. enable: DEBUG
  61. }, {
  62. name: zh.name,
  63. value: "zh-CN",//简体中文
  64. enable: true
  65. }, {
  66. name: tw.name,
  67. value: "zh-HK",//繁體中文
  68. enable: true
  69. }]
  70. if (app.modules.i18n && !app.isLumiWhitelabel) {
  71. I18n.translations = {
  72. default: en,
  73. en, es, de, fr,
  74. ja, km, ko, my,
  75. th, vi, zh,
  76. "zh-CN": zh,
  77. "zh-HK": tw
  78. }
  79. global.defaultLocale = I18n.currentLocale();
  80. if (app.debug)
  81. console.log("[I18n] defaultLocal", global.defaultLocale);
  82. } else {
  83. I18n.translations = {
  84. default: en,
  85. en
  86. }
  87. global.defaultLocale = "en";
  88. }
  89. const getLocaleName = () => {
  90. var locale = ""
  91. for (let l of LOCALES) {
  92. if (l.value == defaultLocale) {
  93. locale = l.value;
  94. break;
  95. }
  96. }
  97. if (!locale) {
  98. if (defaultLocale.indexOf("zh") >= 0) {
  99. if (defaultLocale.indexOf("CN") >= 0) {
  100. locale = "zh-CN"
  101. } else {
  102. locale = "zh-HK"
  103. }
  104. } else {
  105. for (let l of LOCALES) {
  106. if (defaultLocale.indexOf(l.value) >= 0) {
  107. locale = l.value;
  108. break;
  109. }
  110. }
  111. }
  112. if (app.debug)
  113. console.log("[I18n] getLocaleName(2)", locale);
  114. } else {
  115. if (app.debug)
  116. console.log("[I18n] getLocaleName(1)", locale);
  117. }
  118. return locale;
  119. }
  120. global.$t = (scope) => {
  121. return I18n.t(scope);
  122. }
  123. const init = (back) => {
  124. if (app.modules.i18n && !app.isLumiWhitelabel) {
  125. getStorage(SETTING_KEY).then(res => {
  126. if (res) {
  127. global.currentLocale = I18n.locale = res;
  128. } else {
  129. global.currentLocale = getLocaleName();//"zh-CN"
  130. I18n.locale = global.currentLocale;//设置local
  131. }
  132. if (app.debug) console.log("[I18n] currentLocale", global.currentLocale, I18n.locale);
  133. back();
  134. })
  135. } else {
  136. global.currentLocale = "en";
  137. back();
  138. }
  139. }
  140. export const i18nUtil = {
  141. init: init,
  142. locales: LOCALES,
  143. SETTING_KEY: SETTING_KEY,
  144. getLocaleName: getLocaleName,
  145. isChinese: () => global.currentLocale.indexOf("zh") >= 0,
  146. getLocale: () => global.currentLocale,
  147. setlocale: (localeName) => {
  148. global.currentLocale = I18n.locale = localeName;
  149. global.ComCountryList = undefined;
  150. },
  151. getDefaultLocale: () => global.defaultLocale,
  152. analyzeLocaleData: (data, def) => {
  153. for (let l in data) {
  154. //console.log(l);
  155. if (currentLocale.indexOf(l) >= 0) {
  156. return data[l]
  157. }
  158. }
  159. return def ?? data;
  160. }
  161. }