Skip to content

OpportunityLiu/ResourceGenerator

Repository files navigation

ResourceGenerator

A tool to generate classes for UWP string resources.

NuGet Build status

How-To

  1. Install package

    To install this package, execute following command in package manager console.

    Install-Package Opportunity.ResourceGenerator
  2. Create config file

    To create default config file of ResourceGenerator, execute following command.

    New-Config Resources/Strings

    You can also specify paths other than Resources/Strings.
    Use -Project <ProjectName> to create config file in project other than default project.

  3. Edit config file

    After step 2, you'll get a .resgenconfig file with following content.

    {
      "`$schema": "https://raw.githubusercontent.com/OpportunityLiu/ResourceGenerator/master/resgenconfig.json?version=1.4.0",
      // Path for resource files (*.resw & *.resjson).
      // Default value is "/Strings".
      "ResourcePath": "/Strings",
    
      // Default language of resources, will be detected automatically if unset.
      //"SourceLanguagePath": "en-Us",
    
      // Namespace for resource visitor class.
      // Default value is "<ProjectDefaultNamespace>".
      //"LocalizedStringsNamespace": "MyNamespace",
    
      // Namespace for resource visitor interfaces.
      // Default value is "<ProjectDefaultNamespace>.ResourceInfo".
      //"InterfacesNamespace": "MyNamespace.ResourceInfo",
    
      // Modifier for resource visitor class and interfaces.
      "Modifier": "internal",
    
      // Specifies whether this project is the default project or not.
      // Determines if it is necessary to contains project name in the resource path.
      "IsDefaultProject": true,
    
      // Regard resource strings whose name starts with '$' as format string.
      // Default value is false.
      //"IsFormatStringEnabled": true,
    
      // Format function used for format strings, must be replaceble with string.Format.
      //"FormatStringFunction": "string.Format",
    
      // Excluded resource paths.
      //"Exclude": [],
    
      // Included resource paths, has higher priority than Exclude.
      //"Include": [],
    
      // Specifies whether the tool generates code that is debuggable.
      "DebugGeneratedCode": false
    }

    Edit this file to control properties of generated classes.

  4. Generate resource class

    Run following command to generate resource class.

    Convert-Resource -Project <ProjectName>

    To generate resource classes in all projects, run Convert-Resource without arguments.

If you edited your resource file (.resw & .resjson), re-generate resource classes as the last step.

Features

Takes following .resjson file as an example:

//File `Resources.resjosn`:
{
  "AppName": "TestName",
  "ContentTextBox": {
    "Header": "Header",
    "Text": "Content",
    "ToolTipService/ToolTip": "A simple text box."
  },
  "$FileNotFound": "Line {line:g}: Can not find file with name \"{name}\" in \"{path}\""
}
  1. Nested resource strings (. in .resw file or / in .resjson file)

    For example, you can visit the tool tip ("A simple text box.") via expression Strings.Resources.ContentTextBox.ToolTipService.ToolTip.

  2. Format resource strings (resource strings whose name starts with a $)

    To enable this feature, you should set IsFormatStringEnabled to true in .resgenconfig file.
    You can find a generated function string Strings.Resources.FileNotFound(object line, object name, object path) for format strings.

  3. Dynamic visit support

    You should use a pair of parentheses to end visiting with a string result.
    Dynamic version of the first example looks like following:

    string tooltip = Strings.Resources.ContentTextBox.ToolTipService.ToolTip;
    dynamic resources = Strings.Resources;
    string tooltip1 = (string)resources.ContentTextBox.ToolTipService.ToolTip();
    string tooltip2 = (string)resources.ContentTextBox["ToolTipService"].ToolTip();
    string tooltip3 = (string)resources.ContentTextBox["ToolTipService/ToolTip"]();

About

A tool to generate classes for UWP string resources.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published