简体   繁体   English

在 HTML 中更改语言而不重定向的正确方法是什么?

[英]What is the right way to change language without redirection in HTML?

I am new to web development and I have given the task to change the language of a website without changing the pages.我是 web 开发的新手,我的任务是在不更改页面的情况下更改网站的语言。 (For example, somesite.com/en/home for English, somesite.com/jp/home for Japanese, etc.,). (例如,somesite.com/en/home 用于英语,somesite.com/jp/home 用于日语等)。 I think I have come up with a way using React, but I am not so sure whether this approach to changing languages is natural, efficient or not.我想我想出了一种使用 React 的方法,但我不确定这种改变语言的方法是否自然、有效。 Below is my code in React.js,下面是我在 React.js 中的代码,

import React from 'react'
import metadata from './metadata'

class Display extends React.Component
{
    constructor()
    {
        super()
        this.state = {l: metadata[0]}
        this.handleLanguageChange = this.handleLanguageChange.bind(this)
    }

    handleLanguageChange()
    {
        let lang = event.target.value;
        if(lang == 'ta')
            this.setState({l: metadata[0]})
        else if(lang == 'en')
            this.setState({l: metadata[1]})
        else if(lang == 'jp')
            this.setState({l: metadata[2]})
        else if(lang == 'zh')
            this.setState({l: metadata[3]})
    }

    render()
    {
        return(
            <div>
               <select onChange={this.handleLanguageChange}>
                    <option value="ta">Tamil</option>
                    <option value="en">English</option>
                    <option value="jp">Japanese</option>
                    <option value="zh">Chinese</option>
               </select>
               <h1>{this.state.l.language}</h1>
               <h2>{this.state.l.title}</h2>
               <p>{this.state.l.content}</p>
            </div>
        )
    }
}

export default Display

and, here is my metadata.而且,这是我的元数据。

const data = [
    {
        language: 'தமிழ்',
        title: 'அறிமுகம் ',
        content: 'வேலை செய்கிறதா என பார்க்க'
    },
    {
        language: 'English',
        title: 'Introduction',
        content: 'To check whether this is working or not'
    },
    {
        language: '日本語',
        title: '紹介',
        content: '動くかどうか分かるのため'
    },
    {
        language: '中文',
        title: '介绍',
        content: '知道这是否有效'
    }
]

export default data

and, index.js as well.而且,index.js 也是如此。

import React from 'react';
import ReactDOM from 'react-dom'
import Display from './Display'

ReactDOM.render(<Display />, document.getElementById('root'));

When I tested it, it worked like a dream.当我测试它时,它就像做梦一样。 But is this approach right?但这种方法对吗? Could this pose any security threats?这会构成任何安全威胁吗?

Please guide me in the right direction.请指导我正确的方向。

Your approach is good, probably is the same approach everyone is using.你的方法很好,可能是每个人都在使用的方法。

Couple of notes, refreshing a page with new text everywhere might not be easily accomplished if you use a prop or state variable without "global" state/storage strategy.需要注意的是,如果您在没有“全局”状态/存储策略的情况下使用 prop 或 state 变量,则可能无法轻松地在各处刷新带有新文本的页面。

Try to think of using "Context" to turn this language into a more "global" variable to be sitting locally with any of your components.尝试考虑使用“上下文”将这种语言转换为更“全局”的变量,以便与您的任何组件一起在本地放置。 This way you don't have to worry about passing this variable around, also you can have a separate scope to change this variable.这样你就不必担心传递这个变量,你也可以有一个单独的 scope 来改变这个变量。

This isn't obvious at the beginning, but when you expand your code base, you'll realize that your current solution won't scale.这在开始时并不明显,但是当您扩展代码库时,您会意识到您当前的解决方案无法扩展。

Keep json file each for all languages and then you can do like this:

    import english from './locales/english.json'

    handleLanguageChange()
        {
            let lang = event.target.value;
            if(lang == 'english')
                this.setState({stateOfLocale: 'english'});
                I18n.locale = stateOfLocale;
            ...
        }       

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

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