0
点赞
收藏
分享

微信扫一扫

netcore多语言使用命令行编译中文包出不来的问题解决

毅会 2022-08-22 阅读 52

多语言功能比较常见。.netcore里,我们一般都会使用资源包,也就是在Resources目录下创建语言包。

netcore多语言使用命令行编译中文包出不来的问题解决_docker

 

 

第二步:startup里写代码:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace WebApplication4
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddLocalization(options => options.ResourcesPath = "Resources");
services.AddControllers();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "WebApplication4", Version = "v1" });
});

}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{

var supportedCultures = new[] { "zh-CN","en-US" };
var localizationOptions = new RequestLocalizationOptions().SetDefaultCulture(supportedCultures[0])
.AddSupportedCultures(supportedCultures)
.AddSupportedUICultures(supportedCultures);

app.UseRequestLocalization(localizationOptions);

if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "WebApplication4 v1"));
}

app.UseRouting();

app.UseAuthorization();

app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}

在VS2022里 F5运行没任何问题。但是若使用在Docker里编译+运行,就出现问题了,中文一直无法出来。

我们在Docker里 使用 dotnet build来编译的,完整的Dockerfile如下:

netcore多语言使用命令行编译中文包出不来的问题解决_ico_02

netcore多语言使用命令行编译中文包出不来的问题解决_ico_03

#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
WORKDIR /app
EXPOSE 80

FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /src
COPY ["WebApplication4/WebApplication4.csproj", "WebApplication4/"]
RUN dotnet restore "WebApplication4/WebApplication4.csproj"
COPY . .
WORKDIR "/src/WebApplication4"
RUN dotnet build "WebApplication4.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "WebApplication4.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "WebApplication4.dll"]

View Code

各种疑点排查,发现dotnet build竟然无法生成 zh-CN  。这个就是问题所在。

那么如何解决呢?根据官方文档,发现标准里竟然没有 zh-CN   。

中文只有  zh-Hans和 zh-Hant,以及父类  zh     。

知道这个就可以了,我们将资源包 Lang.zh-CN改名为   Lang.zh就可以了。当然也可改为  Lang.zh-Hans  。

 

 

官方文档如下:

​​https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/localization?view=aspnetcore-5.0​​

​​https://docs.microsoft.com/zh-cn/dotnet/api/system.globalization.cultureinfo?view=net-6.0​​

 

作者:沐雪
文章均系作者原创或翻译,如有错误不妥之处,欢迎各位批评指正。本文版权归作者所有,如需转载恳请注明。
​​​ 为之网-热爱软件编程 http://www.weizhi.cc/​​



举报

相关推荐

0 条评论