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

Create ResourceKey, Razor IViewLocalizer example #31

Open
damienbod opened this issue Apr 16, 2017 · 2 comments
Open

Create ResourceKey, Razor IViewLocalizer example #31

damienbod opened this issue Apr 16, 2017 · 2 comments

Comments

@damienbod
Copy link
Owner

How shound be format for the ResourceKey field for use inside View (injecting IViewLocalizer) ? I try for example: Views.Home.Index .. not works
Or, in other words – Can use IViewLocalizer

@harveytriana
Copy link

Two things:

  1. In configuration:
    services.AddSqlLocalization(options => options.UseTypeFullNames = false);
    That is, use false, so the class name is the same ResourcesKey in database (like HomeController or SharedResources)

Explanation:
In class SqlStringLocalizerFactory:
// classes go this way
public IStringLocalizer Create(Type resourceSource) {...}
...
// Views got this way
public IStringLocalizer Create(string baseName, string location) {...}

  1. I think that is a bug in second approach, the resouces key includes the location, and the database not exists that entry , I do this workaround:
    public IStringLocalizer Create(string baseName, string location)
    {
    // fix
    var resourceSource = baseName.Replace(location, string.Empty).Substring(1);
    if (_resourceLocalizations.Keys.Contains(resourceSource)) // goodbye: baseName + location
    {
    return _resourceLocalizations[resourceSource];
    }
    var sqlStringLocalizer = new SqlStringLocalizer(GetAllFromDatabaseForResource(resourceSource), resourceSource, false);
    return _resourceLocalizations.GetOrAdd(resourceSource, sqlStringLocalizer);
    }

Then, in my database i can use ResourcesKey like: Views.Home.Index
And works fine!

Perhaps Damien Bod should consider making this change in a future publication.

Regards

@OanaSorina
Copy link

Hello! I am struggling the same issue with IViewLocalizer usage. The fix for correcting the resourceSource is not in the latest release. When will it be released? I cannot use IViewLocalizer in my Razor pages, unless I get the source code for SqlLocalizer and apply the above fix.

Thanks! Oana

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants