Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support asp.netcore IStringLocalizer<> #31

Closed
daveh101 opened this issue Jan 22, 2020 · 6 comments
Closed

Support asp.netcore IStringLocalizer<> #31

daveh101 opened this issue Jan 22, 2020 · 6 comments
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@daveh101
Copy link

Installed product versions

  • Visual Studio: 2019 Professional (16.5 preview 1)
  • This extension: 1.5

Description

Doesn't support asp.netcore core IStringLocalizer<T> resource localization system

Steps to recreate

Added localization based on the microsft docs for asp,netcore: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/localization?view=aspnetcore-3.1

  1. Create new Asp.netcore project (I'm using 3.1, the process has been the same since at least 2.0)
  2. Configure the startup.cs to support localization
public void ConfigureServices(IServiceCollection services)
{
  services.AddLocalization();
  services.Configure<RequestLocalizationOptions>(options =>
  {
    options.DefaultRequestCulture = new Microsoft.AspNetCore.Localization.RequestCulture(culture: "en-GB", uiCulture: "en-GB");
  });
  //other configuration code
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
  var locOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
  app.UseRequestLocalization(locOptions.Value);
}
  1. Create Resource Strings
    strings
    The Strings.cs file is just an empty class
public class Strings
{
}

Resource files are then added to the same Resources Folder and due to naming are made a sub-item of this main strings class. The resource file as the Access Modifer set to No Code Generation.

Add resources to the resx files.

  1. Use resources wihtin the Controllers or views (I'll just show controllers)
public class MyController : Controller
{
  private readonly IStringLocalizer<Strings> _localizer;
  public MyController(IStringLocalizer<Strings> localizer)
  {
    _localizer = localizer; //added through DI
  }

  [HttpGet("")]
  public IActionResult Get()
  {
    var stringToDisplay = _localizer["key in resource file"];
    return new JsonResult(stringToDisplay);
  }
}

Current behavior

The String Resource Visualizer only seems to pick up on resx files that are contained in the Properties folder of the project. Due to this is doesn't see the available resources as something useable.

I suspect that it might not recognise the IStringLocalizer...but have no idea here.

Expected behavior

Work :)

@mrlacey
Copy link
Owner

mrlacey commented Jan 22, 2020

Good call.

Yes, current implementation relies on conventions that don't pick this up.

@mrlacey mrlacey added this to the 1.6 milestone Jan 22, 2020
@mrlacey
Copy link
Owner

mrlacey commented Jan 22, 2020

X-Ref #5 and look to add this in a way that allows identifying uses of resource strings beyond the current identifier method.

@mrlacey
Copy link
Owner

mrlacey commented Mar 26, 2020

Note. The way resources are defined in the OP (using consts to define the lookup keys) doesn't match the usage shown in the official docs and samples.

This will need two solutions

  1. For the key being directly specified in the code (as per official example)
  2. For the key being a lookup const specified elsewhere.

@mrlacey mrlacey self-assigned this Mar 26, 2020
@mrlacey mrlacey added the enhancement New feature or request label Mar 26, 2020
@mrlacey
Copy link
Owner

mrlacey commented Mar 27, 2020

image

@mrlacey
Copy link
Owner

mrlacey commented Mar 27, 2020

Proof-of-concept
image

This is working with the ASP.NET Core official sample for localization.

It doesn't (yet) work with constant values specified for the keys, but I've got a working lookup for resolving all the different possible files to use for where the localized text may be.

@mrlacey
Copy link
Owner

mrlacey commented Jun 1, 2020

This functionality has been added to version 1.6 and is now available in the marketplace.

If this extension is useful to you please leave a review (this both encourages me and helps others find this extension.)
You can also help me continue to develop this and other tools by becoming a sponsor.

@mrlacey mrlacey closed this as completed Jun 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants