简体   繁体   English

切换 i18n 语言的按钮

[英]button to switch i18n language

my code is in react and uses the i18next library.我的代码处于反应状态并使用 i18next 库。 this was previously done with material ui's Select API, where i was able to switch languages using the value , onChange , and inputProps parameters.这以前是通过 Material ui 的Select API 完成的,我可以在其中使用valueonChangeinputProps参数切换语言。 however, the design has since changed to be two separate buttons (eg. 'en' and 'zh') instead of the switch.但是,此后设计已更改为两个单独的按钮(例如“en”和“zh”)而不是开关。 this is the code that successfully toggles between two languages with the Select API (language toggling = dropdown menu):这是使用Select API 在两种语言之间成功切换的代码(语言切换 = 下拉菜单):

const LanguageMenu = () => {
    const { t, i18n } = useTranslation()
    const classes = useStyles()

    const [values, setValues] = useState({
        language: i18next.language
    })

    function handleChange(event) {
        i18n.changeLanguage(event.target.value)

        setValues(oldValues => ({
            ...oldValues,
            [event.target.name]: event.target.value,
        }))
    }

    return(
        <>
            <Select
    value={values.language}
    onChange={(event) => handleChange(event)}
    disableUnderline
    inputProps={{
        name: 'language'
    }}
    classes={{
        select: classes.selectFocus
    }}
>
        

<MenuItem value={'en'}><p role="img" aria-label="en">🇬🇧</p></MenuItem>
<MenuItem value={'zh-Hant'}><p role="img" aria-label="zh">🇭🇰</p></MenuItem>
</Select>
        </>
    )
}

I've looked at the ButtonGroup API and the Button API and none of them seem to give me the parameters the Select API offers.我查看了ButtonGroup API 和Button API,但它们似乎都没有给我Select API 提供的参数。 I'm looking into using the native button in HTML, but i still need to pass in the values.language context before calling value={'en'} and value={'zh-Hant'} and i'm not sure how i can achieve that.我正在考虑在 HTML 中使用本机button ,但我仍然需要在调用value={'en'}value={'zh-Hant'}之前传入values.language上下文,我不确定如何我可以做到这一点。 i've also tried the code below, which doesn't work:我也试过下面的代码,它不起作用:

<button onChange={(e) => handleChange(e)}>En</button>
<button onChange={(e) => handleChange(e)}>Zh</button>

thanks!谢谢!

You can give your button some attributes and you can access them with the help of those attributes.你可以给你的按钮一些属性,你可以在这些属性的帮助下访问它们。

Something like就像是

<button id="EN" value="EN" name="EN" onChange={(event) => handleChange(event)}>En</button>
<button id="ZH" value="ZH" name="ZH" onChange={(event) => handleChange(event)}>Zh</button>

In you handleChange funtion you can get element through ID.在您的 handleChange 功能中,您可以通过 ID 获取元素。

function handleChange(e:any){
    var x = document.getElementById(e.target.id).value;
    //Use x in your code as per requirement

// x will have the value of your button you canget whatever you want value,name etc
}

I also had example for you created you can check for reference.我还为您创建了示例,您可以检查以供参考。

https://www.w3schools.com/code/tryit.asp?filename=GRZ6X9IAGG39 https://www.w3schools.com/code/tryit.asp?filename=GRZ6X9IAGG39

I hope this will solve your problem.我希望这能解决你的问题。 Let me know if it works.让我知道它是否有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM