1 Star 0 Fork 0

同步项目 / WeihanLi.Extensions.Localization.Json

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

WeihanLi.Extensions.Localization.Json WeihanLi.Extensions.Localization.Json

Intro

dotnet 基于 json 的本地化组件,支持基于 culture 的资源路径

Build

AzureDevOps Build Status

Github Build Status

GetStarted

注册服务:

services.AddJsonLocalization(options =>
    {
        options.ResourcesPath = Configuration.GetAppSetting("ResourcesPath");
        options.ResourcesPathType = ResourcesPathType.TypeBased; // 默认方式和微软找资源的方式类似
        // options.ResourcesPathType = ResourcesPathType.CultureBased; // 在对应的 culture 子目录下寻找资源文件,可以参考后面的示例
    });

中间件配置(如果是asp.net core,和之前一样):

app.UseRequestLocalization();

That's it~

添加你的资源文件

TypeBased 资源文件的路径

For Types:

Home/Index => Controllers/HomeController

资源路径:

  • [ResourcesPath]/Controllers/HomeController.[cultureName].json

示例:

  • Resources/Controllers/HomeController.en.json
  • Resources/Controllers/HomeController.zh.json

For Razor 视图:

示例:

  • Resources/Views/Home/Index.en.json
  • Resources/Views/Home/Index.zh.json

CultureBased 资源文件路径

For Types:

Home/Index => Controllers/HomeController

资源路径:

  • [ResourcesPath]/[cultureName]/Controllers/HomeController.json

示例:

  • Resources/en/Controllers/HomeController.json
  • Resources/zh/Controllers/HomeController.json

For Razor 视图:

示例:

  • Resources/en/Views/Home/Index.json
  • Resources/zh/Views/Home/Index.json

Copy your resource files to output:

需要设置将资源文件拷贝到输出目录,否则会找不到资源文件,可以在启动项目项目文件中加入以下示例代码:

<ItemGroup>
<Content Update="Resources\**\*.json">
    <CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>

上面的配置会将 Resources 目录下的所有 json 文件拷贝到输出目录下,可以根据自己的需要进行修改

Use

用法和之前是一样的

Controller 示例:

public class ValuesController : Controller
{
    private readonly IStringLocalizer<ValuesController> _localizer;

    public ValuesController(IStringLocalizer<ValuesController> localizer)
    {
        _localizer = localizer;
    }

    // GET: api/<controller>
    [HttpGet]
    public string Get()
    {
        return _localizer["Culture"];
    }
}

Razor 视图示例:

@using Microsoft.AspNetCore.Mvc.Localization
@using Microsoft.Extensions.Localization
@using WeihanLi.Extensions.Localization.Json.Sample.Controllers
@inject IHtmlLocalizer<HomeController> HtmlLocalizer
@inject IStringLocalizer<HomeController> StringLocalizer
@inject IViewLocalizer ViewLocalizer
@{
    ViewData["Title"] = "Index";
}

<h2>Index</h2>

<div>string: @StringLocalizer["Hello"]</div>

<div>html: @HtmlLocalizer["Hello"]</div>

<div>view: @ViewLocalizer["Hello"]</div>

资源文件示例:

{
  "Culture": "中文"
}

Samples

Contact

Contact me via weihanli@outlook.com if you need

MIT License Copyright (c) 2020 liweihan Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

暂无描述 展开 收起
C# 等 3 种语言
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/flyinghat/WeihanLi.Extensions.Localization.Json.git
git@gitee.com:flyinghat/WeihanLi.Extensions.Localization.Json.git
flyinghat
WeihanLi.Extensions.Localization.Json
WeihanLi.Extensions.Localization.Json
master

搜索帮助