behavior.ts 722 B

123456789101112131415161718192021222324
  1. import request from './install';
  2. import { PREFIX, formatRecord, formatData } from './tool';
  3. export default Behavior({
  4. lifetimes: {
  5. created() { this._i18n_init(false); },
  6. },
  7. pageLifetimes: {
  8. created() { this._i18n_init(true); },
  9. },
  10. methods: {
  11. async _i18n_init(page?: boolean) {
  12. const [gather, reset] = formatRecord(this.data[PREFIX]);
  13. const app = getApp({ allowDefault: true });
  14. if (app.i18n == null) {
  15. if (reset) this.setData(reset);
  16. app.i18n = await request();
  17. }
  18. if (app.i18n != null) {
  19. const i18n = formatData(gather, (key) => app.i18n?.[key]);
  20. if (i18n) this.setData(i18n);
  21. } else this.setData(formatData(gather));
  22. },
  23. }
  24. });