简体   繁体   English

Asp.Net Core 2.0-翻译服务器端消息

[英]Asp.Net core 2.0 - Translate Server-Side messages

Intro 介绍

I'm using asp.net core 2.0 & angular 6 for my website. 我的网站使用的是asp.net core 2.0angular 6 I want to support multi-language - English as default and Spanish as another language for now. 我想支持多国语言-目前默认为英语,而西班牙语为另一种语言。 I'm using @ngx-translate/core as translation service in the client-side which is working awesome and easy. 我在客户端使用@ ngx-translate / core作为翻译服务,它的工作非常简单。

My Problem 我的问题

I have many different messages and errors that come from the server and they always appear in English as I never translated them. 我有许多来自服务器的不同消息和错误,由于我从未翻译过,所以它们总是以英文显示。 I want to be able to translate the messages from the server as well. 我也希望能够翻译来自服务器的消息。

I need help to decide what is the best way to translate thus messages or maybe help me find pros and cons for each method. 我需要帮助来决定什么是翻译消息的最佳方法,或者也许可以帮助我找到每种方法的利弊。

On the one hand, I can use aspnet/Localization and create .resx files to translate the messages in the server. 一方面,我可以使用aspnet / Localization并创建.resx文件来转换服务器中的消息。 In this method, I will end up with a bunch of translation files in the server and some in the client which will be hard to maintain. 在这种方法中,我最终将在服务器中生成一堆翻译文件,而在客户端中得到一些翻译文件,这将很难维护。

On the other hand, I can send error codes ( not status codes ) like so - 另一方面,我可以这样发送错误代码( 不是状态代码 ):

  1. 15: the user is denied. 15:拒绝用户
  2. 30: the user not exist. 30:用户不存在
  3. Some other error code. 其他错误代码。

This will allow me to take these codes and translate it on the client which will eventually help me put all the translations in one place and translate them using @ngx-translate/core . 这将使我能够接受这些代码并在客户端上进行翻译,最终将帮助我将所有翻译内容放在一个位置,并使用@ ngx-translate / core进行翻译

I found at least one "problem" with the method above, if in the future someone will need to consume my API , he/she won't be able to understand this error codes unless I will expose an API reference with examples which can be time-consuming. 我发现上述方法中至少有一个“问题”,如果将来有人需要使用我的API ,除非我将提供带有以下示例的API参考,否则他/她将无法理解此错误代码。耗时的。

Do you have better suggestions? 您有更好的建议吗? Or maybe Best Practice for this case? 还是这种情况的最佳实践?

This is what I have done. 这就是我所做的。 I also have Angular client and ASP.NET core backend. 我也有Angular客户端和ASP.NET核心后端。

When client call backend API it includes Accept-Language header that contains the language of the client. 当客户端调用后端API时,它包括包含客户端语言的Accept-Language标头。 If backend needs to send anything that is locale depend it uses the localized strings. 如果后端需要发送任何区域设置,则使用本地化的字符串。

When HHTP call lands the server you first read the language id and then you set the thread locale to that language. 当HHTP呼叫登陆服务器时,您首先读取语言ID,然后将线程语言环境设置为该语言。 You store all you strings in .resx and localize the .resx into the languages you clients use. 您将所有字符串存储在.resx中,并将.resx本地化为客户端使用的语言。 If the client uses language that you do not support resource manager falls back to you original language (eg English). 如果客户端使用您不支持的语言,则资源管理器将退回到您的原始语言(例如英语)。

Remember to include the error code inside the error message. 切记在错误消息中包括错误代码。 It is very likely that the localized error message needs to be handled with a person not understanding the language. 很可能需要由不懂该语言的人员来处理本地化的错误消息。 By having the error go makes she that they understand the error message. 通过出错,她使他们知道错误消息。

暂无
暂无

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

相关问题 ASP.NET Core中的服务器端图形 - Server-side graphics in ASP.NET Core 结合 Asp.Net Core 服务器端和 React 客户端 - Combining Asp.Net Core server-side and React client-side 用于客户端和服务器端验证的 ASP.NET Core 重用代码 - ASP.NET Core Reuse Code for Client-Side and Server-Side Validation 如何在服务器端捕获 ASP.NET Core 2 SignalR 异常并在客户端使用 JavaScript 处理它们? - How to catch ASP.NET Core 2 SignalR exceptions on server-side and handle them on client side with JavaScript? 如何在 ASP.NET Core 3.1 MVC 中进行RequiredIf 客户端和服务器端验证? - How to make RequiredIf Client-side and server-side validation in ASP.NET Core 3.1 MVC? 无法生成一致的令牌ASP.NET C#服务器端Oauth2.0 - Cannot generate a consistant token ASP.NET C# server-side Oauth2.0 IExceptionFilter 处理程序在 ASP.NET Core 3.1 Blazor(服务器端)中不起作用 - IExceptionFilter handler not working in ASP.NET Core 3.1 Blazor (server-side) Blazor 服务器端或 ASP.NET 核心 Razor SaaS 页面 - Blazor Server-side or ASP.NET Core Razor Pages for SaaS 如何在带有 Razor 页面的 ASP.NET Core 2.1 中使用或处理不是为并发而设计的服务器端 SDK - How to Use or Deal With Server-Side SDKs not Designed for Concurrency in ASP.NET Core 2.1 with Razor Pages Blazor ASP.Net Core(服务器端)中的自定义 URL 重写规则在使用导航链接时不会触发 - Custom URL rewrite rule in Blazor ASP.Net Core (server-side) not triggering when using navlink
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM