Localization improvement: Check if standard language exists then process #53

Closed
Alphapage opened this Issue Nov 19, 2012 · 2 comments

2 participants

@Alphapage

Hello,

I was playing with Localization plugin and it appears that if a sub language folder for example fr-be or es-ar isn't found, the default language is displayed. It would be great to search for the standard language fr or es before switching to the default one. I tweaked the LoadResources from MvxTextProviderBuilder.cs like this:

public void LoadResources(string whichLocalisationFolder)
{
foreach (var kvp in ResourceFiles)
{
try
{
TextProvider.LoadJsonFromResource(_generalNamespaceKey, kvp.Key, GetResourceFilePath(whichLocalisationFolder, kvp.Value));
}
#if !NETFX_CORE
//catch (ThreadAbortException)
//{
// throw;
//}
#endif
catch (Exception exception)
{
MvxTrace.Trace(MvxTraceLevel.Warning, "Language file could not be loaded for {0}.{1} - {2}", whichLocalisationFolder, kvp.Key, exception.ToLongString());
if (whichLocalisationFolder.Contains("-"))
{
string standardLocalisationFolder = whichLocalisationFolder.Substring(0, whichLocalisationFolder.IndexOf("-"));
try
{
TextProvider.LoadJsonFromResource(_generalNamespaceKey, kvp.Key, GetResourceFilePath(standardLocalisationFolder, kvp.Value));
}
#if !NETFX_CORE
//catch (ThreadAbortException)
//{
// throw;
//}
#endif
catch (Exception exceptionStandard)
{
MvxTrace.Trace(MvxTraceLevel.Warning, "Language file could not be loaded for {0}.{1} - {2}", standardLocalisationFolder, kvp.Key, exceptionStandard.ToLongString());
}
}
}
}
}

You can add a Boolean to check whether or not you want to use this feature.

Hope it will help to improve MvvmCross.

@slodge

Interesting.

If you want this sort of behaviour then I think technically what we should do is:

  • load the default
  • then load the 'fr'
  • then load the 'fr-be'

That way you would get fallback behaviour at each layer - ie most of your Belgian French could be fr, with just a little in fr-be

This might be better done by an external wrapper - and I would be interested in putting this into a reusable plugin :)

Stuart

@Alphapage

Clever than me!

Here is my modified and smarter version:

public void LoadResources(string whichLocalisationFolder)

    {

        bool isSubLanguage = whichLocalisationFolder.Contains("-");

        string standardLocalisationFolder = isSubLanguage ? whichLocalisationFolder.Substring(0, whichLocalisationFolder.IndexOf("-")) : whichLocalisationFolder;

        loadResources(standardLocalisationFolder);

        if (isSubLanguage)
        {
            loadResources(whichLocalisationFolder);
        }
    }

void loadResources(string whichLocalisationFolder)
{
foreach (var kvp in ResourceFiles)
{
try
{
TextProvider.LoadJsonFromResource(_generalNamespaceKey, kvp.Key, GetResourceFilePath(whichLocalisationFolder, kvp.Value));
}
#if !NETFX_CORE
//catch (ThreadAbortException)
//{
// throw;
//}
#endif
catch (Exception exception)
{
MvxTrace.Trace(MvxTraceLevel.Warning, "Language file could not be loaded for {0}.{1} - {2}", whichLocalisationFolder, kvp.Key, exception.ToLongString());
}
}
}

:)

@Alphapage Alphapage closed this Nov 19, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment