简体   繁体   中英

Asp.Net core 2.0 - Translate Server-Side messages

Intro

I'm using asp.net core 2.0 & angular 6 for my website. 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.

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. 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.
  2. 30: the user not exist.
  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 .

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.

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.

When client call backend API it includes Accept-Language header that contains the language of the client. 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. You store all you strings in .resx and localize the .resx into the languages you clients use. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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