234 lines
9.5 KiB
C#
234 lines
9.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.Specialized;
|
|
using System.Configuration;
|
|
using System.Globalization;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Web;
|
|
using ASC.TeamlabSite.MultiRegionHelper;
|
|
|
|
public static class LanguageProvider
|
|
{
|
|
private const string SELECTED = "culture.selected";
|
|
|
|
private static readonly Dictionary<string, CultureInfo> cultureUiMap = new Dictionary<string, CultureInfo>()
|
|
{
|
|
{ "", CultureInfo.GetCultureInfo("en-US") },
|
|
{ "fr", CultureInfo.GetCultureInfo("fr-FR") },
|
|
{ "es", CultureInfo.GetCultureInfo("es-ES") },
|
|
{ "de", CultureInfo.GetCultureInfo("de-DE") },
|
|
{ "ru", CultureInfo.GetCultureInfo("ru-RU") },
|
|
{ "lv", CultureInfo.GetCultureInfo("lv-LV") },
|
|
};
|
|
|
|
private static readonly Dictionary<string, TimeZoneInfo> timeZones = new Dictionary<string, TimeZoneInfo>()
|
|
{
|
|
{ "", TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time") },
|
|
{ "fr", TimeZoneInfo.FindSystemTimeZoneById("Romance Standard Time") },
|
|
{ "es", TimeZoneInfo.FindSystemTimeZoneById("Romance Standard Time")},
|
|
{ "de", TimeZoneInfo.FindSystemTimeZoneById("W. Europe Standard Time") },
|
|
{ "ru", TimeZoneInfo.FindSystemTimeZoneById("Russian Standard Time") },
|
|
{ "lv", TimeZoneInfo.FindSystemTimeZoneById("FLE Standard Time") },
|
|
};
|
|
|
|
|
|
static LanguageProvider()
|
|
{
|
|
try
|
|
{
|
|
var languages = ConfigurationManager.AppSettings["locale-enabled"];
|
|
if (!string.IsNullOrEmpty(languages))
|
|
{
|
|
cultureUiMap = new Dictionary<string, CultureInfo> { { "", CultureInfo.GetCultureInfo("en-US") } };
|
|
languages
|
|
.Split(',', ';')
|
|
.Select(x => x.Trim())
|
|
.ToList()
|
|
.ForEach(ln => cultureUiMap.Add(ln.Substring(0, 2), CultureInfo.GetCultureInfo(ln)));
|
|
}
|
|
}
|
|
catch { }
|
|
}
|
|
|
|
|
|
public static IDictionary<string, CultureInfo> GetAvailibleLanguages()
|
|
{
|
|
return cultureUiMap.ToDictionary(key => MakeUrl(key.Key), value => value.Value);
|
|
}
|
|
|
|
public static IDictionary<string, string> GetAvailibleLanguagesName()
|
|
{
|
|
var result = new Dictionary<string, string>();
|
|
cultureUiMap.ToList().ForEach(el => result.Add(el.Key, el.Value.DisplayName));
|
|
return result;
|
|
}
|
|
|
|
public static string GetLanguageName(CultureInfo culture)
|
|
{
|
|
try
|
|
{
|
|
//capitalize
|
|
return char.ToUpper(culture.NativeName[0]) + culture.NativeName.Substring(1);
|
|
}
|
|
catch { }
|
|
return string.Empty;
|
|
}
|
|
|
|
public static CultureInfo GetCurrentCulture()
|
|
{
|
|
if (HttpContext.Current != null)
|
|
{
|
|
var _log = log4net.LogManager.GetLogger("ASC.LanguageProvider");
|
|
if (HttpContext.Current.Items[SELECTED] == null || !(HttpContext.Current.Items[SELECTED] is CultureInfo))
|
|
{
|
|
_log.Debug("HttpContext.Current.Items[SELECTED] == null || !(HttpContext.Current.Items[SELECTED] is CultureInfo = TRUE");
|
|
if (string.IsNullOrEmpty(GetCultureKey()))
|
|
{
|
|
_log.Debug("HttpContext.Current.Items[SELECTED] == null || !(HttpContext.Current.Items[SELECTED] is CultureInfo = TRUE");
|
|
if (HttpContext.Current.Session != null && HttpContext.Current.Session["redirected"] == null)
|
|
{
|
|
_log.Debug("HttpContext.Current.Session['redirected'] = null");
|
|
try
|
|
{
|
|
var currentRegionDbEntity = MultiRegionHelper.Instance.GetRegionDbEntityFull();
|
|
var priorityLng = currentRegionDbEntity.Lang;
|
|
if (!String.IsNullOrEmpty(priorityLng)) {
|
|
var acceptLng = HttpContext.Current.Request.Headers["Accept-Language"];
|
|
if (!string.IsNullOrEmpty(acceptLng))
|
|
{
|
|
try
|
|
{
|
|
var priorityLngs = acceptLng.Split(',')
|
|
.Select(x => x.Split(';').FirstOrDefault())
|
|
.Where(x => !string.IsNullOrEmpty(x)).Select(x => x.Split('-').FirstOrDefault())
|
|
.Select(x => "en".Equals(x, StringComparison.OrdinalIgnoreCase) ? string.Empty : x);
|
|
if (priorityLngs.Any())
|
|
{
|
|
priorityLng = priorityLngs.Where(x => x != null && cultureUiMap.ContainsKey(x)).FirstOrDefault();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_log.Error(ex);
|
|
}
|
|
}
|
|
}
|
|
_log.Debug("priorityLng = " + priorityLng);
|
|
|
|
if (!String.IsNullOrEmpty(priorityLng) && cultureUiMap.ContainsKey(priorityLng))
|
|
{
|
|
if (HttpContext.Current.Session != null)
|
|
HttpContext.Current.Session["redirected"] = true;
|
|
var newUrl = MakeUrl(priorityLng);
|
|
_log.Debug("newUrl = " + newUrl);
|
|
|
|
HttpContext.Current.Response.Redirect(newUrl, false);
|
|
HttpContext.Current.Response.Clear();
|
|
HttpContext.Current.ApplicationInstance.CompleteRequest();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_log.Error(ex);
|
|
}
|
|
}
|
|
}
|
|
|
|
string cultureKey = GetCultureKey() ?? string.Empty;
|
|
_log.Debug("130. cultureKey = " + cultureKey);
|
|
if (cultureUiMap.ContainsKey(cultureKey))
|
|
{
|
|
var culture = cultureUiMap[cultureKey];
|
|
HttpContext.Current.Items[SELECTED] = culture;
|
|
return culture;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
return (CultureInfo)HttpContext.Current.Items[SELECTED];
|
|
}
|
|
}
|
|
return CultureInfo.CurrentUICulture;
|
|
}
|
|
|
|
private static string GetCultureKey()
|
|
{
|
|
var cultureKey = string.Empty;
|
|
if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["locale-mod"]))
|
|
{
|
|
cultureKey = HttpContext.Current.Request[ConfigurationManager.AppSettings["locale-mod"]];
|
|
}
|
|
else
|
|
{
|
|
//Try select from request
|
|
if (HttpContext.Current.Request.ApplicationPath != null)
|
|
{
|
|
cultureKey = HttpContext.Current.Request.ApplicationPath.TrimStart('/');
|
|
}
|
|
}
|
|
return cultureKey;
|
|
}
|
|
|
|
private static string MakeUrl(string lngKey)
|
|
{
|
|
if (HttpContext.Current != null && HttpContext.Current.Request != null)
|
|
{
|
|
//get request url
|
|
var request = HttpContext.Current.Request;
|
|
var url = request.Url;
|
|
if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["locale-mod"]))
|
|
{
|
|
var lngQueryKey = ConfigurationManager.AppSettings["locale-mod"];
|
|
url = new Uri(url, url.AbsolutePath + SetUrlParam(lngQueryKey, lngKey, request.QueryString));
|
|
}
|
|
else
|
|
{
|
|
//Based on app path
|
|
if (request.ApplicationPath != null)
|
|
{
|
|
var urlApp = "/" + string.Format("/{0}/{1}", lngKey, request.Url.PathAndQuery.Substring(request.ApplicationPath.Length).TrimStart('/')).TrimStart('/');
|
|
url = new Uri(url, urlApp);
|
|
}
|
|
}
|
|
return url.ToString();
|
|
}
|
|
return string.Empty;
|
|
}
|
|
|
|
private static string SetUrlParam(string param, string value, NameValueCollection queryStringOriginal)
|
|
{
|
|
var queryString = new NameValueCollection(queryStringOriginal);
|
|
if (!string.IsNullOrEmpty(value))
|
|
{
|
|
if (!string.IsNullOrEmpty(queryString[param]))
|
|
{
|
|
queryString[param] = value;
|
|
}
|
|
else
|
|
{
|
|
queryString.Add(param, value);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
//Remove value
|
|
queryString.Remove(param);
|
|
}
|
|
var query = new StringBuilder();
|
|
if (queryString.Count > 0)
|
|
{
|
|
query.Append("?");
|
|
foreach (var key in queryString.AllKeys)
|
|
{
|
|
if (!string.IsNullOrEmpty(queryString[key]))
|
|
{
|
|
query.AppendFormat("{0}={1}&", HttpUtility.UrlPathEncode(key), HttpUtility.UrlPathEncode(queryString[key]));
|
|
}
|
|
}
|
|
}
|
|
return query.ToString().TrimEnd('&');
|
|
}
|
|
|
|
}
|