69 lines
1.9 KiB
C#
69 lines
1.9 KiB
C#
using LanguageDetection;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using Xceed.Wpf.Toolkit.PropertyGrid.Attributes;
|
|
|
|
namespace CtrEditor.FuncionesBase
|
|
{
|
|
public class Idiomas
|
|
{
|
|
public const string DEFAULT_LANGUAGE = "English";
|
|
|
|
private static readonly Dictionary<string, string> _languageMap = new Dictionary<string, string>
|
|
{
|
|
{ "en", "English" },
|
|
{ "es", "Spanish" },
|
|
{ "it", "Italian" },
|
|
{ "pt", "Portuguese" },
|
|
{ "fr", "French" },
|
|
{ "de", "German" }
|
|
};
|
|
|
|
private static readonly LanguageDetector _languageDetector;
|
|
|
|
static Idiomas()
|
|
{
|
|
_languageDetector = new LanguageDetector();
|
|
_languageDetector.AddLanguages("en", "es", "it", "pt", "fr", "de");
|
|
}
|
|
|
|
public static List<string> GetLanguageList()
|
|
{
|
|
return new List<string>(_languageMap.Values);
|
|
}
|
|
|
|
public static string DetectarIdioma(string texto)
|
|
{
|
|
if (string.IsNullOrEmpty(texto)) return DEFAULT_LANGUAGE;
|
|
|
|
try
|
|
{
|
|
string detectedLanguageCode = _languageDetector.Detect(texto);
|
|
return _languageMap.GetValueOrDefault(detectedLanguageCode, DEFAULT_LANGUAGE);
|
|
}
|
|
catch
|
|
{
|
|
return DEFAULT_LANGUAGE;
|
|
}
|
|
}
|
|
|
|
public static string GetLanguageCode(string languageName)
|
|
{
|
|
return _languageMap.FirstOrDefault(x => x.Value == languageName).Key ?? "en";
|
|
}
|
|
}
|
|
|
|
public class IdiomasItemsSource<T> : IItemsSource
|
|
{
|
|
public ItemCollection GetValues()
|
|
{
|
|
ItemCollection items = new ItemCollection();
|
|
foreach (string language in Idiomas.GetLanguageList())
|
|
{
|
|
items.Add(language);
|
|
}
|
|
return items;
|
|
}
|
|
}
|
|
}
|