Is internationalization support in the roadmap?
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/localization
Thanks
Hi, I wouldn't know what there is to be done from Giraffe at this point. You can create resource files in F# already and you can access them from your Giraffe web application. You can also register an IStringLocalizer<'T> service via DI in ASP.NET Core and retrieve it from Giraffe http handlers via ctx.GetService<IStringLocalizer<'T>>(). Were you thinking of something else?
Just wanted to understand your vision =) Using IStringLocalizer<'T> is not quite a convenient choice, since we don't have controllers like c# version to place into type parameter. Therefore some helper might be helpful just to write localize "abc"
Also, MVC supports View localization and DataAnnotations, which however I'm not sure if should be ported to giraffe.
@rmotyka solution looks good to me, but it doesn't really do anything specific for Giraffe.
He's basically configured localisation support in ASP.NET Core by registering the following services:
let configureApp (app : IApplicationBuilder) =
let supportedCultures = [| new CultureInfo("pl-PL"); new CultureInfo("en-GB") |]
let localizationOptions = new RequestLocalizationOptions()
localizationOptions.DefaultRequestCulture <- new RequestCulture(supportedCultures.[0])
localizationOptions.SupportedCultures <- supportedCultures
localizationOptions.SupportedUICultures <- supportedCultures
app.UseRequestLocalization(localizationOptions) |> ignore
GlobalStringLocalizerFactory <- app.ApplicationServices.GetService<IStringLocalizerFactory>()
let env = app.ApplicationServices.GetService<IHostingEnvironment>()
(match env.IsDevelopment() with
| true -> app.UseDeveloperExceptionPage()
| false -> app.UseGiraffeErrorHandler errorHandler)
.UseCors(configureCors)
.UseStaticFiles()
.UseGiraffe(webApp)
let configureServices (services : IServiceCollection) =
services.AddCors() |> ignore
services.AddGiraffe() |> ignore
services.AddLocalization(fun options -> options.ResourcesPath <- "Resources") |> ignore
Then added this one generic F# function to help with getting a localized string:
let mutable GlobalStringLocalizerFactory: IStringLocalizerFactory = null
type SharedResource () =
class
end
let getLocalString key =
let assemblyName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name
let localizer = GlobalStringLocalizerFactory.Create("SharedResource", assemblyName)
localizer.Item(key).Value
Given that the majority of this code was just the configuration of the ASP.NET Core middleware I'd say that there's not much Giraffe could simplify this.
I'm going to close this issue and point towards @rmotyka's code for the recommended solution!
Most helpful comment
If someone is interested, I've made some simple i18n solution, which is described here. I made a kind of global helper function as @Lanayx mentioned. The code is here.