Multi-language support is enabled by default. See AddLocalizer in WebExtensions.cs for the service configuration.
Note
Built on ASP.NET Core globalization/localization. See: ASP.NET Core Globalization and Localization.
In the Definition/Share project you’ll find resource files (often nested under Localizer.cs in Visual Studio). Add or modify keys as needed.
Perigon.AspNetCore.SourceGeneration automatically generates constants in the Localizer partial class from these resources.
Inject Localizer wherever needed. It wraps IStringLocalizer<Localizer>:
public partial class Localizer(IStringLocalizer<Localizer> localizer) { public string Get(string key, params object[] arguments) { try { return localizer[key, arguments]; } catch (Exception) { return key; } } }
Controllers inherit RestControllerBase and inject Localizer. Common MVC responses like BadRequest and NotFound are overridden to support localization:
return BadRequest(Localizer.UserNotFound);
In Manager classes, throw BusinessException to return localized content:
throw new BusinessException(Localizer.UserNotFound);
GlobalExceptionMiddleware handles these exceptions and returns localized messages to clients.
内容大纲