GithubHelp home page GithubHelp logo

Comments (31)

hishamco avatar hishamco commented on July 20, 2024 1

@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.

shahabganji avatar shahabganji commented on July 20, 2024 1

@hishamco The PR created, hope it helps

from my.extensions.localization.json.

hishamco avatar hishamco commented on July 20, 2024 1

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.

hishamco avatar hishamco commented on July 20, 2024 1

Also I will try your change to check if it's work with me too on Windows

from my.extensions.localization.json.

shahabganji avatar shahabganji commented on July 20, 2024 1

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.

hishamco avatar hishamco commented on July 20, 2024 1

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.

hishamco avatar hishamco commented on July 20, 2024

Thanks for reporting this, but did you try to run the sample, it should work as expected

from my.extensions.localization.json.

shahabganji avatar shahabganji commented on July 20, 2024

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.

hishamco avatar hishamco commented on July 20, 2024

I need to check the sample with your changes first before I review the PR

Thanks

from my.extensions.localization.json.

hishamco avatar hishamco commented on July 20, 2024

@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.

hishamco avatar hishamco commented on July 20, 2024

Merged into dev

from my.extensions.localization.json.

shahabganji avatar shahabganji commented on July 20, 2024

@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 :

Screen Shot 2020-04-03 at 13 25 59

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.

hishamco avatar hishamco commented on July 20, 2024

That means the error occurs when you are using IStringLocalizer alone or beside IStringLocalizer<T> right?

from my.extensions.localization.json.

shahabganji avatar shahabganji commented on July 20, 2024

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.

shahabganji avatar shahabganji commented on July 20, 2024

Also changing Line 174 to

.SetBasePath(Path.Combine(Directory.GetCurrentDirectory() ,_resourcesPath))

solved the issue.

from my.extensions.localization.json.

hishamco avatar hishamco commented on July 20, 2024

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.

shahabganji avatar shahabganji commented on July 20, 2024

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.

hishamco avatar hishamco commented on July 20, 2024

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.

shahabganji avatar shahabganji commented on July 20, 2024

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.

hishamco avatar hishamco commented on July 20, 2024

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.

hishamco avatar hishamco commented on July 20, 2024

This is a French version of how Privacy page is look like at my end

Privacy

from my.extensions.localization.json.

shahabganji avatar shahabganji commented on July 20, 2024

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.

hishamco avatar hishamco commented on July 20, 2024

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.

shahabganji avatar shahabganji commented on July 20, 2024

No problem I'll put a time on that.

from my.extensions.localization.json.

hishamco avatar hishamco commented on July 20, 2024

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.

shahabganji avatar shahabganji commented on July 20, 2024

Sure, just give me some time till evening, currently at the middle of some important stuff, is that okay?

from my.extensions.localization.json.

hishamco avatar hishamco commented on July 20, 2024

Take your time .. I will try to fix and improve something else meanwhile

from my.extensions.localization.json.

hishamco avatar hishamco commented on July 20, 2024

I will have a look ...

Thanks

from my.extensions.localization.json.

shahabganji avatar shahabganji commented on July 20, 2024

@hishamco Please also update the nuget feed. thanks 🙏

from my.extensions.localization.json.

hishamco avatar hishamco commented on July 20, 2024

Do you mean upload a NuGet package?

from my.extensions.localization.json.

shahabganji avatar shahabganji commented on July 20, 2024

from my.extensions.localization.json.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.