Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
df1bf63
commit e737aa0
Showing
4 changed files
with
65 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
using System; | ||
using System.Globalization; | ||
using System.Linq; | ||
using System.Web; | ||
using Orchard.Localization.Services; | ||
using Orchard.Logging; | ||
|
||
namespace Orchard.Localization { | ||
public class Text : IText { | ||
private readonly string _scope; | ||
private readonly IWorkContextAccessor _workContextAccessor; | ||
private readonly ILocalizedStringManager _localizedStringManager; | ||
|
||
public Text(string scope, IWorkContextAccessor workContextAccessor, ILocalizedStringManager localizedStringManager) { | ||
_scope = scope; | ||
_workContextAccessor = workContextAccessor; | ||
_localizedStringManager = localizedStringManager; | ||
Logger = NullLogger.Instance; | ||
} | ||
|
||
public ILogger Logger { get; set; } | ||
|
||
public LocalizedString Get(string textHint, params object[] args) { | ||
Logger.Debug("{0} localizing '{1}'", _scope, textHint); | ||
|
||
var workContext = _workContextAccessor.GetContext(); | ||
|
||
if (workContext != null) { | ||
var currentCulture = workContext.CurrentCulture; | ||
var localizedFormat = _localizedStringManager.GetLocalizedString(_scope, textHint, currentCulture); | ||
|
||
return args.Length == 0 | ||
? new LocalizedString(localizedFormat, _scope, textHint, args) | ||
: new LocalizedString( | ||
String.Format(GetFormatProvider(currentCulture), localizedFormat, args.Select(Encode).ToArray()), | ||
_scope, | ||
textHint, | ||
args); | ||
} | ||
|
||
return new LocalizedString(textHint, _scope, textHint, args); | ||
} | ||
|
||
private static IFormatProvider GetFormatProvider(string currentCulture) { | ||
try { | ||
return CultureInfo.GetCultureInfoByIetfLanguageTag(currentCulture); | ||
} | ||
catch { | ||
return null; | ||
} | ||
} | ||
|
||
static object Encode(object arg) | ||
{ | ||
if (arg is IFormattable || arg is IHtmlString) { | ||
return arg; | ||
} | ||
|
||
return HttpUtility.HtmlEncode(arg); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
<h1 id="page-title">@Model.Title.ToString()</h1> | ||
<h1 id="page-title">@Model.Title</h1> |