using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using System.Diagnostics;
using System.Globalization;

namespace ThickTcpUiInWindow
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : Application
    {
        /// <summary>
        /// 根据当前的culture 更新语言库
        /// </summary>
        /// <param name="culture_preorder"></param>
        public static void UpdateCulture(string culture_preorder)
        {
            CultureInfo currentCulture = CultureInfo.CurrentCulture;
            string requestedCulture_path = culture_preorder + currentCulture.Name + ".xaml";

            ResourceDictionary res = Application.Current.Resources;

            //目标语言
            ResourceDictionary landRd =
                res.MergedDictionaries.FirstOrDefault(d => d.Source.OriginalString == requestedCulture_path);

            //全部语言包
            //Regex r = new Regex(@"/StringBase;component/String/StringResource.(\w{5}).xaml");

            //int culture_cnt = res.MergedDictionaries.Count(d => r.IsMatch(d.Source.OriginalString));

            if (landRd == null)//目标语言还没添加
            {
                try
                {
                    landRd = new ResourceDictionary();
                    landRd.Source = new Uri(
                        culture_preorder +
                    currentCulture.Name + ".xaml", UriKind.Relative);

                    //landRd = Application.LoadComponent(
                    //new Uri(
                    //    @"/StringBase;component/String/StringResource." +
                    //currentCulture.Name + ".xaml", UriKind.Relative)) as ResourceDictionary;

                }
                catch (Exception e)
                {

                }
                if (landRd != null)
                {
                    res.MergedDictionaries.Add(landRd);
                }
            }
            else//改变排序，资源排在最后！！！！
            {
                res.MergedDictionaries.Remove(landRd);
                res.MergedDictionaries.Add(landRd);
            }
        }
    }
}
