Skip to content

Commit d52a7a1

Browse files
committed
Ensured localization consistency with IFormatProvider
Where appropriate, implemented the `IFormatProvider` overload to ensure consistent behavior regardless of the browser's locale settings. This resolves CA1305.
1 parent 83bf4f3 commit d52a7a1

7 files changed

Lines changed: 20 additions & 12 deletions

File tree

OnTopic.Editor.AspNetCore/Components/LastModifiedViewComponent.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
| Project Topics Library
55
\=============================================================================================================================*/
66
using System;
7+
using System.Globalization;
78
using Microsoft.AspNetCore.Mvc;
89
using OnTopic.Editor.Models;
910
using OnTopic.Editor.Models.Components.ViewModels;
@@ -61,12 +62,12 @@ string htmlFieldPrefix
6162
| Set model values
6263
\-----------------------------------------------------------------------------------------------------------------------*/
6364
if (currentTopic.LastModified != DateTime.MinValue) {
64-
model.CurrentValue = currentTopic.LastModified.ToString();
65+
model.CurrentValue = currentTopic.LastModified.ToString(CultureInfo.InvariantCulture);
6566
}
6667
else {
6768
model.CurrentValue = model.Value;
6869
}
69-
model.Value = DateTime.Now.ToString();
70+
model.Value = DateTime.Now.ToString(CultureInfo.InvariantCulture);
7071

7172
/*------------------------------------------------------------------------------------------------------------------------
7273
| Return view with view model

OnTopic.Editor.AspNetCore/Components/TokenizedTopicListViewComponent.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
| Project Topics Library
55
\=============================================================================================================================*/
66
using System;
7+
using System.Globalization;
78
using Microsoft.AspNetCore.Mvc;
89
using OnTopic.Editor.Models;
910
using OnTopic.Editor.Models.Components.ViewModels;
@@ -125,7 +126,7 @@ private string GetTopicJson(string topicId) {
125126
| Set initial variables
126127
\---------------------------------------------------------------------------------------------------------------------------*/
127128
var topicJson = "";
128-
var topic = _topicRepository.Load(Int32.Parse(topicId));
129+
var topic = _topicRepository.Load(Int32.Parse(topicId, CultureInfo.InvariantCulture));
129130

130131
/*----------------------------------------------------------------------------------------------------------------------------
131132
| Write out JSON for existing topic, if available

OnTopic.Editor.AspNetCore/Components/TopicListViewComponent.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
\=============================================================================================================================*/
66
using System;
77
using System.Collections.Generic;
8+
using System.Globalization;
89
using System.Linq;
910
using Microsoft.AspNetCore.Mvc;
1011
using Microsoft.AspNetCore.Mvc.Rendering;
@@ -228,7 +229,7 @@ public static List<QueryResultTopicViewModel> GetTopics(
228229
private static string ReplaceTokens(QueryResultTopicViewModel topic, string source) {
229230
if (topic is not null && !String.IsNullOrEmpty(source)) {
230231
source = source
231-
.Replace("{TopicId}", topic.Id.ToString(), StringComparison.InvariantCultureIgnoreCase)
232+
.Replace("{TopicId}", topic.Id.ToString(CultureInfo.InvariantCulture), StringComparison.InvariantCultureIgnoreCase)
232233
.Replace("{Key}", topic.Key, StringComparison.InvariantCultureIgnoreCase)
233234
.Replace("{UniqueKey}", topic.UniqueKey, StringComparison.InvariantCultureIgnoreCase)
234235
.Replace("{WebPath}", topic.WebPath, StringComparison.InvariantCultureIgnoreCase)

OnTopic.Editor.AspNetCore/Controllers/EditorController.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
| Project Topics Library
55
\=============================================================================================================================*/
66
using System;
7+
using System.Globalization;
78
using System.IO;
89
using System.Linq;
910
using System.Text;
@@ -257,7 +258,7 @@ public async Task<IActionResult> Edit(
257258
var parentTopic = isNew? CurrentTopic : CurrentTopic.Parent;
258259
var contentTypeDescriptor = GetContentType(contentType?? CurrentTopic.ContentType);
259260
var derivedTopicValue = model.Attributes.Contains("TopicID")? model.Attributes["TopicID"].Value : "-1";
260-
var derivedTopicId = String.IsNullOrWhiteSpace(derivedTopicValue)? -1 : Int32.Parse(derivedTopicValue);
261+
var derivedTopicId = String.IsNullOrWhiteSpace(derivedTopicValue)? -1 : Int32.Parse(derivedTopicValue, CultureInfo.InvariantCulture);
261262
var derivedTopic = (derivedTopicId >= 0)? TopicRepository.Load(derivedTopicId) : null;
262263
var newKey = model.Attributes.Contains("Key")? model.Attributes["Key"].Value : null;
263264

OnTopic.Editor.AspNetCore/Infrastructure/TopicQueryService.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,13 @@ public static bool IsValidTopic(Topic topic, TopicQueryOptions options) {
152152
| Validate filtered attribute
153153
\-----------------------------------------------------------------------------------------------------------------------*/
154154
if (!String.IsNullOrEmpty(options.AttributeName) && !String.IsNullOrEmpty(options.AttributeName)) {
155+
var attributeValue = topic.Attributes.GetValue(options.AttributeName, "");
155156
if (options.UsePartialMatch) {
156-
if (topic.Attributes.GetValue(options.AttributeName, "").IndexOf(options.AttributeValue) is -1) {
157+
if (attributeValue.IndexOf(options.AttributeValue, StringComparison.Ordinal) is -1) {
157158
return false;
158159
}
159160
}
160-
if (!topic.Attributes.GetValue(options.AttributeName, "").Equals(options.AttributeValue)) {
161+
if (!attributeValue.Equals(options.AttributeValue)) {
161162
return false;
162163
}
163164
}

OnTopic.Editor.Models/Components/ViewModels/DateTimeAttributeViewModel.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
| Project Topics Library
55
\=============================================================================================================================*/
66
using System;
7+
using System.Globalization;
78
using OnTopic.Editor.Models.Metadata;
89

910
namespace OnTopic.Editor.Models.Components.ViewModels {
@@ -23,6 +24,7 @@ public class DateTimeAttributeViewModel: AttributeViewModel<DateTimeAttributeTop
2324
\-------------------------------------------------------------------------------------------------------------------------*/
2425
private string _defaultDate;
2526
private string _defaultTime;
27+
private readonly IFormatProvider _format = CultureInfo.InvariantCulture;
2628

2729
/*==========================================================================================================================
2830
| CONSTRUCTOR
@@ -54,11 +56,11 @@ public string GetDefaultDate() {
5456
var dateFormat = AttributeDescriptor.DateFormat.Replace("y", "yy").Replace("mm", "MM");
5557
if (!String.IsNullOrEmpty(Value)) {
5658
if (DateTime.TryParse(Value, out var dateValue)) {
57-
_defaultDate = dateValue.ToString(dateFormat);
59+
_defaultDate = dateValue.ToString(dateFormat, _format);
5860
}
5961
}
6062
else {
61-
_defaultDate = CalculateOffset(DateTime.Now).ToString(dateFormat);
63+
_defaultDate = CalculateOffset(DateTime.Now).ToString(dateFormat, _format);
6264
}
6365
}
6466
return _defaultDate;
@@ -74,11 +76,11 @@ public string GetDefaultTime() {
7476
if (String.IsNullOrEmpty(_defaultTime)) {
7577
if (!String.IsNullOrEmpty(Value)) {
7678
if (DateTime.TryParse(Value, out var timeValue)) {
77-
_defaultTime = timeValue.ToString(AttributeDescriptor.TimeFormat);
79+
_defaultTime = timeValue.ToString(AttributeDescriptor.TimeFormat, _format);
7880
}
7981
}
8082
else {
81-
_defaultTime = CalculateOffset(DateTime.Now).ToString(AttributeDescriptor.TimeFormat);
83+
_defaultTime = CalculateOffset(DateTime.Now).ToString(AttributeDescriptor.TimeFormat, _format);
8284
}
8385
}
8486
return _defaultTime;

OnTopic.Editor.Models/Metadata/AttributeDescriptorTopicViewModel.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
\=============================================================================================================================*/
66
using System;
77
using System.Collections.Generic;
8+
using System.Globalization;
89
using System.Linq;
910
using OnTopic.Metadata;
1011
using OnTopic.Metadata.AttributeTypes;
@@ -153,7 +154,7 @@ public bool GetBooleanConfigurationValue(string key, bool defaultValue = false)
153154
"AttributeDescriptor."
154155
)]
155156
public int GetIntegerConfigurationValue(string key, int defaultValue = 0) {
156-
if (Int32.TryParse(GetConfigurationValue(key, defaultValue.ToString()), out var value)) {
157+
if (Int32.TryParse(GetConfigurationValue(key, defaultValue.ToString(CultureInfo.InvariantCulture)), out var value)) {
157158
return value;
158159
}
159160
return defaultValue;

0 commit comments

Comments
 (0)