Skip to content
Permalink
Browse files

pretty much got 5966 working just need to do some more tests.

  • Loading branch information...
Shazwazza committed Dec 17, 2014
1 parent 4e33552 commit db087a9e788e3239e746760f9097787502785149
@@ -1,11 +1,34 @@
using System;
using System.Globalization;
using System.Linq;
using Umbraco.Core.Models.Membership;
using Umbraco.Core.Services;

namespace Umbraco.Core.Models
{
internal static class UserExtensions
public static class UserExtensions
{
/// <summary>
/// Returns the culture info associated with this user, based on the language they're assigned to in the back office
/// </summary>
/// <param name="user"></param>
/// <param name="textService"></param>
/// <returns></returns>
public static CultureInfo GetUserCulture(this IUser user, ILocalizedTextService textService)
{
return GetUserCulture(user.Language, textService);
}

internal static CultureInfo GetUserCulture(string userLanguage, ILocalizedTextService textService)
{
return textService.GetSupportedCultures()
.FirstOrDefault(culture =>
//match on full name first
culture.Name.InvariantEquals(userLanguage.Replace("_", "-")) ||
//then match on the 2 letter name
culture.TwoLetterISOLanguageName.InvariantEquals(userLanguage));
}

/// <summary>
/// Checks if the user has access to the content item based on their start noe
/// </summary>
@@ -27,5 +27,11 @@ public interface ILocalizedTextService
/// </summary>
/// <returns></returns>
IDictionary<string, string> GetAllStoredValues(CultureInfo culture);

/// <summary>
/// Returns a list of all currently supported cultures
/// </summary>
/// <returns></returns>
IEnumerable<CultureInfo> GetSupportedCultures();
}
}
@@ -38,7 +38,7 @@ public LocalizedTextServiceFileSources(IRuntimeCacheProvider cache, DirectoryInf
foreach (var fileInfo in _fileSourceFolder.GetFiles("*.xml"))
{
var localCopy = fileInfo;
var filename = Path.GetFileNameWithoutExtension(localCopy.FullName);
var filename = Path.GetFileNameWithoutExtension(localCopy.FullName).Replace("_", "-");
var culture = CultureInfo.GetCultureInfo(filename);
//get the lazy value from cache
result.Add(culture, new Lazy<XDocument>(() => _cache.GetCacheItem<XDocument>(
@@ -158,6 +158,15 @@ public string Localize(string key, CultureInfo culture, IDictionary<string, stri
return result;
}

/// <summary>
/// Returns a list of all currently supported cultures
/// </summary>
/// <returns></returns>
public IEnumerable<CultureInfo> GetSupportedCultures()
{
return _xmlSource != null ? _xmlSource.Keys : _dictionarySource.Keys;
}

private string GetFromDictionarySource(CultureInfo culture, string area, string key, IDictionary<string, string> tokens)
{
if (_dictionarySource.ContainsKey(culture) == false)
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<language alias="cs" intName="Czech" localName="česky" lcid="5" culture="en-GB">
<language alias="cs" intName="Czech" localName="česky" lcid="5" culture="cs-CZ">
<creator>
<name>umbraco</name>
<link>http://umbraco.org</link>
@@ -5,7 +5,7 @@
<user>0</user>
<startNode>1080</startNode>
<fullTree>False</fullTree>
<documentTypeAlias>Base</documentTypeAlias>
<documentTypeAlias>Articulate</documentTypeAlias>
<fields>
<categories>
</categories>
@@ -117,7 +117,7 @@ public FormsAuthenticationTicket PerformLogin(IUser user)
StartContentNode = user.StartContentId,
StartMediaNode = user.StartMediaId,
Username = user.Username,
Culture = ui.Culture(user.Language)
Culture = ui.Culture(user)
});

LogHelper.Info<WebSecurity>("User Id: {0} logged in", () => user.Id);
@@ -275,7 +275,7 @@ public static void doLogin(User u)
StartContentNode = u.StartNodeId,
StartMediaNode = u.StartMediaId,
Username = u.LoginName,
Culture = ui.Culture(u.Language)
Culture = ui.Culture(u)

});
LogHelper.Info<BasePage>("User {0} (Id: {1}) logged in", () => u.Name, () => u.Id);
@@ -3,7 +3,6 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
@@ -13,9 +12,11 @@
using Umbraco.Core;
using Umbraco.Core.IO;
using Umbraco.Core.Logging;
using Umbraco.Core.Models;
using Umbraco.Core.Models.Membership;
using umbraco.BasePages;
using Umbraco.Core.Services;
using File = System.IO.File;
using User = umbraco.BusinessLogic.User;

namespace umbraco
@@ -27,7 +28,7 @@ namespace umbraco
/// The ui class handles the multilingual text in the umbraco back-end.
/// Provides access to language settings and language files used in the umbraco back-end.
/// </summary>
[Obsolete("Use the ITextManager instead which is available on most umbraco base classes and also on the ApplicationContext")]
[Obsolete("Use the ILocalizedTextService instead which is the ApplicationContext.Services")]
public class ui
{
private static readonly string UmbracoDefaultUiLanguage = GlobalSettings.DefaultUILanguage;
@@ -37,29 +38,18 @@ public class ui
[Obsolete("Get the current culture/language from the currently logged in IUser, the IUser object is available on most Umbraco base classes and on the UmbracoContext")]
public static string Culture(User u)
{
return Culture(u.Language);
var found = UserExtensions.GetUserCulture(u.Language, ApplicationContext.Current.Services.TextService);
return found == null ? string.Empty : found.Name;
}

[EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("Get the current culture/language from the currently logged in IUser, the IUser object is available on most Umbraco base classes and on the UmbracoContext")]
internal static string Culture(IUser u)
{
return Culture(u.Language);
var found = u.GetUserCulture(ApplicationContext.Current.Services.TextService);
return found == null ? string.Empty : found.Name;
}

internal static string Culture(string userLanguage)
{
var langFile = getLanguageFile(userLanguage);
try
{
return langFile.SelectSingleNode("/language").Attributes.GetNamedItem("culture").Value;
}
catch
{
return string.Empty;
}
}


[EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("Get the current culture/language from the currently logged in IUser, the IUser object is available on most Umbraco base classes and on the UmbracoContext")]
private static string GetLanguage()
@@ -112,14 +102,14 @@ private static string GetLanguage(string userLanguage)
/// <returns></returns>
public static string Text(string Key, User u)
{
return ApplicationContext.Current.Services.TextService.Localize(Key, CultureInfo.GetCultureInfo(GetLanguage(u)));
return ApplicationContext.Current.Services.TextService.Localize(Key, GetCultureFromUserLanguage(GetLanguage(u)));

//return GetText(string.Empty, Key, null, GetLanguage(u));
}

internal static string Text(string key, IUser u)
{
return ApplicationContext.Current.Services.TextService.Localize(key, CultureInfo.GetCultureInfo(GetLanguage(u)));
return ApplicationContext.Current.Services.TextService.Localize(key, GetCultureFromUserLanguage(GetLanguage(u)));

//return GetText(string.Empty, key, null, GetLanguage(u));
}
@@ -131,7 +121,7 @@ internal static string Text(string key, IUser u)
/// <returns></returns>
public static string Text(string Key)
{
return ApplicationContext.Current.Services.TextService.Localize(Key, CultureInfo.GetCultureInfo(GetLanguage()));
return ApplicationContext.Current.Services.TextService.Localize(Key, GetCultureFromUserLanguage(GetLanguage()));

//return GetText(Key);
}
@@ -146,8 +136,8 @@ public static string Text(string Key)
public static string Text(string Area, string Key, User u)
{
return ApplicationContext.Current.Services.TextService.Localize(
string.Format("{0}/{1}", Area, Key),
CultureInfo.GetCultureInfo(GetLanguage(u)));
string.Format("{0}/{1}", Area, Key),
GetCultureFromUserLanguage(GetLanguage(u)));

//return GetText(Area, Key, null, GetLanguage(u));
}
@@ -156,7 +146,7 @@ public static string Text(string area, string key, IUser u)
{
return ApplicationContext.Current.Services.TextService.Localize(
string.Format("{0}/{1}", area, key),
CultureInfo.GetCultureInfo(GetLanguage(u)));
GetCultureFromUserLanguage(GetLanguage(u)));

//return GetText(area, key, null, GetLanguage(u));
}
@@ -171,7 +161,7 @@ public static string Text(string Area, string Key)
{
return ApplicationContext.Current.Services.TextService.Localize(
string.Format("{0}/{1}", Area, Key),
CultureInfo.GetCultureInfo(GetLanguage()));
GetCultureFromUserLanguage(GetLanguage()));

//return GetText(Area, Key, GetLanguage());
}
@@ -188,7 +178,7 @@ public static string Text(string Area, string Key, string[] Variables, User u)
{
return ApplicationContext.Current.Services.TextService.Localize(
string.Format("{0}/{1}", Area, Key),
CultureInfo.GetCultureInfo(GetLanguage(u)),
GetCultureFromUserLanguage(GetLanguage(u)),
ConvertToObjectVars(Variables));

//return GetText(Area, Key, Variables, GetLanguage(u));
@@ -198,7 +188,7 @@ internal static string Text(string area, string key, string[] variables)
{
return ApplicationContext.Current.Services.TextService.Localize(
string.Format("{0}/{1}", area, key),
CultureInfo.GetCultureInfo(GetLanguage()),
GetCultureFromUserLanguage(GetLanguage()),
ConvertToObjectVars(variables));

//return GetText(area, key, variables, GetLanguage((IUser)null));
@@ -208,7 +198,7 @@ internal static string Text(string area, string key, string[] variables, IUser u
{
return ApplicationContext.Current.Services.TextService.Localize(
string.Format("{0}/{1}", area, key),
CultureInfo.GetCultureInfo(GetLanguage(u)),
GetCultureFromUserLanguage(GetLanguage(u)),
ConvertToObjectVars(variables));

//return GetText(area, key, variables, GetLanguage(u));
@@ -226,7 +216,7 @@ public static string Text(string Area, string Key, string Variable, User u)
{
return ApplicationContext.Current.Services.TextService.Localize(
string.Format("{0}/{1}", Area, Key),
CultureInfo.GetCultureInfo(GetLanguage(u)),
GetCultureFromUserLanguage(GetLanguage(u)),
ConvertToObjectVars(new[] { Variable }));

//return GetText(Area, Key, new[] { Variable }, GetLanguage(u));
@@ -236,7 +226,7 @@ internal static string Text(string area, string key, string variable)
{
return ApplicationContext.Current.Services.TextService.Localize(
string.Format("{0}/{1}", area, key),
CultureInfo.GetCultureInfo(GetLanguage()),
GetCultureFromUserLanguage(GetLanguage()),
ConvertToObjectVars(new[] { variable }));

//return GetText(area, key, new[] { variable }, GetLanguage((IUser)null));
@@ -246,7 +236,7 @@ internal static string Text(string area, string key, string variable, IUser u)
{
return ApplicationContext.Current.Services.TextService.Localize(
string.Format("{0}/{1}", area, key),
CultureInfo.GetCultureInfo(GetLanguage(u)),
GetCultureFromUserLanguage(GetLanguage(u)),
ConvertToObjectVars(new[] { variable }));

//return GetText(area, key, new[] { variable }, GetLanguage(u));
@@ -259,7 +249,7 @@ internal static string Text(string area, string key, string variable, IUser u)
/// <returns></returns>
public static string GetText(string key)
{
return ApplicationContext.Current.Services.TextService.Localize(key, CultureInfo.GetCultureInfo(GetLanguage()));
return ApplicationContext.Current.Services.TextService.Localize(key, GetCultureFromUserLanguage(GetLanguage()));

//return GetText(string.Empty, key, null, GetLanguage());
}
@@ -273,8 +263,8 @@ public static string GetText(string key)
public static string GetText(string area, string key)
{
return ApplicationContext.Current.Services.TextService.Localize(
string.Format("{0}/{1}", area, key),
CultureInfo.GetCultureInfo(GetLanguage()));
string.Format("{0}/{1}", area, key),
GetCultureFromUserLanguage(GetLanguage()));

//return GetText(area, key, null, GetLanguage());
}
@@ -290,7 +280,7 @@ public static string GetText(string area, string key, string[] variables)
{
return ApplicationContext.Current.Services.TextService.Localize(
string.Format("{0}/{1}", area, key),
CultureInfo.GetCultureInfo(GetLanguage()),
GetCultureFromUserLanguage(GetLanguage()),
ConvertToObjectVars(variables));

//return GetText(area, key, variables, GetLanguage());
@@ -307,7 +297,7 @@ public static string GetText(string area, string key, string variable)
{
return ApplicationContext.Current.Services.TextService.Localize(
string.Format("{0}/{1}", area, key),
CultureInfo.GetCultureInfo(GetLanguage()),
GetCultureFromUserLanguage(GetLanguage()),
ConvertToObjectVars(new[] { variable }));

//return GetText(area, key, new[] { variable }, GetLanguage());
@@ -327,7 +317,7 @@ public static string GetText(string area, string key, string[] variables, string

return ApplicationContext.Current.Services.TextService.Localize(
string.Format("{0}/{1}", area, key),
CultureInfo.GetCultureInfo(GetLanguage()),
GetCultureFromUserLanguage(GetLanguage()),
ConvertToObjectVars(variables));


@@ -429,5 +419,10 @@ public static XmlDocument getLanguageFile(string language)
.ToDictionary(keyvals => keyvals.index, keyvals => keyvals.value);
}

private static CultureInfo GetCultureFromUserLanguage(string userLang)
{
return CultureInfo.GetCultureInfo(userLang.Replace("_", "-"));
}

}
}

0 comments on commit db087a9

Please sign in to comment.
You can’t perform that action at this time.