Comments (31)
@shahabganji let me try your scenario to make sure that I can reproduce the bug, if you think your fix is a real bug, you could send a PR if you want ..
Thanks
from my.extensions.localization.json.
@hishamco The PR created, hope it helps
from my.extensions.localization.json.
I see, I don't now you are running on Mac OS, so I need to create a functional test, so AzurePipeline should fail if there's issue
I will try create it ASAP
Thanks again
from my.extensions.localization.json.
Also I will try your change to check if it's work with me too on Windows
from my.extensions.localization.json.
Dear @hishamco, check PR #44 . I've added functional tests for one of the samples, LocalizationSample
, do we need another one for LocalizationSample.Mvc
?
from my.extensions.localization.json.
I need to add some new features, then I will release 2.1.0
hope this will not take more than few days
from my.extensions.localization.json.
Thanks for reporting this, but did you try to run the sample, it should work as expected
from my.extensions.localization.json.
Dear @hishamco, thanks for the prompt reply, yes I ran the sample with a controller trying to use IStringLocalizer
[Route("")]
public class TestController : ControllerBase
{
private readonly IStringLocalizer _localizer;
public TestController(IStringLocalizer localizer)
{
_localizer = localizer;
}
[HttpGet]
public string Get() => _localizer["Hello"];
}
---- EDIT ------
The startup
method Configure
:
public void Configure(IApplicationBuilder app, IHostEnvironment env, IStringLocalizer localizer1, IStringLocalizer<Startup> localizer2)
{
var supportedCultures = new List<CultureInfo>
{
new CultureInfo("en-US"),
new CultureInfo("fr-FR")
};
var options = new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture("en-US"),
SupportedCultures = supportedCultures,
SupportedUICultures = supportedCultures
};
app.UseRequestLocalization(options);
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.Run(async (context) =>
{
await context.Response.WriteAsync($"{localizer1["Hello"]}!!");
await context.Response.WriteAsync($"{localizer2["Hello"]}!!");
});
app.UseRouting();
app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
}
from my.extensions.localization.json.
I need to check the sample with your changes first before I review the PR
Thanks
from my.extensions.localization.json.
@shahabganji I think there's some confusion here you 're using IStringLocalizer
instead of IStringlocalizer<T>
which is may causes a strange behavior
Please refer to my new added sample here
Hope it clarifies what's the preferred localizer you should use as well as resource name conventions
from my.extensions.localization.json.
Merged into dev
from my.extensions.localization.json.
@hishamco
I still get the same error, even with IStringLocalizer<HomeController>
; besides using IStringLocalizer<T>
only loads messages from T.{culture}.json
file, right? however, using non-generic IStringLocalizer
will load messages which are not only for a specific class/controller, like some global shared messages.
I added the following to resolve problem in Startup -> ConfigureServices
services.AddJsonLocalization(
options => options.ResourcesPath = Path.Combine(
Directory.GetCurrentDirectory(), "Resources"));
without that it is like :
note that the response is 500 Internal Server Error
and this is the log :
Microsoft.AspNetCore.Server.Kestrel: Error: Connection id "0HLUNLKCTNMP6", Request id "0HLUNLKCTNMP6:00000001": An unhandled exception was thrown by the application.
System.ArgumentException: The path must be absolute. (Parameter 'root')
at Microsoft.Extensions.FileProviders.PhysicalFileProvider..ctor(String root, ExclusionFilters filters)
at Microsoft.Extensions.FileProviders.PhysicalFileProvider..ctor(String root)
at Microsoft.Extensions.Configuration.FileConfigurationExtensions.SetBasePath(IConfigurationBuilder builder, String basePath)
at My.Extensions.Localization.Json.JsonStringLocalizer.<>c__DisplayClass16_0.<BuildResourcesCache>b__0(String _) in /Users/shahab/dev/playground/My.Extensions.Localization.Json/src/My.Extensions.Localization.Json/JsonStringLocalizer.cs:line 173
at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
at My.Extensions.Localization.Json.JsonStringLocalizer.BuildResourcesCache(String culture) in /Users/shahab/dev/playground/My.Extensions.Localization.Json/src/My.Extensions.Localization.Json/JsonStringLocalizer.cs:line 162
at My.Extensions.Localization.Json.JsonStringLocalizer.GetStringSafely(String name) in /Users/shahab/dev/playground/My.Extensions.Localization.Json/src/My.Extensions.Localization.Json/JsonStringLocalizer.cs:line 101
at My.Extensions.Localization.Json.JsonStringLocalizer.get_Item(String name) in /Users/shahab/dev/playground/My.Extensions.Localization.Json/src/My.Extensions.Localization.Json/JsonStringLocalizer.cs:line 42
at Microsoft.Extensions.Localization.StringLocalizer`1.get_Item(String name)
at LocalizationSample.Mvc.Controllers.HomeController.Index() in /Users/shahab/dev/playground/My.Extensions.Localization.Json/samples/LocalizationSample.Mvc/Controllers/HomeController.cs:line 19
at lambda_method(Closure , Object , Object[] )
at Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters)
at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.SyncObjectResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeActionMethodAsync()
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeNextActionFilterAsync()
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|19_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
at Microsoft.AspNetCore.Localization.RequestLocalizationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication`1 application)
from my.extensions.localization.json.
That means the error occurs when you are using IStringLocalizer
alone or beside IStringLocalizer<T>
right?
from my.extensions.localization.json.
I just checked out the project and run the new sample as dotnet project. I have the same error no matter which type of usage, IStringLocalizer<T>
alone also produces the error. using both together also produces error.
my current controller :
using System;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Localization;
namespace LocalizationSample.Mvc.Controllers
{
[Route("")]
public class HomeController : Controller
{
private readonly IStringLocalizer<HomeController> _localizer;
public HomeController(IStringLocalizer<HomeController> localizer)
{
_localizer = localizer ?? throw new ArgumentNullException(nameof(localizer));
}
public string Index()
{
var result = _localizer["Hello"].Value;
return result;
}
}
}
and Startup.cs
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace LocalizationSample.Mvc
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddJsonLocalization(options => options.ResourcesPath = "Resources");
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseRequestLocalization("en-US", "fr-FR");
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseEndpoints(endpoints => endpoints.MapControllers());
}
}
}
I have two resource files, HomeController.fr-FR.json
and fr-FR.json
changed the line to services.AddJsonLocalization( options => options.ResourcesPath = Path.Combine(Directory.GetCurrentDirectory(), "Resources"));
and it works like a charm.
from my.extensions.localization.json.
Also changing Line 174 to
.SetBasePath(Path.Combine(Directory.GetCurrentDirectory() ,_resourcesPath))
solved the issue.
from my.extensions.localization.json.
I did few updates after I support JsonHtmlLocalizer
, I will try to use IStringLocalizer
alongside with IStringLocalizer
to reproduce the error
from my.extensions.localization.json.
You meant IStringLocalizer
alongside IStringLocalizer<T>
? but in this case IStringLocalizer<T>
alone also throws an exception. Anyway thanks for the follow ups.
from my.extensions.localization.json.
Seems I confused, could please try the latest sample LocalizationSample.Mvc
and let me know what are the required changes to raise the error
from my.extensions.localization.json.
I exactly did that. No Changes required. please check here and here
I just checked out the project and run the new sample as dotnet project. I have the same error no matter which type of usage, IStringLocalizer <T> alone also produces the error. using both together also produces error.
from my.extensions.localization.json.
Seems you didn't fetch the latest bits ;) I just merged a new commits anyhow I don't have such T.{culture}.json
file, furthermore there's no error occurs on IStringLocalizer<HomeController>
at all
from my.extensions.localization.json.
This is a French version of how Privacy page is look like at my end
from my.extensions.localization.json.
By T
I meant HomeController
, I pulled the latest version, however, the problem still exist. I did no changes just the ran the sample
- URL:
https://localhost:8001/Home/Privacy/?culture-fr-FR&ui-culture=fr-FR
- error the same as before:
Microsoft.AspNetCore.Server.Kestrel: Error: Connection id "0HLUNMASR1PM7", Request id "0HLUNMASR1PM7:00000001": An unhandled exception was thrown by the application.
System.ArgumentException: The path must be absolute. (Parameter 'root')
at Microsoft.Extensions.FileProviders.PhysicalFileProvider..ctor(String root, ExclusionFilters filters)
at Microsoft.Extensions.FileProviders.PhysicalFileProvider..ctor(String root)
at Microsoft.Extensions.Configuration.FileConfigurationExtensions.SetBasePath(IConfigurationBuilder builder, String basePath)
at My.Extensions.Localization.Json.JsonStringLocalizer.<>c__DisplayClass16_0.<BuildResourcesCache>b__0(String _) in /Users/shahab/dev/playground/My.Extensions.Localization.Json/src/My.Extensions.Localization.Json/JsonStringLocalizer.cs:line 173
at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
at My.Extensions.Localization.Json.JsonStringLocalizer.BuildResourcesCache(String culture) in /Users/shahab/dev/playground/My.Extensions.Localization.Json/src/My.Extensions.Localization.Json/JsonStringLocalizer.cs:line 162
at My.Extensions.Localization.Json.JsonStringLocalizer.GetStringSafely(String name) in /Users/shahab/dev/playground/My.Extensions.Localization.Json/src/My.Extensions.Localization.Json/JsonStringLocalizer.cs:line 101
at My.Extensions.Localization.Json.JsonStringLocalizer.get_Item(String name) in /Users/shahab/dev/playground/My.Extensions.Localization.Json/src/My.Extensions.Localization.Json/JsonStringLocalizer.cs:line 42
at Microsoft.Extensions.Localization.StringLocalizer`1.get_Item(String name)
at LocalizationSample.Mvc.Controllers.HomeController.Privacy() in /Users/shahab/dev/playground/My.Extensions.Localization.Json/samples/LocalizationSample.Mvc/Controllers/HomeController.cs:line 22
at lambda_method(Closure , Object , Object[] )
at Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters)
at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.SyncActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeActionMethodAsync()
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeNextActionFilterAsync()
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
at Microsoft.AspNetCore.Localization.RequestLocalizationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication`1 application)
I did the same thing to solve the problem and it worked.
I don't know whether the OS matters or not, mine is Mac OS X
from my.extensions.localization.json.
If you wish to create a functional test that runs the LocalizationSample.Mvc
that I created in you PR it will be very good to make sure that your changes works on ever OS
from my.extensions.localization.json.
No problem I'll put a time on that.
from my.extensions.localization.json.
I request few changes on your PR, please fix them and let me know if you need help on creating a functional test
Thanks Saeed
from my.extensions.localization.json.
Sure, just give me some time till evening, currently at the middle of some important stuff, is that okay?
from my.extensions.localization.json.
Take your time .. I will try to fix and improve something else meanwhile
from my.extensions.localization.json.
I will have a look ...
Thanks
from my.extensions.localization.json.
@hishamco Please also update the nuget feed. thanks
from my.extensions.localization.json.
Do you mean upload a NuGet package?
from my.extensions.localization.json.
from my.extensions.localization.json.
Related Issues (20)
- Add Benchmark HOT 16
- Missing Localization Behavior
- Remove internal contructor in JsonStringLocalizer HOT 1
- Ability to override methods in JsonStringLocalizer and JsonStringLocalizerFactory HOT 9
- Localization FallBack Considerations
- Support ResourceLocationAttribute HOT 1
- Support RootNamespaceAttribute
- assign culture in runtime HOT 3
- Have one resource file for project HOT 13
- Any plan to switch the project to a .NET standard class library ? HOT 10
- How to use with DataAnnotation? HOT 17
- Regression Bug HOT 1
- LocalizationSample project is not working HOT 3
- About NET5.0 Blazor WebAssembly Uses My.Extensions.Localization.Json HOT 14
- System.ArgumentOutOfRangeException: startIndex cannot be larger than length of string. HOT 11
- Class based localisation only works when the class is in the root namespace HOT 5
- Can you provide an core api example with DataAnnotations? HOT 2
- when using xunit test(SUT),there is an exception throwed! HOT 7
- JsonResourceManager does not dispose of (unix) File System Watches HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from my.extensions.localization.json.