Set de localisation language

Hi,

I’m calling the RegisterService Post method from another service. Now I want to get the errors (e.g. Email already exists) in another language. How do I set this language?
CultureInfo.CurrentUICulture = new CultureInfo(“nl-nl”);
or
ValidatorOptions.Global.LanguageManager.Culture = new CultureInfo(“nl”);
didn’t work.

Thanks!

Code example:

        [Route("/aanmelden")]
        public class Aanmelden : Register
        {
            public string Language { get; set; }
        }

        [DefaultView("login")]
        public async Task<object> Post(Aanmelden r)
        {
            try
            {
                using (var service = ResolveService<RegisterService>())
                {
                    // Set the language for the next statement
                    var result = await service.PostAsync(r);
                    return result;
                }
            }
            catch (Exception ex)
            {
                return RegistrationError(r, ex.Message);
            }
        }

ServiceStack only includes its messages in English, but you can override what gets returned by overriding ResolveLocalizedString and ResolveLocalizedStringFormat in your AppHost to intercept the English Error Message (maintained in ErrorMessages class) to return the message you want:

public class AppHost : AppHostBase
{
    Dictionary<string, string> DutchErrorMessages { get; } = new()
    {
        [ErrorMessages.EmailAlreadyExists] = "E-mail bestaat al",
        //...
    };

    public override string ResolveLocalizedString(string text, IRequest request = null)
    {
        return DutchErrorMessages.TryGetValue(text, out var localizedMessage)
            ? localizedMessage
            : text;
    }

    public override string ResolveLocalizedStringFormat(string text, object[] args, IRequest request = null)
    {
        return string.Format(text, args);
    }
}

Thank you. That works!

I saw the FluentValidation translation somewhere so I thought it was part of Authentication.

1 Like