forked from cabinetoffice/govuk-design-system-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes to allow localisation/overwriting strings #15
Open
jamiehumphries
wants to merge
10
commits into
master
Choose a base branch
from
localisation
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
2d8698f
feat: GovUkRadioCheckboxLabelTextAttribute.cs now uses Display attrib…
Glenn-Clarke 5a6fe65
feat: Allow option of localisation of Error title.
Glenn-Clarke 4d96a43
feat: All binder error texts (excluding Date type) can be overwritten…
Glenn-Clarke f94b453
feat: Allows for the license description and copyright strings to be …
Glenn-Clarke 75a079d
fix: Fix case when resource manager is null.
jamiehumphries 456b8e9
Merge branch 'master' into localisation
Glenn-Clarke 8e52df3
fix: Fixed argument ordering in DataBindingErrorTexts. Formatting of …
Glenn-Clarke 972ca98
docs: updated documentation in code to reflect new functionality wher…
Glenn-Clarke e5aa4f9
refactor: Separation of Localisable Data Binding error text attributes
Glenn-Clarke 668df7e
fix: localised error text attributes now inherit from their non-local…
Glenn-Clarke File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
20 changes: 19 additions & 1 deletion
20
GovUkDesignSystem/Attributes/DataBinding/GovUkDataBindingErrorTextAttribute.cs
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,12 +1,30 @@ | ||
using System; | ||
using System.Resources; | ||
using System.Globalization; | ||
|
||
namespace GovUkDesignSystem.Attributes.DataBinding | ||
{ | ||
/// <summary> | ||
/// Abstract base class for all attributes that hold data binding error text | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Property, Inherited = true)] | ||
[AttributeUsage(AttributeTargets.Property)] | ||
public abstract class GovUkDataBindingErrorTextAttribute : Attribute | ||
{ | ||
private ResourceManager _resourceManager; | ||
|
||
protected Type ResourceType { get; set; } | ||
|
||
protected string ResourceName { get; set; } | ||
|
||
protected string GetResourceValue(string resourceKey) | ||
{ | ||
var resourceAssembly = ResourceType?.Assembly; | ||
if (resourceAssembly != null && _resourceManager == null) | ||
{ | ||
_resourceManager = new ResourceManager(ResourceName, resourceAssembly); | ||
} | ||
|
||
return _resourceManager?.GetString(resourceKey, CultureInfo.CurrentCulture) ?? resourceKey; | ||
} | ||
} | ||
} |
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
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
43 changes: 43 additions & 0 deletions
43
.../DataBinding/Localisable/GovUkDataBindingMandatoryDecimalLocalisableErrorTextAttribute.cs
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,43 @@ | ||
using System; | ||
|
||
namespace GovUkDesignSystem.Attributes.DataBinding | ||
{ | ||
public class GovUkDataBindingMandatoryDecimalLocalisableErrorTextAttribute : GovUkDataBindingMandatoryDecimalErrorTextAttribute | ||
{ | ||
public GovUkDataBindingMandatoryDecimalLocalisableErrorTextAttribute(string errorMessageIfMissing, string nameAtStartOfSentence = "", string mustBeNumberErrorMessage = "", string resourceName = "", Type resourceType = null): base(errorMessageIfMissing, nameAtStartOfSentence) | ||
{ | ||
if(string.IsNullOrEmpty(nameAtStartOfSentence) && string.IsNullOrEmpty(mustBeNumberErrorMessage)) | ||
{ | ||
throw new ArgumentNullException("nameAtStartOfSentence cannot be null or empty unless all error messages are overwritten"); | ||
} | ||
|
||
if (resourceType == null ^ string.IsNullOrEmpty(resourceName)) | ||
{ | ||
throw new ArgumentNullException("resourceName or resourceType cannot be null or empty while the other is not null or empty"); | ||
} | ||
|
||
if(string.IsNullOrEmpty(errorMessageIfMissing)) | ||
{ | ||
throw new ArgumentNullException("errorMessageIfMissing cannot be null or empty"); | ||
} | ||
|
||
_mustBeNumberErrorMessage = mustBeNumberErrorMessage; | ||
ResourceType = resourceType; | ||
ResourceName = resourceName; | ||
} | ||
|
||
/// <summary> | ||
/// A complete sentence of the form: ‘Enter [whatever it is]’. | ||
/// <br/>For example, ‘Enter your first name’. | ||
/// </summary> | ||
public override string ErrorMessageIfMissing => GetResourceValue(base.ErrorMessageIfMissing); | ||
|
||
/// <summary> | ||
/// An override for the error message that is displayed if the value entered is not a number. | ||
/// A complete sentence of the form: ‘[Whatever it is] must be a number’ | ||
/// <br/>e.g. "Median age must be a number" | ||
/// </summary> | ||
private readonly string _mustBeNumberErrorMessage; | ||
public override string MustBeNumberErrorMessage => GetResourceValue(_mustBeNumberErrorMessage); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
...utes/DataBinding/Localisable/GovUkDataBindingMandatoryIntLocalisableErrorTextAttribute.cs
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,53 @@ | ||
using System; | ||
|
||
|
||
namespace GovUkDesignSystem.Attributes.DataBinding | ||
{ | ||
public class GovUkDataBindingMandatoryIntLocalisableErrorTextAttribute : GovUkDataBindingMandatoryIntErrorTextAttribute | ||
{ | ||
public GovUkDataBindingMandatoryIntLocalisableErrorTextAttribute(string errorMessageIfMissing, string nameAtStartOfSentence = "", Type resourceType = null, string resourceName = "", string isWholeNumberErrorMessage = "", string mustBeNumberErrorMessage = ""): base(errorMessageIfMissing, nameAtStartOfSentence) | ||
{ | ||
if(string.IsNullOrEmpty(nameAtStartOfSentence) && (string.IsNullOrEmpty(isWholeNumberErrorMessage) || string.IsNullOrEmpty(mustBeNumberErrorMessage))) | ||
{ | ||
throw new ArgumentNullException("nameAtStartOfSentence cannot be null or empty unless all error messages are overwritten"); | ||
} | ||
|
||
if (resourceType == null ^ string.IsNullOrEmpty(resourceName)) | ||
{ | ||
throw new ArgumentNullException("resourceName or resourceType cannot be null or empty while the other is not null or empty"); | ||
} | ||
|
||
if(string.IsNullOrEmpty(errorMessageIfMissing)) | ||
{ | ||
throw new ArgumentNullException("errorMessageIfMissing cannot be null or empty"); | ||
} | ||
|
||
_mustBeNumberErrorMessage = mustBeNumberErrorMessage; | ||
_isWholeNumberErrorMessage = isWholeNumberErrorMessage; | ||
ResourceType = resourceType; | ||
ResourceName = resourceName; | ||
} | ||
|
||
/// <summary> | ||
/// An override for the error message that is displayed if the value entered is not a whole number. | ||
/// A complete sentence of the form: ‘[Whatever it is] must be a whole number’ | ||
/// <br/>e.g. "Median age must be a whole number" | ||
/// </summary> | ||
private readonly string _isWholeNumberErrorMessage; | ||
public override string IsWholeNumberErrorMessage => GetResourceValue(_isWholeNumberErrorMessage); | ||
|
||
/// <summary> | ||
/// An override for the error message that is displayed if the value entered is not a number. | ||
/// A complete sentence of the form: ‘[Whatever it is] must be a number’ | ||
/// <br/>e.g. "Median age must be a number" | ||
/// </summary> | ||
private readonly string _mustBeNumberErrorMessage; | ||
public override string MustBeNumberErrorMessage => GetResourceValue(_mustBeNumberErrorMessage); | ||
|
||
/// <summary> | ||
/// A complete sentence of the form: ‘Enter [whatever it is]’. | ||
/// <br/>For example, ‘Enter your first name’. | ||
/// </summary> | ||
public override string ErrorMessageIfMissing => GetResourceValue(base.ErrorMessageIfMissing); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...s/DataBinding/Localisable/GovUkDataBindingOptionalDecimalLocalisableErrorTextAttribute.cs
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,31 @@ | ||
using System; | ||
|
||
namespace GovUkDesignSystem.Attributes.DataBinding | ||
{ | ||
public class GovUkDataBindingOptionalDecimalLocalisableErrorTextAttribute : GovUkDataBindingOptionalDecimalErrorTextAttribute | ||
{ | ||
public GovUkDataBindingOptionalDecimalLocalisableErrorTextAttribute(string nameAtStartOfSentence = "", string resourceName = "", string mustBeNumberErrorMessage = "", Type resourceType = null): base(nameAtStartOfSentence) | ||
{ | ||
if (resourceType == null ^ string.IsNullOrEmpty(resourceName)) | ||
{ | ||
throw new ArgumentNullException("resourceName or resourceType cannot be null or empty while the other is not null or empty"); | ||
} | ||
|
||
if (string.IsNullOrEmpty(nameAtStartOfSentence)) | ||
{ | ||
throw new ArgumentNullException("nameAtStartOfSentence cannot be null or empty"); | ||
} | ||
_mustBeNumberErrorMessage = mustBeNumberErrorMessage; | ||
ResourceType = resourceType; | ||
ResourceName = resourceName; | ||
} | ||
|
||
/// <summary> | ||
/// An override for the error message that is displayed if the value entered is not a number. | ||
/// A complete sentence of the form: ‘[Whatever it is] must be a number’ | ||
/// <br/>e.g. "Median age must be a number" | ||
/// </summary> | ||
private readonly string _mustBeNumberErrorMessage; | ||
public override string MustBeNumberErrorMessage => GetResourceValue(_mustBeNumberErrorMessage); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
...butes/DataBinding/Localisable/GovUkDataBindingOptionalIntLocalisableErrorTextAttribute.cs
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,42 @@ | ||
using System; | ||
|
||
namespace GovUkDesignSystem.Attributes.DataBinding | ||
{ | ||
public class GovUkDataBindingOptionalIntLocalisableErrorTextAttribute : GovUkDataBindingOptionalIntErrorTextAttribute | ||
{ | ||
public GovUkDataBindingOptionalIntLocalisableErrorTextAttribute(string nameAtStartOfSentence = "", string mustBeNumberErrorMessage = "", string resourceName = "", string isWholeNumberErrorMessage = "", Type resourceType = null): base(nameAtStartOfSentence) | ||
{ | ||
|
||
if(string.IsNullOrEmpty(nameAtStartOfSentence) && (string.IsNullOrEmpty(isWholeNumberErrorMessage) || string.IsNullOrEmpty(mustBeNumberErrorMessage))) | ||
{ | ||
throw new ArgumentNullException("nameAtStartOfSentence cannot be null or empty unless all error messages are overwritten"); | ||
} | ||
|
||
if (resourceType == null ^ string.IsNullOrEmpty(resourceName)) | ||
{ | ||
throw new ArgumentNullException("resourceName or resourceType cannot be null or empty while the other is not null or empty"); | ||
} | ||
|
||
_mustBeNumberErrorMessage = mustBeNumberErrorMessage; | ||
_isWholeNumberErrorMessage = isWholeNumberErrorMessage; | ||
ResourceType = resourceType; | ||
ResourceName = resourceName; | ||
} | ||
|
||
/// <summary> | ||
/// An override for the error message that is displayed if the value entered is not a whole number. | ||
/// A complete sentence of the form: ‘[Whatever it is] must be a whole number’ | ||
/// <br/>e.g. "Median age must be a whole number" | ||
/// </summary> | ||
private readonly string _isWholeNumberErrorMessage; | ||
public override string IsWholeNumberErrorMessage => GetResourceValue(_isWholeNumberErrorMessage); | ||
|
||
/// <summary> | ||
/// An override for the error message that is displayed if the value entered is not a number. | ||
/// A complete sentence of the form: ‘[Whatever it is] must be a number’ | ||
/// <br/>e.g. "Median age must be a number" | ||
/// </summary> | ||
private readonly string _mustBeNumberErrorMessage; | ||
public override string MustBeNumberErrorMessage => GetResourceValue(_mustBeNumberErrorMessage); | ||
} | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not keen on adding these properties to these classes that will never use them, but I don't think it's worth the effort to change it at the moment.
I'm not sure exactly what I'd do differently to simplify things, but perhaps the model binder could look for two different attribute types. I might even go further and create two different model binders (one localisable) with a shared base.