XAMLMarkupExtensions/WPFLocalizeExtension

Allow outputting the key itself (without "Key: ") if missing from localization dictionary

Opened this issue · 7 comments

Hello,

Many times there're texts coming from a remote system, then you would want to localize if possible (by using the text as key) else if it doesn't exist as key - output it as is (without the "Key: " prefix).

I don't see an easy workaround, will be very helpful to add this property per usage (and not in dictionary instance etc.):

if (!OutputKeyAsFallback)
{
    if (LocalizeDictionary.Instance.OutputMissingKeys
        && !string.IsNullOrEmpty(_key) && (targetType == typeof(String) || targetType == typeof(object)))
    {
        if (missingKeyEventResult.MissingKeyResult != null)
            result = missingKeyEventResult.MissingKeyResult;
        else
            result = "Key: " + _key;
    }
}
else
{
    result = _key;
}

Thank you for this amazing package.

Hello, @superware!
You can try to use LocalizeDictionary.Instance.MissingKeyEvent. Here the sample how you can set any string what you want:

private void Instance_MissingKeyEvent(object sender, MissingKeyEventArgs e)
{
e.MissingKeyResult = "Hello World";
}

Hi @Karnah!

Your suggestion is per dictionary (and requires coding), what about per usage?

<StackPanel>
  <TextBlock Text="{lex:Loc Product_name}" />
  <TextBlock Text="{lex:Loc {Binding Item.ProductName}, OutputKeyAsFallback=True}" />
</StackPanel>

It's a very powerful feature. You're not always localizing known/static strings, sometimes you have a string (usually from an external source) which you might have localized, but if not then your only true fallback is the string itself.

This should be a simple non-breaking feature...

Thanks!

I see here three possible ways:

  1. Add just property OutputKeyAsFallback. This is quick and easy
  2. Add property and enum FallbackBehavior. Possible values: Default ("Key: " + _key), Key (_key), EmptyString (string.Emty). It's also easy and a bit more flexible.
  3. Add property FallbackValue. It can be really hard, because it should support Binding. But it can be really powerful feature.

@konne , @superware, what do you think?

@Karnah thank you for your comments!

Both 1 and 2 will be perfect.

Hello, @superware.
I have some ideas about refactoring XAMLMarkupExtensions which will allow implementing the third option. I think it's the beast solution, because it's the most powerful feature.
If it doesn't work, I'll try to implement the first or second way.
Thanks!

Thanks @Karnah! Is there something I can already test?

Hello, @superware.
I'm so sorry, I didn't have time to implement the third solution. And I really don't know when I can do it.
I hope I can create PR with the first or second solution on next week. I thinks it's the best way now.