Remove implicit conversion from string to LocalizedString

In most cases, relying on the implicit conversion is a sign
of a missing call to "T".

--HG--
branch : dev
This commit is contained in:
Renaud Paquay 2010-06-12 13:33:35 -07:00
parent ff5448ac31
commit ad54ffdbb2
21 changed files with 58 additions and 44 deletions

View File

@ -3,6 +3,7 @@ using System.Web.Mvc;
using System.Web.Routing;
using NUnit.Framework;
using Orchard.ContentManagement;
using Orchard.Localization;
using Orchard.Security;
using Orchard.Security.Permissions;
using Orchard.Tests.Stubs;
@ -71,9 +72,10 @@ namespace Orchard.Tests.UI.Navigation {
public string MenuName { get { return "admin"; } }
public void GetNavigation(NavigationBuilder builder) {
var T = NullLocalizer.Instance;
builder
.Add("Foo", "1.0", x => x.Action("foo"))
.Add("Bar", "2.0", x => x.Add("Frap", "1.b"));
.Add(T("Foo"), "1.0", x => x.Action("foo"))
.Add(T("Bar"), "2.0", x => x.Add(T("Frap"), "1.b"));
}
}
@ -81,9 +83,10 @@ namespace Orchard.Tests.UI.Navigation {
public string MenuName { get { return "admin"; } }
public void GetNavigation(NavigationBuilder builder) {
var T = NullLocalizer.Instance;
builder
.Add("Frap", "3.0", x => x.Action("foo"))
.Add("Bar", "4.0", x => x.Add("Quad", "1.a"));
.Add(T("Frap"), "3.0", x => x.Action("foo"))
.Add(T("Bar"), "4.0", x => x.Add(T("Quad"), "1.a"));
}
}
}

View File

@ -9,12 +9,12 @@ namespace Orchard.Tests.UI.Notify {
[Test]
public void MessageServiceCanAccumulateWarningsAndErrorsToReturn() {
INotifier notifier = new Notifier();
notifier.Warning("Hello world");
notifier.Information("More Info");
notifier.Error("Boom");
Localizer T = NullLocalizer.Instance;
notifier.Warning(T("Hello world"));
notifier.Information(T("More Info"));
notifier.Error(T("Boom"));
Assert.That(notifier.List(), Has.Count.EqualTo(3));
Assert.That(notifier.List(), Has.Some.Property("Message").EqualTo(T("Hello world")));
Assert.That(notifier.List(), Has.Some.Property("Message").EqualTo(T("More Info")));

View File

@ -23,7 +23,8 @@ namespace Orchard.Tests.UI.Notify {
public void AfterActionExecutedMessagesShouldAppearInTempData() {
var sink = new Notifier();
var filter = new NotifyFilter(sink);
sink.Information("Hello world");
var T = NullLocalizer.Instance;
sink.Information(T("Hello world"));
var executedContext = BuildContext();
filter.OnActionExecuted(executedContext);
@ -47,7 +48,8 @@ namespace Orchard.Tests.UI.Notify {
public void NewMessagesAreConcatinated() {
var sink = new Notifier();
var filter = new NotifyFilter(sink);
sink.Error("Boom");
var T = NullLocalizer.Instance;
sink.Error(T("Boom"));
var executedContext = BuildContext();
executedContext.Controller.TempData.Add("messages", "dont-destroy");
@ -60,7 +62,8 @@ namespace Orchard.Tests.UI.Notify {
public void TempDataBuildsMessagesWhenResultExecutingIsBaseViewModel() {
var sink = new Notifier();
var filter = new NotifyFilter(sink);
sink.Information("Working");
var T = NullLocalizer.Instance;
sink.Information(T("Working"));
var model = new BaseViewModel();
@ -74,8 +77,6 @@ namespace Orchard.Tests.UI.Notify {
filter.OnActionExecuted(context);
filter.OnResultExecuting(new ResultExecutingContext(context, context.Result));
var T = NullLocalizer.Instance;
Assert.That(model.Messages, Is.Not.Null);
Assert.That(model.Messages, Has.Count.EqualTo(2));
Assert.That(model.Messages, Has.Some.Property("Message").EqualTo(T("dont-destroy")));

View File

@ -38,7 +38,7 @@ namespace Orchard.Core.Common.Drivers {
updater.TryUpdateModel(model, Prefix, null, null);
if (!_routableService.IsSlugValid(part.Slug)){
updater.AddModelError("Routable.Slug", T("Please do not use any of the following characters in your slugs: \"/\", \":\", \"?\", \"#\", \"[\", \"]\", \"@\", \"!\", \"$\", \"&\", \"'\", \"(\", \")\", \"*\", \"+\", \",\", \";\", \"=\". No spaces are allowed (please use dashes or underscores instead).").ToString());
updater.AddModelError("Routable.Slug", T("Please do not use any of the following characters in your slugs: \"/\", \":\", \"?\", \"#\", \"[\", \"]\", \"@\", \"!\", \"$\", \"&\", \"'\", \"(\", \")\", \"*\", \"+\", \",\", \";\", \"=\". No spaces are allowed (please use dashes or underscores instead)."));
}
string originalSlug = part.Slug;

View File

@ -2,6 +2,7 @@
using JetBrains.Annotations;
using Orchard.ContentManagement;
using Orchard.Core.Navigation.Models;
using Orchard.Localization;
using Orchard.UI.Navigation;
using MenuItem=Orchard.Core.Navigation.Models.MenuItem;
@ -36,11 +37,11 @@ namespace Orchard.Core.Navigation.Services {
if (part.Is<MenuItem>())
builder.Add(
menu => menu.Add(part.MenuText, part.MenuPosition, nib => nib.Url(part.As<MenuItem>().Url)));
menu => menu.Add(new LocalizedString(part.MenuText), part.MenuPosition, nib => nib.Url(part.As<MenuItem>().Url)));
else
builder.Add(
menu =>
menu.Add(part.MenuText, part.MenuPosition,
menu.Add(new LocalizedString(part.MenuText), part.MenuPosition,
nib =>
nib.Action(_contentManager.GetItemMetadata(part.ContentItem).DisplayRouteValues)));
}

View File

@ -83,7 +83,7 @@ namespace Orchard.Core.Routable.Drivers {
}
if (!_routableService.IsSlugValid(part.Slug)) {
updater.AddModelError("Routable.Slug", T("Please do not use any of the following characters in your slugs: \"/\", \":\", \"?\", \"#\", \"[\", \"]\", \"@\", \"!\", \"$\", \"&\", \"'\", \"(\", \")\", \"*\", \"+\", \",\", \";\", \"=\". No spaces are allowed (please use dashes or underscores instead).").ToString());
updater.AddModelError("Routable.Slug", T("Please do not use any of the following characters in your slugs: \"/\", \":\", \"?\", \"#\", \"[\", \"]\", \"@\", \"!\", \"$\", \"&\", \"'\", \"(\", \")\", \"*\", \"+\", \",\", \";\", \"=\". No spaces are allowed (please use dashes or underscores instead)."));
}
string originalSlug = part.Slug;

View File

@ -27,7 +27,7 @@
<li><%
if (Model.Item.ScheduledPublishUtc.HasValue && Model.Item.ScheduledPublishUtc.Value > DateTime.UtcNow) { %>
<img class="icon" src="<%=ResolveUrl("~/Modules/Orchard.Blogs/Content/Admin/images/scheduled.gif") %>" alt="<%: T("Scheduled") %>" title="<%: T("The post is scheduled for publishing") %>" /><%: T("Scheduled")%>
<%=Html.DateTime(Model.Item.ScheduledPublishUtc.Value, "M/d/yyyy h:mm tt")%><%
<%=Html.DateTime(Model.Item.ScheduledPublishUtc.Value, T("M/d/yyyy h:mm tt"))%><%
}
else if (Model.Item.IsPublished) { %>
<%: T("Published: {0}", Html.DateTimeRelative(Model.Item.As<ICommonAspect>().VersionPublishedUtc.Value, T)) %><%

View File

@ -48,7 +48,7 @@ namespace Orchard.Comments.Controllers {
if(!TryUpdateModel(viewModel)) {
if (Request.Form["Name"].IsNullOrEmptyTrimmed()) {
_notifier.Error("You must provide a Name in order to comment");
_notifier.Error(T("You must provide a Name in order to comment"));
}
return Redirect(returnUrl);
}

View File

@ -22,7 +22,7 @@ namespace Orchard.DevTools.Controllers {
}
public ActionResult NotAuthorized() {
_notifier.Warning("Simulated error goes here.");
_notifier.Warning(T("Simulated error goes here."));
return new HttpUnauthorizedResult();
}

View File

@ -42,7 +42,7 @@ namespace Orchard.Media.Controllers {
return RedirectToAction("Index");
}
catch (Exception exception) {
Services.Notifier.Error("Deleting Folder failed: " + exception.Message);
Services.Notifier.Error(T("Deleting Folder failed: {0}", exception.Message));
return View();
}
}
@ -62,7 +62,7 @@ namespace Orchard.Media.Controllers {
return RedirectToAction("Index");
}
catch (Exception exception) {
Services.Notifier.Error("Creating Folder failed: " + exception.Message);
Services.Notifier.Error(T("Creating Folder failed: {0}", exception.Message));
return View(viewModel);
}
}
@ -96,7 +96,7 @@ namespace Orchard.Media.Controllers {
return RedirectToAction("Index");
}
catch (Exception exception) {
Services.Notifier.Error("Deleting failed: " + exception.Message);
Services.Notifier.Error(T("Deleting failed: {0}", exception.Message));
return View();
}
}
@ -128,7 +128,7 @@ namespace Orchard.Media.Controllers {
return RedirectToAction("Index");
}
catch (Exception exception) {
Services.Notifier.Error("Modifying Folder Properties failed: " + exception.Message);
Services.Notifier.Error(T("Modifying Folder Properties failed: {0}", exception.Message));
return View(viewModel);
}
}
@ -161,7 +161,7 @@ namespace Orchard.Media.Controllers {
return RedirectToAction("Edit", new { name = viewModel.FolderName, mediaPath = viewModel.MediaPath });
}
catch (Exception exception) {
Services.Notifier.Error("Uploading media file failed: " + exception.Message);
Services.Notifier.Error(T("Uploading media file failed: {0}", exception.Message));
return View(viewModel);
}
}
@ -240,7 +240,7 @@ namespace Orchard.Media.Controllers {
mediaPath = viewModel.MediaPath });
}
catch (Exception exception) {
Services.Notifier.Error("Editing media file failed: " + exception.Message);
Services.Notifier.Error(T("Editing media file failed: {0}", exception.Message));
return View(viewModel);
}
}

View File

@ -1,4 +1,5 @@
<%@ Control Language="C#" Inherits="Orchard.Mvc.ViewUserControl<FeaturesViewModel>" %>
<%@ Import Namespace="Orchard.Localization" %>
<%@ Import Namespace="Orchard.Modules.Extensions" %>
<%@ Import Namespace="Orchard.Mvc.Html"%>
<%@ Import Namespace="Orchard.Modules.ViewModels"%>
@ -11,7 +12,7 @@
<ul class="features summary-view switchable"><%
var featureGroups = Model.Features.OrderBy(f => f.Descriptor.Category).GroupBy(f => f.Descriptor.Category);
foreach (var featureGroup in featureGroups) {
var categoryName = featureGroup.First().Descriptor.Category ?? T("Uncategorized");
var categoryName = LocalizedString.TextOrDefault(featureGroup.First().Descriptor.Category, T("Uncategorized"));
var categoryClassName = string.Format("category {0}", Html.Encode(categoryName.ToString().HtmlClassify()));
if (featureGroup == featureGroups.First())
categoryClassName += " first";

View File

@ -55,7 +55,7 @@ namespace Orchard.MultiTenancy.Controllers {
return RedirectToAction("Index");
}
catch (Exception exception) {
Services.Notifier.Error(T("Creating Tenant failed: ") + exception.Message);
Services.Notifier.Error(T("Creating Tenant failed: {0}", exception.Message));
return View(viewModel);
}
}
@ -103,7 +103,7 @@ namespace Orchard.MultiTenancy.Controllers {
return RedirectToAction("Index");
}
catch (Exception exception) {
Services.Notifier.Error(T("Failed to edit tenant: ") + exception.Message);
Services.Notifier.Error(T("Failed to edit tenant: {0} ", exception.Message));
return View(viewModel);
}
}

View File

@ -62,7 +62,7 @@ using (Html.BeginFormAntiForgeryPost()) { %>
<li><%
if (pageEntry.Page.ScheduledPublishUtc.HasValue && pageEntry.Page.ScheduledPublishUtc.Value > DateTime.UtcNow) { %>
<img class="icon" src="<%=ResolveUrl("~/Modules/Orchard.Pages/Content/Admin/images/scheduled.gif") %>" alt="<%: T("Scheduled") %>" title="<%: T("The page is scheduled for publishing") %>" /><%: T("Scheduled")%>
<%=Html.DateTime(pageEntry.Page.ScheduledPublishUtc.Value, "M/d/yyyy h:mm tt")%><%
<%=Html.DateTime(pageEntry.Page.ScheduledPublishUtc.Value, T("M/d/yyyy h:mm tt"))%><%
}
else if (pageEntry.Page.IsPublished) { %>
<%: T("Published: {0}", Html.DateTimeRelative(pageEntry.Page.As<ICommonAspect>().VersionPublishedUtc.Value, T)) %><%

View File

@ -25,6 +25,7 @@ namespace Orchard.Roles.Controllers {
Services = services;
_roleService = roleService;
_authorizationService = authorizationService;
T = NullLocalizer.Instance;
}
public IOrchardServices Services { get; set; }
@ -55,7 +56,7 @@ namespace Orchard.Roles.Controllers {
return RedirectToAction("Index");
}
catch (Exception exception) {
Services.Notifier.Error("Deleting Role failed: " + exception.Message);
Services.Notifier.Error(T("Deleting Role failed: {0}", exception.Message));
return View();
}
}
@ -87,7 +88,7 @@ namespace Orchard.Roles.Controllers {
return RedirectToAction("Index");
}
catch (Exception exception) {
Services.Notifier.Error("Creating Role failed: " + exception.Message);
Services.Notifier.Error(T("Creating Role failed: {0}", exception.Message));
return RedirectToAction("Create");
}
}
@ -141,7 +142,7 @@ namespace Orchard.Roles.Controllers {
return RedirectToAction("Edit", new { viewModel.Id });
}
catch (Exception exception) {
Services.Notifier.Error("Editing Role failed: " + exception.Message);
Services.Notifier.Error(T("Editing Role failed: {0}", exception.Message));
return RedirectToAction("Edit", viewModel.Id);
}
}

View File

@ -3,6 +3,7 @@ using JetBrains.Annotations;
using Orchard.ContentManagement.Drivers;
using Orchard.Data;
using Orchard.ContentManagement;
using Orchard.Localization;
using Orchard.Roles.Models;
using Orchard.Roles.Services;
using Orchard.Roles.ViewModels;
@ -29,6 +30,7 @@ namespace Orchard.Roles.Drivers {
_notifier = notifier;
_authenticationService = authenticationService;
_authorizationService = authorizationService;
T = NullLocalizer.Instance;
}
protected override string Prefix {
@ -37,6 +39,8 @@ namespace Orchard.Roles.Drivers {
}
}
public Localizer T { get; set; }
protected override DriverResult Editor(UserRoles userRoles) {
// don't show editor without apply roles permission
if (!_authorizationService.TryCheckAccess(Permissions.ApplyRoles, _authenticationService.GetAuthenticatedUser(), userRoles))
@ -75,12 +79,12 @@ namespace Orchard.Roles.Drivers {
var targetRoleRecords = model.Roles.Where(x => x.Granted).Select(x => _roleService.GetRole(x.RoleId));
foreach (var addingRole in targetRoleRecords.Where(x => !currentRoleRecords.Contains(x))) {
_notifier.Warning(string.Format("Adding role {0} to user {1}", addingRole.Name, userRoles.As<IUser>().UserName));
_notifier.Warning(T("Adding role {0} to user {1}", addingRole.Name, userRoles.As<IUser>().UserName));
_userRolesRepository.Create(new UserRolesRecord { UserId = model.User.Id, Role = addingRole });
}
foreach (var removingRole in currentUserRoleRecords.Where(x => !targetRoleRecords.Contains(x.Role))) {
_notifier.Warning(string.Format("Removing role {0} from user {1}", removingRole.Role.Name, userRoles.As<IUser>().UserName));
_notifier.Warning(T("Removing role {0} from user {1}", removingRole.Role.Name, userRoles.As<IUser>().UserName));
_userRolesRepository.Delete(removingRole);
}

View File

@ -84,7 +84,7 @@ namespace Orchard.Setup.Controllers {
catch (Exception exception) {
_notifier.Error(T("Setup failed:"));
for (var scan = exception; scan != null; scan = scan.InnerException) {
_notifier.Error(scan.Message);
_notifier.Error(new LocalizedString(scan.Message));
}
return IndexViewResult(model);
}

View File

@ -11,7 +11,7 @@ namespace Orchard.Themes.Services {
public void ZoneRendering(ZoneRenderContext context) {
#if DEBUG
context.Html.ViewContext.Writer.WriteLine(T("<!-- begin zone: {0} -->", context.ZoneName ?? T("etc. (ZonesAny)")));
context.Html.ViewContext.Writer.WriteLine(T("<!-- begin zone: {0} -->", LocalizedString.TextOrDefault(context.ZoneName, T("etc. (ZonesAny)"))));
#endif
}
@ -43,7 +43,7 @@ namespace Orchard.Themes.Services {
public void ZoneRendered(ZoneRenderContext context) {
#if DEBUG
context.Html.ViewContext.Writer.WriteLine(T("<!-- end zone: {0} -->", context.ZoneName ?? T("etc. (ZonesAny)")));
context.Html.ViewContext.Writer.WriteLine(T("<!-- end zone: {0} -->", LocalizedString.TextOrDefault(context.ZoneName, T("etc. (ZonesAny)"))));
#endif
}
}

View File

@ -9,8 +9,11 @@ namespace Orchard.Localization {
_localized = localized;
}
public static implicit operator LocalizedString(string x) {
return new LocalizedString(x);
public static LocalizedString TextOrDefault(string text, LocalizedString defaultValue) {
if (string.IsNullOrEmpty(text))
return defaultValue;
else
return new LocalizedString(text);
}
public string Text {

View File

@ -2,7 +2,7 @@ namespace Orchard.Localization {
public static class NullLocalizer {
static NullLocalizer () {
_instance = (format, args) => (args == null || args.Length == 0) ? format : string.Format(format, args);
_instance = (format, args) => new LocalizedString((args == null || args.Length == 0) ? format : string.Format(format, args));
}
static readonly Localizer _instance;

View File

@ -27,8 +27,8 @@ namespace Orchard.Localization {
var localizedFormat = _resourceManager.GetLocalizedString(_scope, textHint, currentCulture);
return args.Length == 0
? new LocalizedString(localizedFormat)
: string.Format(GetFormatProvider(currentCulture), localizedFormat, args.Select(Encode).ToArray());
? new LocalizedString(localizedFormat)
: new LocalizedString(string.Format(GetFormatProvider(currentCulture), localizedFormat, args.Select(Encode).ToArray()));
}
private static IFormatProvider GetFormatProvider(string currentCulture) {

View File

@ -216,7 +216,7 @@ namespace Orchard.Mvc.Html {
public static LocalizedString DateTime(this HtmlHelper htmlHelper, DateTime value, LocalizedString customFormat) {
//TODO: (erikpo) In the future, convert this to "local" time before calling ToString
return value.ToString(customFormat.Text);
return new LocalizedString(value.ToString(customFormat.Text));
}
#endregion