<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using System.Threading;
using System.IO;
using System.Reflection;
using System.ComponentModel;
using System.Diagnostics.Contracts;

namespace SierraLib.Translation
{

    public class TranslationManager
    {
        public TranslationManager()
        {
            Translations = new TranslationList();
        }

        private static TranslationManager _translationManager;

        public event EventHandler LanguageChanged;

        public CultureInfo CurrentLanguage
        {
            get { return Thread.CurrentThread.CurrentUICulture; }
            set
            {
                if (value != Thread.CurrentThread.CurrentUICulture)
                {
                    Thread.CurrentThread.CurrentUICulture = value;
                    RefreshCurrentTranslation();
                    OnLanguageChanged();
                }
            }
        }

		private CultureInfo _DefaultLanguage = new CultureInfo("en");
		public CultureInfo DefaultLanguage
		{
			get { return _DefaultLanguage; }
			set 
			{
				_DefaultLanguage = value;
				RefreshCurrentTranslation();
				OnLanguageChanged();
			}
		}

        public IEnumerable<CultureInfo> Languages
        {
            get
            {
                Contract.Ensures(Contract.Result<IEnumerable<CultureInfo>>() != null);

                if (Translations != null)
                    foreach (ITranslation translation in Translations)
                        yield return translation.Culture;
            }
        }

        public static TranslationManager Instance
        {
            get
            {
                if (_translationManager == null)
                    _translationManager = new TranslationManager();
                return _translationManager;
            }
        }

        public TranslationList Translations { get; private set; }

        public ITranslation CurrentTranslation
        {
            get;
            private set;
        }
        
        private void OnLanguageChanged()
        {
            if (LanguageChanged != null)
            {
                LanguageChanged(this, EventArgs.Empty);
            }
        }
        
        public string this[string key]
        {
            get
            {
                return this[key, "!!!" + key];
            }
        }

        public string this[string key, string defaultValue]
        {
            get
            {
                if (CurrentTranslation == null)
                    RefreshCurrentTranslation();

                if (CurrentTranslation != null)
                {
                    string translatedValue = CurrentTranslation[key, defaultValue];
                    if (translatedValue != null)
                        return translatedValue;
                }
                return defaultValue;
            }
        }

		/// <summary>
		/// Loads the best available translation for the current thread's UI culture
		/// </summary>
		public void LoadBest()
		{
			LoadBest(Thread.CurrentThread.CurrentUICulture);
		}

		/// <summary>
		/// Loads the best available translation for the specified culture
		/// </summary>
		/// <param name="targetCulture">The <see cref="CultureInfo"/> describing the ideal translation to use</param>
		public void LoadBest(CultureInfo targetCulture)
		{
			if (targetCulture == CurrentLanguage)
			{
				RefreshCurrentTranslation();
				OnLanguageChanged();
			}
			else
				CurrentLanguage = targetCulture;
		}

        void RefreshCurrentTranslation()
        {
            string current = Thread.CurrentThread.CurrentUICulture.Name;

            //First, we attempt to find a translator for this exact dialect
            while (true)
            {
                foreach (ITranslation translation in Translations)
                    if (translation.Culture.Name == current)
                    {
                        CurrentTranslation = translation;
                        return;
                    }

                //If we get here, it means that we couldn't find anything, so
                //we will just resort to using the defaults specified, or showing keys
                if (current.Length == 0)
                {
					CurrentTranslation = Translations.FirstOrDefault(x => x.Culture == DefaultLanguage) ?? new DesignerTranslation();
					return;
                }

                //If we get here it means we didn't find one, so lets try
                //broadening the search
                int index = current.IndexOf("-");
                if (index < 0)
                    current = "";
                else<commit_msg>Further fix for default language #50<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using System.Threading;
using System.IO;
using System.Reflection;
using System.ComponentModel;
using System.Diagnostics.Contracts;

namespace SierraLib.Translation
{

    public class TranslationManager
    {
        public TranslationManager()
        {
            Translations = new TranslationList();
        }

        private static TranslationManager _translationManager;

        public event EventHandler LanguageChanged;

        public CultureInfo CurrentLanguage
        {
            get { return Thread.CurrentThread.CurrentUICulture; }
            set
            {
                if (value != Thread.CurrentThread.CurrentUICulture)
                {
                    Thread.CurrentThread.CurrentUICulture = value;
                    RefreshCurrentTranslation();
                    OnLanguageChanged();
                }
            }
        }

		private CultureInfo _DefaultLanguage = new CultureInfo("en");
		public CultureInfo DefaultLanguage
		{
			get { return _DefaultLanguage; }
			set 
			{
				_DefaultLanguage = value;
				RefreshCurrentTranslation();
				OnLanguageChanged();
			}
		}

        public IEnumerable<CultureInfo> Languages
        {
            get
            {
                Contract.Ensures(Contract.Result<IEnumerable<CultureInfo>>() != null);

                if (Translations != null)
                    foreach (ITranslation translation in Translations)
                        yield return translation.Culture;
            }
        }

        public static TranslationManager Instance
        {
            get
            {
                if (_translationManager == null)
                    _translationManager = new TranslationManager();
                return _translationManager;
            }
        }

        public TranslationList Translations { get; private set; }

        public ITranslation CurrentTranslation
        {
            get;
            private set;
        }
        
        private void OnLanguageChanged()
        {
            if (LanguageChanged != null)
            {
                LanguageChanged(this, EventArgs.Empty);
            }
        }
        
        public string this[string key]
        {
            get
            {
                return this[key, "!!!" + key];
            }
        }

        public string this[string key, string defaultValue]
        {
            get
            {
                if (CurrentTranslation == null)
                    RefreshCurrentTranslation();

                if (CurrentTranslation != null)
                {
                    string translatedValue = CurrentTranslation[key, defaultValue];
                    if (translatedValue != null)
                        return translatedValue;
                }
                return defaultValue;
            }
        }

		/// <summary>
		/// Loads the best available translation for the current thread's UI culture
		/// </summary>
		public void LoadBest()
		{
			LoadBest(Thread.CurrentThread.CurrentUICulture);
		}

		/// <summary>
		/// Loads the best available translation for the specified culture
		/// </summary>
		/// <param name="targetCulture">The <see cref="CultureInfo"/> describing the ideal translation to use</param>
		public void LoadBest(CultureInfo targetCulture)
		{
			if (targetCulture == CurrentLanguage)
			{
				RefreshCurrentTranslation();
				OnLanguageChanged();
			}
			else
				CurrentLanguage = targetCulture;
		}

        void RefreshCurrentTranslation()
        {
            string current = Thread.CurrentThread.CurrentUICulture.Name;
			
            //First, we attempt to find a translator for this exact dialect
            while (true)
            {
                foreach (ITranslation translation in Translations)
                    if (translation.Culture.Name == current)
                    {
                        CurrentTranslation = translation;
                        return;
                    }

                //If we get here, it means that we couldn't find anything, so
                //we will just resort to using the defaults specified, or showing keys
                if (current.Length == 0)
                {
					CurrentTranslation = Translations.FirstOrDefault(x => x.Culture.Equals(DefaultLanguage)) ?? new DesignerTranslation();
					return;
                }

                //If we get here it means we didn't find one, so lets try
                //broadening the search
                int index = current.IndexOf("-");
                if (index < 0)
                    current = "";
                else
{'fraction_non_alphanumeric': 0.07350300982152286, 'fraction_numerical': 0.0006336466363924385, 'mean_word_length': 1.5266808964781218, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '402173', 'n_tokens_mistral': 2325, 'n_tokens_neox': 2037, 'n_words': 579}