i18n-chain icon indicating copy to clipboard operation
i18n-chain copied to clipboard

Taro v2.0.7 无法渲染出字符串,但是 console.log 显示正常

Open xyxc0673 opened this issue 4 years ago • 2 comments

微信开发者工具版本:v1.02.2003121

我用v2.0.7的 Taro-cli init 一个新的项目复现了这个问题 可能和 Pull requests #1 的问题一样?我不是很了解。

image

pages/index/Index.tsx

import Taro, { Component, Config } from "@tarojs/taro";
import { View, Text } from "@tarojs/components";
import { useI18n } from "@i18n-chain/taro";

import "./index.scss";
import i18n from "../../i18n";

const Index = () => {
  useI18n(i18n);
  console.log(i18n.button.submit)
  return <View>before_{i18n.button.submit}_after</View>;
};

export default Index;

i18n/index.ts

import { createI18n } from '@i18n-chain/taro';
import en from './locales/en';

const i18n = createI18n({
  defaultLocale: {
    key: 'en',
    values: en,
  },
});

export default i18n;

i18n/locales/index.ts

const en = {
  button: {
    submit: 'Submit',
    cancel: 'Go back',
  },
  user: {
    profile: 'Tom',
  },
};

export default en;
export type Locale = typeof en;

xyxc0673 avatar Mar 21 '20 11:03 xyxc0673

taro最终会编译为小程序,而链式操作用到了Proxy功能。

对于这个问题,我曾向taro团队提过issue,https://github.com/NervJS/taro/issues/5244 ,不过他们似乎没有心思去处理这块逻辑。

~~所以目前解决方案是,建立临时变量。~~

升级到 @i18n-chain/[email protected],使用最新解决方案(hack)

import { useI18n, hack } from '@i18n-chain/taro';

const Index = () => {
  useI18n(i18n);

  console.log(i18n.button.submit);

  // 仅模板需要hack
  return <View>before_{hack(i18n).button.submit}_after</View>;
};

fwh1990 avatar Mar 27 '20 06:03 fwh1990

taro最终会编译为小程序,而链式操作用到了Proxy功能。

对于这个问题,我曾向taro团队提过issue,NervJS/taro#5244 ,不过他们似乎没有心思去处理这块逻辑。

~所以目前解决方案是,建立临时变量。~

升级到 @i18n-chain/[email protected],使用最新解决方案(hack)

import { useI18n, hack } from '@i18n-chain/taro';

const Index = () => {
  useI18n(i18n);

  console.log(i18n.button.submit);

  // 仅模板需要hack
  return <View>before_{hack(i18n).button.submit}_after</View>;
};

谢谢提供更方便的解决方案 👍

xyxc0673 avatar Apr 14 '20 03:04 xyxc0673