damienbod / AspNetCoreLocalization

Localization.SqlLocalizer & ASP.NET Core MVC Localization Examples
http://damienbod.com/2015/10/21/asp-net-5-mvc-6-localization/
MIT License
251 stars 101 forks source link

Return Key if localization is not found #22

Closed jimmy84 closed 7 years ago

jimmy84 commented 7 years ago

Hi, Is there anyway to return the key if the localization is not found instead of the searched key?

_localizer["This is a localize text"]

to

This is a localize text

instead of

SharedResource.This is a localize text.en-US

This would be extremely useful in View to have default text which tremendously reduce the database records which has all the default localize text.

Perhaps an options which consist one of the below

services.AddSqlLocalization(options => 
{
    options.ReturnKeyIfNotFound = true,
    options.ReturnSearchKeyIfNotFound = false,
});
damienbod commented 7 years ago

Yes, this would be possible, will need to make a small change, will add it to the next version

jimmy84 commented 7 years ago

Great can't wait! :)

damienbod commented 7 years ago

Hi @jimmy84 I reopened this, will close it once it implemented, thanks

frontegi commented 7 years ago

Thumb up for this feature ! Thanks Damien.

damienbod commented 7 years ago

@jimmy84 @frontegi Can you give your 2 cents?

The full computed key is returned per default. I like this because if a localization is missing from the database, I see what I need to add.

The following configuration returns the default keys, if the localization record does not exist. OK?

var useTypeFullNames = true;
var useOnlyPropertyNames = false;
var returnOnlyKeyIfNotFound = true;

services.AddSqlLocalization(options => options.UseSettings(
  useTypeFullNames, 
  useOnlyPropertyNames, 
  returnOnlyKeyIfNotFound
));

Here's the pull request.

https://github.com/damienbod/AspNet5Localization/pull/23

I will create a new package for this once this is merged.

Greetings Damien

jimmy84 commented 7 years ago

Looks great to me! I'm so excited! Haha.

damienbod commented 7 years ago

https://www.nuget.org/packages/Localization.SqlLocalizer/1.0.6

jimmy84 commented 7 years ago

@damienbod I tested the 1.0.6 version, it seems the returnOnlyKeyIfNotFound only works on SharedResouce (IHtmlLocalizer). It doesn't work on normal view(IViewLocalizer) localizer.

I have following config

services.AddSqlLocalization(options =>
{
    options.UseTypeFullNames = false;
    options.UseOnlyPropertyNames = false;
    options.ReturnOnlyKeyIfNotFound = true;
});

This is what I had in my _ViewImports.chtml

@inject IViewLocalizer Localizer
@inject IHtmlLocalizer<SharedResource> SharedLocalizer

Localizer not working. SharedLocalizer is working fine.