Created
January 25, 2017 11:16
-
-
Save sturlath/b695da5222f87beb2b0834eca92c8ac7 to your computer and use it in GitHub Desktop.
Get values from resx files
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
/// <summary> | |
/// Just a simple wraper for getting values from resx file with just basic error handling | |
/// </summary> | |
/// <example> | |
/// Usage is just like = ErrorStrings.Current.NotAValidEmail; | |
/// </example> | |
public class ErrorStrings | |
{ | |
public string NotAValidEmail | |
{ | |
get | |
{ | |
return GetValue("NotAValidEmail"); | |
} | |
} | |
public string NotAValidPhoneNumber | |
{ | |
get | |
{ | |
return GetValue("NotAValidPhoneNumber"); | |
} | |
} | |
private string GetValue(string text) | |
{ | |
var translation = _resmanager.GetString(text, _culture); | |
if (translation == null) | |
{ | |
#if DEBUG | |
throw new ArgumentException($"Key '{text}' was not found in resources '{ResourceId}' for culture '{_culture.Name}'.", $"Text"); | |
#else | |
translation = Text; // HACK: returns the key, which GETS DISPLAYED TO THE USER | |
#endif | |
} | |
return translation; | |
} | |
#region Setup | |
private const string ResourceId = "My.Full.AsemblyName.ErrorStrings"; //Base name of resx file ("ErrorStrings.is-IS.resx","ErrorStrings.en-US.resx" etc.) | |
private static ResourceManager _resmanager; | |
private static ErrorStrings _errors; | |
public static ErrorStrings Current => _errors ?? (_errors = new ErrorStrings()); | |
public static CultureInfo _culture; | |
public ErrorStrings() | |
{ | |
_resmanager = new ResourceManager(ResourceId, typeof(ErrorStrings).GetTypeInfo().Assembly); | |
_culture = new CultureInfo("is-IS"); //This can either be picked up in config or current culture. | |
} | |
#endregion | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment