Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

国际化支持(Internationalization support) #89

Closed
wzxinchen opened this issue Apr 9, 2020 · 0 comments
Closed

国际化支持(Internationalization support) #89

wzxinchen opened this issue Apr 9, 2020 · 0 comments
Assignees
Labels
feature request New feature or request wait publish 已修复,等待发布

Comments

@wzxinchen
Copy link
Contributor

wzxinchen commented Apr 9, 2020

中文介绍
WebAssembly 渲染版,进行如下配置:
在 Program 类中增加如下代码即可使用,默认为简体中文
builder.Services.AddSingleton<BLang>();//此处必须单例

Server 渲染版,进行如下设置:
在 Startup 类中增加如下代码即可使用,默认为简体中文
services.AddSingleton<BLang>();//此处必须单例

使用方法
新建组件,继承 BComponentBase 类,可像如下切换语言

public class BasicLangBase : BComponentBase
    {
        public async Task SetEnLang(MouseEventArgs eventArgs)
        {
            Lang.LangLocale = "en-US";
        }

        public async Task SetCnLang(MouseEventArgs eventArgs)
        {
            Lang.LangLocale = "zh-CN";
        }
    }

可像如下显示当前语言对应的文本

@inherits BasicLangBase
<div>@Lang.T("Message")</div>
<h1>@Lang.T("Snowman:Data:Title")</h1>
<div>@Lang.T("Snowman:Body")</div>
<br/>
<BButton Type="@ButtonType.Primary" OnClick="SetEnLang">@Lang.T("SetEnLang")</BButton>
<BButton Type="@ButtonType.Primary" OnClick="SetCnLang">@Lang.T("SetCnLang")</BButton>

其中,Lang是BComponentBase的一个属性,通过它可输出对应语言的文本
国际化文件格式如下:

{
  "Message": "你好,世界",
  "Snowman": {
    "Data" : {
      "Title": "《雪人》"
    },
    "Body": "    好棒哦!下雪了!是时候堆个雪人了。詹姆斯跑了出去。他弄了一大堆雪。他把一个大雪球放到了最上面来充当头部。他给雪人加了一个围巾和一个帽子,又给雪人添了一个桔子当鼻子。他就加了煤炭来充当眼睛和纽扣。傍晚,詹姆斯打开了门。他看见了什么?雪人在移动!詹姆斯邀请它进来。雪人从来没有去过房间里面。它对猫咪打了个招呼。猫咪玩着纸巾。不久之后,雪人牵着詹姆斯的手出去了。他们一直向上升,一直升到空中!他们在飞翔!多么美妙的夜晚!第二天早上,詹姆斯从床上蹦了起来。他向门口跑去。他想感谢雪人,但是它已经消失了。"
  },
  "SetEnLang": "切换英文",
  "SetCnLang": "切换中文",
   "key":"text"
}

目前内置 English 与 中文 两种语言,可以自行扩充语言,例如下面是繁体中文的文件格式,文件名为zh-TW.json:

{
      "reg":"註冊",
      "login":"登錄"
}

要让这个文件生效,只需要获取到BLang实例,然后将 LangLocale 属性设置为 zh-TW,即可切换语言。

如果您不想继承 BComponentBase 这个类,又想切换语言,只需要依赖注入获取到BLang实例,然后将 LangLocale 属性设置为 zh-TW,即可切换语言。

English introduction:

The webassembly rendering version is configured as follows:

Add the following code to the program class, which is simplified Chinese by default

Builder. Services. Addsingleton <Blang> (); // a single instance is required here

Server rendering version, set as follows:

Add the following code to the startup class, which is simplified Chinese by default

Services. Addsingleton <Blang> (); // a single instance is required here

How to use

Create a new component, inherit bccomponentbase class, and switch languages as follows

public class BasicLangBase : BComponentBase
{
public async Task SetEnLang(MouseEventArgs eventArgs)
{
Lang.LangLocale = "en-US";
}



public async Task SetCnLang(MouseEventArgs eventArgs)
{
Lang.LangLocale = "zh-CN";
}
}

The text corresponding to the current language can be displayed as follows

@inherits BasicLangBase
<div>@Lang.T("Message")</div>
<h1>@Lang.T("Snowman:Data:Title")</h1>
<div>@Lang.T("Snowman:Body")</div>

<BButton Type="@ButtonType.Primary" OnClick="SetEnLang">@Lang.T("SetEnLang")</BButton>
<BButton Type="@ButtonType.Primary" OnClick="SetCnLang">@Lang.T("SetCnLang")</BButton>

Where Lang is an attribute of BComponentbase, through which the text of the corresponding language can be output

The international file format is as follows:

{
"Message": "Hello, world",
"Snowman": {
"Data": {
"Title": "snowman"
}

"Body": "great! It's snowing! It's time to make a snowman. James ran out. He made a lot of snow. He put a big snowball on the top to act as the head. He added a scarf and a hat to the snowman and an orange as his nose. He added coal as eyes and buttons. In the evening, James opened the door. What did he see? Snowman moving! James invited it in. The snowman has never been in the room. He said hello to the cat. The cat is playing with paper towels. Soon after, the snowman took James by the hand and went out. They're going up, up in the air! They are flying! What a wonderful night! The next morning, James jumped out of bed. He ran to the door. He wants to thank the snowman, but it's gone. "
}
"Setenlang": "switching English",
"Setcnlang": "switching Chinese",
"Key": "text"
}

At present, there are two built-in languages, English and Chinese, which can be expanded by themselves. For example, the following is the file format of traditional Chinese, with the file name of zh-tw.json:

{
      "reg":"註冊",
      "login":"登錄"
}

For this file to take effect, just get the Blang instance and set the langlocale property to zh-TW to switch the language.

If you don't want to inherit BComponentbase and want to switch languages, you only need to obtain the Blang instance by dependency injection, and then set the langlocale property to zh-TW to switch languages.

@wzxinchen wzxinchen added the feature request New feature or request label Apr 9, 2020
@wzxinchen wzxinchen added the wait publish 已修复,等待发布 label Apr 9, 2020
@wzxinchen wzxinchen added this to the WASM开始支持 milestone Apr 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature or request wait publish 已修复,等待发布
Projects
None yet
Development

No branches or pull requests

2 participants