<commit_before>﻿/*
 * =| ADAPTIVE RUNTIME PLATFORM |=======================================================================================
 *
 * (C) Copyright 2013-2014 <NAME> t/a Adaptive.me <http://adaptive.me>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 *
 * Original author:
 *
 *     * <NAME>
 *                 <http://github.com/carloslozano>
 *                 <http://twitter.com/adaptivecoder>
 *                 <mailto:<EMAIL>>
 *
 * Contributors:
 *
 *     * <NAME>
 *             <https://github.com/aryslan>
 *             <mailto:<EMAIL>>
 *
 * =====================================================================================================================
 */

using Adaptive.Arp.Api;
using Adaptive.Arp.Impl.Globalization;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;
using Windows.Storage;

namespace Adaptive.Arp.Impl.WinPhone
{
    public class GlobalizationImpl : IGlobalization
    {
        protected const String APP_CONFIG_PATH = @"Html\App";
        protected const String DICT_TAG = "dict";
        protected const String I18N_CONFIG_FILE = @"Html\App\i18n-config.xml";
        protected const String KEY_TAG = "key";
        protected const String PLIST_EXTENSION = ".plist";
        protected GlobalizationConfig _I18NConfiguration = null;
        protected Dictionary<string, SortedDictionary<String, string>> languageFilesDictionary = new Dictionary<string, SortedDictionary<string, string>>();

        public GlobalizationImpl()
        {
            //ManualResetEvent AllFinished = new ManualResetEvent(false);
            TaskFactory taskFactory = new TaskFactory();
            var myTask = taskFactory.StartNew<Task<bool>>(async () =>
            {
                try
                {
                    StorageFile I18NConfigFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri(@"ms-appx:///" + I18N_CONFIG_FILE));
                    XmlSerializer serializer = new XmlSerializer(typeof(GlobalizationConfig));
                    XmlReader reader = XmlReader.Create(await I18NConfigFile.OpenStreamForReadAsync());
                    _I18NConfiguration = (GlobalizationConfig)serializer.Deserialize(reader);
                    FillLanguagesDictionary();
                    //AllFinished.Set();
                    return true;
                }
                catch (Exception)
                {
                    //AllFinished.Set();
                    return false;
                }
            });
            //AllFinished.WaitOne();
            /*if (myTask.Result.Result)
            {
                //ALL OK
            }
            else
            {
                //ERROR HAPPENED
            }*/
        }

        public string[] GetLocaleSupportedDescriptors()
        {
            return (languageFilesDictionary != null && languageFilesDictionary.Count > 0) ? languageFilesDictionary.Keys.ToArray() : null;
        }

        public string GetResourceLiteral(string key, Locale locale)
        {
            if (!String.IsNullOrWhiteSpace(key))
            {
                key = key.ToUpper();
                String sLocaleName = (locale == null) ? _I18NConfiguration.DefaultLanguage.Language.ToLower() : locale.ToString().ToLower();
                sLocaleName = (sLocaleName.StartsWith("-")) ? sLocaleName.Remove(0, 1) : sLocaleName;
                sLocaleName = (sLocaleName.EndsWith("-")) ? sLocaleName.Remove(sLocaleName.Length - 1, 1) : sLocaleName;
                if (languageFilesDictionary.ContainsKey(sLocaleName) && languageFilesDictionary[sLocaleName].ContainsKey(key))
                {
                    return languageFilesDictionary[sLocaleName][key].ToString();
                }
                else
                {
                    string sNewLocalName = sLocaleName.Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries)[0];
                    if (!sNewLocalName.Equals(sLocaleName) && !String.IsNullOrWhiteSpace(sNewLocalName))
                    {
                        return GetResourceLiteral(key, new Locale(sNewLocalName, String.Empty));
                    }
                }
            }
            return String.Empty;
        }

        public IDictionary<string, string> GetResourceLiterals(Locale locale)
        {
            String sLocaleName = (locale == null) ? _I18NConfiguration.DefaultLanguage.Language.ToLower() : locale.ToString().ToLower();
            sLocaleName = (sLocaleName.StartsWith("-")) ? sLocaleName.Remove(0, 1) : sLocaleName;
            sLocaleName = (sLocaleName.EndsWith("-")) ? sLocaleName.Remove(sLocaleName.Length - 1, 1) : sLocaleName;
            if (languageFilesDictionary.ContainsKey(sLocaleName))
                return languageFilesDictionary[sLocaleName];
            else
            {
                string sNewLocalName = sLocaleName.Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries)[0];
                if (!sNewLocalName.Equals(sLocaleName) && !String.IsNullOrWhiteSpace(sNewLocalName))
                {
                    return GetResourceLiterals(new Locale(sNewLocalName, String.Empty));
                }<commit_msg>Better fallback mechanism<commit_after>﻿/*
 * =| ADAPTIVE RUNTIME PLATFORM |=======================================================================================
 *
 * (C) Copyright 2013-2014 <NAME> t/a Adaptive.me <http://adaptive.me>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 *
 * Original author:
 *
 *     * <NAME>
 *                 <http://github.com/carloslozano>
 *                 <http://twitter.com/adaptivecoder>
 *                 <mailto:<EMAIL>>
 *
 * Contributors:
 *
 *     * <NAME>
 *             <https://github.com/aryslan>
 *             <mailto:<EMAIL>>
 *
 * =====================================================================================================================
 */

using Adaptive.Arp.Api;
using Adaptive.Arp.Impl.Globalization;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;
using Windows.Storage;

namespace Adaptive.Arp.Impl.WinPhone
{
    public class GlobalizationImpl : IGlobalization
    {
        protected const String APP_CONFIG_PATH = @"Html\App";
        protected const String DICT_TAG = "dict";
        protected const String I18N_CONFIG_FILE = @"Html\App\i18n-config.xml";
        protected const String KEY_TAG = "key";
        protected const String PLIST_EXTENSION = ".plist";
        protected GlobalizationConfig _I18NConfiguration = null;
        protected Dictionary<string, SortedDictionary<String, string>> languageFilesDictionary = new Dictionary<string, SortedDictionary<string, string>>();

        public GlobalizationImpl()
        {
            //ManualResetEvent AllFinished = new ManualResetEvent(false);
            TaskFactory taskFactory = new TaskFactory();
            var myTask = taskFactory.StartNew<Task<bool>>(async () =>
            {
                try
                {
                    StorageFile I18NConfigFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri(@"ms-appx:///" + I18N_CONFIG_FILE));
                    XmlSerializer serializer = new XmlSerializer(typeof(GlobalizationConfig));
                    XmlReader reader = XmlReader.Create(await I18NConfigFile.OpenStreamForReadAsync());
                    _I18NConfiguration = (GlobalizationConfig)serializer.Deserialize(reader);
                    FillLanguagesDictionary();
                    //AllFinished.Set();
                    return true;
                }
                catch (Exception)
                {
                    //AllFinished.Set();
                    return false;
                }
            });
            /*AllFinished.WaitOne();
            if (myTask.Result.Result)
            {
                //ALL OK
            }
            else
            {
                //ERROR HAPPENED
            }*/
        }

        public string[] GetLocaleSupportedDescriptors()
        {
            return (languageFilesDictionary != null && languageFilesDictionary.Count > 0) ? languageFilesDictionary.Keys.ToArray() : null;
        }

        public string GetResourceLiteral(string key, Locale locale)
        {
            if (!String.IsNullOrWhiteSpace(key))
            {
                key = key.ToUpper();
                String sLocaleName = (locale == null) ? _I18NConfiguration.DefaultLanguage.Language.ToLower() : locale.ToString().ToLower();
                sLocaleName = (sLocaleName.StartsWith("-")) ? sLocaleName.Remove(0, 1) : sLocaleName;
                sLocaleName = (sLocaleName.EndsWith("-")) ? sLocaleName.Remove(sLocaleName.Length - 1, 1) : sLocaleName;
                if (languageFilesDictionary.ContainsKey(sLocaleName) && languageFilesDictionary[sLocaleName].ContainsKey(key))
                {
                    return languageFilesDictionary[sLocaleName][key].ToString();
                }
                else
                {
                    string sNewLocalName = (!String.IsNullOrWhiteSpace(locale.GetLanguage())) ? locale.GetLanguage() : (!String.IsNullOrWhiteSpace(locale.GetCountry())) ? locale.GetCountry() : String.Empty;
                    if (!sNewLocalName.Equals(sLocaleName) && !String.IsNullOrWhiteSpace(sNewLocalName))
                    {
                        return GetResourceLiteral(key, new Locale(sNewLocalName, String.Empty));
                    }
                }
            }
            return String.Empty;
        }

        public IDictionary<string, string> GetResourceLiterals(Locale locale)
        {
            String sLocaleName = (locale == null) ? _I18NConfiguration.DefaultLanguage.Language.ToLower() : locale.ToString().ToLower();
            sLocaleName = (sLocaleName.StartsWith("-")) ? sLocaleName.Remove(0, 1) : sLocaleName;
            sLocaleName = (sLocaleName.EndsWith("-")) ? sLocaleName.Remove(sLocaleName.Length - 1, 1) : sLocaleName;
            if (languageFilesDictionary.ContainsKey(sLocaleName))
                return languageFilesDictionary[sLocaleName];
            else
            {
                string sNewLocalName = (!String.IsNullOrWhiteSpace(locale.GetLanguage())) ? locale.GetLanguage() : (!String.IsNullOrWhiteSpace(locale.GetCountry())) ? locale.GetCountry() : String.Empty;
                if (!sNewLocalName.Equals(sLocaleName) && !String.IsNullOrWhiteSpace(sNewLocalName))
                {
                    return GetResourceLiterals(new Locale(sNewLocalName, String.Empty));
                }
{'fraction_non_alphanumeric': 0.1335482776175123, 'fraction_numerical': 0.006787714237230613, 'mean_word_length': 2.2533811758211426, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 43, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5528271', 'n_tokens_mistral': 3062, 'n_tokens_neox': 2901, 'n_words': 628}