LazZiya / XLocalizer

Localizer package for Asp.Net Core web applications, powered by online translation and auto resource creating.
https://docs.ziyad.info
129 stars 14 forks source link

After updating to 1.0.2, XML approach could not get value for neutral language (en culture). #19

Closed mohamedryan closed 3 years ago

mohamedryan commented 3 years ago

Hi @LazZiya ,

Hope you are doing well,

This issue happened after I updated the localizer version to 1.0.2. I'm using the XML approach with autoKey and translate options.

Before the translation work perfect for both languages (Arabic, English).

LocSource.ar.xml image

LocSource.en.xml image

And the value was getting correctly. After updating, only the key name is getting with 'en' culture, the Arabic is working fine.

English Culture image Arabic Culture image

I returned to the previous version until the issue is fixed. I hope this issue is clear to you. And Many Thanks to you.

LazZiya commented 3 years ago

Hi @mohamedryan ,

I'm fine thanks, hope you are fine too 😊.

The final update 1.0.2 intended to enhance a performance issue when the current culture is same as the TranslateFromCulture. In other words, XLocalizer will skip localizing the source translation culture.

So instead of using coded key strings in the views, you have to use a clear texts of the source translation culture inside the views, e.g.: Assume we have a view with one title and one paragraph, and we have defined TranslateFromCultre = en:

<!-- This will not be localized if current culture in "en" -->
<!-- but will be localized for all other cultures -->
<h1 localize-content>Covid19TipsTitle</h1>
<p localize-content>Covid19TipsDesc</p>
<!-- This will be localized for all cultures other than "en" -->
<!-- since it is a clear "en" text, no need for localization if current culture is "en" -->
<h1 localize-content>COVID-19 Prevention Tips</h1>
<p localize-content>
    Adhere to the rules of spatial distancing...
</p>

You can use any culture in the views, but you have to set the TranslateFromCulture value in XLocalizerOption same as the culture used in views:

services.AddRazorPages()
        .AddXLocalizer<LocSource, MyTranslateService>(ops =>
        {
             // ...
             ops.TranslateFromCulture = "en";
        });

Notice: if you don't set a value for TranslateFromCulture it will be same as the DefaultRequestCulture in RequestLocalizationOptions.

LazZiya commented 3 years ago

Just re-evaluated your issue, I think it could be a use case where you need to use codes instead of texts inside views. So, I will add an option to bypass or include source translation culture in localization. It will look something like below:

services.AddRazorPages()
        .AddXLocalizer<LocSource, MyTranslateService>(ops =>
        {
             // ...

             // true: don't localize if current culture == source culture
             // false: always localize 
             ops.BypassSourceCulture = false;
        });

By default it will be true that means the source translation culture will not be localized, but if you set the value to false then the source culture will be localized as in the previous version.

I will provide this option in the upcoming update...

mohamedryan commented 3 years ago

@LazZiya , I believe it will be an important option, as for sure there are some users who used a lot of coded keys, so this update will require him to do many changes in his code.

but for me, I don't have too many coded keys, so I will go with the clear texts for now.

Thanks and Regards. 😄

LazZiya commented 3 years ago

Fix is now available in XLocalizer v1.0.3 (for XML and RESX resource types). And XLocalizer.DB v1.0.2 (for DB resource type).

Both can be downloaded from nuget:

BR; Ziya

LazZiya commented 3 years ago

Explanation to use the fix:

services.AddRazorPages()
    .AddXLocalizer<LocSource, MyMemoryTranslateService>(ops =>
    {
        // ...
        ops.LocalizeDefaultCulture = true;
    });
}

The value of LocalizeDefaultCulture is false by default, just make it true and you will be able to localize default culture as well.

Here is a sample auto added key-value when source-culture and target-cultures are the same:

<data isActive="false">
  <key>Welcome</key>
  <value>Welcome</value>
  <comment>Created by XLocalizer</comment>
</data>

Limitation:

Since online translation services requires two different languages to translate, so online translation will not work when the source and target cultures are same. Anyhow, AutoAddKeys is still available and it will add the key and value to the resource file.

Hope this will help :)