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

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

Comments

Projects
None yet
2 participants
@Alphapage
Contributor

Alphapage commented Nov 19, 2012

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

This comment has been minimized.

Show comment
Hide comment
@slodge

slodge Nov 19, 2012

Contributor

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

Contributor

slodge commented Nov 19, 2012

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

This comment has been minimized.

Show comment
Hide comment
@Alphapage

Alphapage Nov 19, 2012

Contributor

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());
}
}
}

:)

Contributor

Alphapage commented Nov 19, 2012

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

martijn00 added a commit to martijn00/MvvmCross that referenced this issue Dec 8, 2016

Merge pull request #53 from zzcgumn/bugfix/lifetimenotifications
Creates a convenience iOS app delegate base class which merges Xamarin.Forms and MvvmCross publication of app life time updates.

martijn00 pushed a commit to martijn00/MvvmCross that referenced this issue Dec 8, 2016

martijn00 added a commit to martijn00/MvvmCross that referenced this issue Dec 8, 2016

Merge pull request #120 from g0rdan/master
Location plugin: Error handling is missing for iOS #53

kjeremy pushed a commit to kjeremy/MvvmCross that referenced this issue Dec 11, 2016

Merge pull request #53 from martijn00/ForceReplaceFragment
Add possibility to force replace a fragment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment