Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/khellang/resourcehelpergenerator
An MSBuild task that generates a strongly typed helper class for resource files with support for string formatting.
https://github.com/khellang/resourcehelpergenerator
Last synced: about 2 months ago
JSON representation
An MSBuild task that generates a strongly typed helper class for resource files with support for string formatting.
- Host: GitHub
- URL: https://github.com/khellang/resourcehelpergenerator
- Owner: khellang
- License: mit
- Created: 2014-11-20T21:57:02.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2015-06-19T09:25:59.000Z (over 9 years ago)
- Last Synced: 2024-11-03T20:28:14.721Z (2 months ago)
- Language: C#
- Homepage:
- Size: 325 KB
- Stars: 7
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ResourceHelperGenerator
An MSBuild task that generates a strongly typed helper class for resource files with support for string formatting.
The task will automatically hook into the MSBuild pipeline when you install the NuGet package, either using `Install-Package ResourceHelperGenerator` or through the Package Manager UI.
It will generate strongly typed helpers for `*.resx` files under the `Properties` folder of your project. This will hopefully be customizable at some point in the future.
String resources containing either `{0}`-type placeholders or `{argumentName}`-type placeholders, will get methods accepting corresponding arguments, while resources without placeholders will get properties generated.
## Example
If you have the following resource file, called `Strings.resx` sitting under the `Properties` folder of your project, which has a default namespace of `MyCompany.AwesomeApp`:
| Name | Value |
|------|-------|
| ArgumentNull | The argument '{argumentName}' cannot be null. |
| StringArgumentEmpty | The string argument '{argumentName}' cannot be empty. |The following file, `Strings.Designer.cs` will be generated and placed under the `Strings.resx` file in your project:
```csharp
//#if NETFX_CORE
#define RESOURCE_HELPER_TYPEINFO
#endifnamespace MyCompany.AwesomeApp
{
using System;
using System.CodeDom.Compiler;
using System.Diagnostics;
using System.Globalization;
using System.Reflection;
using System.Resources;[GeneratedCode("ResourceHelperGenerator", "0.3.1")]
#if RESOURCE_HELPER_INTERNAL
internal
#else
public
#endif
static class Strings
{
private static readonly ResourceManager ResourceManager
= new ResourceManager("MyCompany.AwesomeApp.Properties.Strings", GetAssembly(typeof(Strings)));///
/// The argument '{argumentName}' cannot be null.
///
public static string ArgumentNull(object argumentName)
{
return string.Format(CultureInfo.CurrentCulture, GetString("ArgumentNull", "argumentName"), argumentName);
}///
/// The string argument '{argumentName}' cannot be empty.
///
public static string StringArgumentEmpty(object argumentName)
{
return string.Format(CultureInfo.CurrentCulture, GetString("StringArgumentEmpty", "argumentName"), argumentName);
}private static string GetString(string name, params string[] formatterNames)
{
var value = ResourceManager.GetString(name);if (value == null)
{
throw new Exception(string.Format("Value for key '{0}' was null.", name));
}if (formatterNames != null)
{
for (var i = 0; i < formatterNames.Length; i++)
{
value = value.Replace("{" + formatterNames[i] + "}", "{" + i + "}");
}
}return value;
}private static Assembly GetAssembly(Type type)
{
#if RESOURCE_HELPER_TYPEINFO
return type.GetTypeInfo().Assembly;
#else
return type.Assembly;
#endif
}
}
}
```The first time this happens, the task will modify your project file (to add the helper) and you will be asked to reload the project.
## Internalized Helper Class
If you want to internalize the helper class, you must add the `RESOURCE_HELPER_INTERNAL` conditional compilation symbol in the project's build configuration.