GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - onitgroup/ResxGenerator

BranchesTags
Open more actions menu

Latest commit

 

History

24 Commits
24 Commits

Folders and files

NameName
Last commit message
Last commit date
src
src
 
 
.gitignore
.gitignore
 
 
README.md
README.md
 
 

Repository files navigation

Resx Generator

A tool to automatically generate, update, export and import .resx files. The tool uses Roslyn to search the project's source code for references to specific symbols, collect the strings and group them by resource type.

  • Resx Generator
    • Installation
    • How does it work
      • Searched Symbols
      • Resource Type Resolution
      • Resx location
    • Configuration file
      • DefaultResourceName
      • Languages
      • WriteKeyAsValue
      • ValidationComment
      • OverwriteTranslations
      • Translator
      • ChatGPT
      • Google Translate
    • Commands
      • Generate
      • Export to Excel
      • Import from Excel
      • Add ChatGPT Configuration
    • Excel Format
    • Monitoring

Installation

You can install the extension directly from the Visual Studio marketplace.

Alternatively, you can download the package and install it manually. Download the zip with the latest version from the Releases section and unzip it, then double click the file "ResxGenerator.VSExtension.vsix". The Visual Studio installation process will start.

Note

Visual Studio version >= 17.7 is required.

How does it work

Important

The tool will operate ONLY on the active project and its locally referenced projects.

The active project depends on the currently open file.

Searched Symbols

The tool only considers the following symbols, overloads are included:

  • Microsoft.AspNetCore.Mvc.Localization.IHtmlLocalizer[]
  • Microsoft.Extensions.Localization.IStringLocalizer[]
    • @SharedLocalizer["Login"]
    • _sharedLocalizer["Parameter {0} is required", variable]
  • LocalizerExtensions.HtmlLocalizerExtensions
  • LocalizerExtensions.StringLocalizerExtensions
    • @SharedLocalizer.Pluralize(...)
    • @SharedLocalizer.Genderize(...)
  • System.ComponentModel.DataAnnotations.DisplayAttribute (all string parameters)
    • [Display(Name = "Search")]
  • System.ComponentModel.DataAnnotations.RequiredAttribute (all string parameters)
    • [Required(ErrorMessage = "The field is required")]
  • System.ComponentModel.DescriptionAttribute (all string parameters)
    • [Description("Deleted")]

Important

Only hardcoded strings are taken into account, not variables.

Resource Type Resolution

Strings are automatically grouped by resource type.

The type is inferred from the generic type argument of IStringLocalizer<T> or IHtmlLocalizer<T>

// Strings go to SharedResource.{lang}.resx
private readonly IStringLocalizer<SharedResource> _sharedLocalizer;

// Strings go to MyResource.{lang}.resx
private readonly IStringLocalizer<MyResource> _myLocalizer;

Or resolved directly from the attribute's ResourceType or ErrorMessageResourceType parameter:

[Display(Name = "Name", ResourceType = typeof(AppResource))]
[Required(ErrorMessageResourceName = "The field is required", ErrorMessageResourceType = typeof(AppResource))]
public string Name { get; set; }

When is not possible to infer the resource type the string falls back to DefaultResourceName:

[Description("Some description")]

Resx location

When the resource type is known the .resx files are created next to the corresponding .cs class file:

MainWebApp/
└── Resources/
    ├── AppResource.cs
    ├── AppResource.it-IT.resx
    └── AppResource.en-US.resx

SharedResources/
├── SharedResource.cs
├── SharedResource.it-IT.resx
└── SharedResource.en-US.resx

Otherwise, for DefaultResourceName, the resx are created under the active project root.

MyProject/
├── DefaultResourceName.it-IT.resx
└── DefaultResourceName.en-US.resx

Configuration file

The tool looks for a resx-generator.json file in the active project's root directory. If not found, it will be created automatically on the first run.

{
  "DefaultResourceName": "SharedResource",
  "ValidationComment": "To be validated",
  "WriteKeyAsValue": true,
  "Languages": ["en-US", "fr-FR"],
  "Translator": "ChatGPT | GoogleTranslate",
  "OverwriteTranslations": false,
  "ChatGPT": {
    "Token": "*AUTHENTICATION TOKEN*",
    "Model": "gpt-3.5-turbo",
    "Prompt": "Translate the values of this JSON object from this locale {sourceLanguage} to this locale {targetLanguage} preserving its keys"
  }
}

DefaultResourceName

The fallback resource name used for strings that cannot be attributed to a specific resource type, see Resource Type Resolution above.

Important

This parameter is REQUIRED

Languages

The list of target languages in ISO format, e.g. ["en-US", "it-IT", "fr-FR"].

Note

The project's NeutralLanguage is used as the source language for translators. If it appears in this list, it will be ignored.

WriteKeyAsValue

(optional) If true, when a new entry is added the value will be set equal to the key. If false, the value will be left empty. Defaults to false.

ValidationComment

(optional) If set, it will be added as a comment to every new or modified entry.

OverwriteTranslations

(optional) If false, keys that already have a translation will not be translated again. If true, they will be retranslated. Defaults to false.

Note

Keys with an empty value are always translated regardless of this setting.

Translator

(optional) The translation service to use. If not set, strings will not be translated. Accepted values: ChatGPT, GoogleTranslate.

Note

When a value is translate a comment is added 'Generated by {translator}'

ChatGPT

"ChatGPT": {
  "Token": "*AUTHENTICATION TOKEN*",
  "Model": "gpt-3.5-turbo",
  "Prompt": "Translate every value of the following JSON object from this locale {sourceLanguage} to this locale {targetLanguage}, do not translate symbols"
}
  • Token: the authentication token for the ChatGPT API;
  • Model: the ChatGPT model to use;
  • Prompt: the translation instruction sent to ChatGPT. Two placeholders are available: {sourceLanguage} and {targetLanguage}. A JSON object with the strings to translate will be appended automatically;

Caution

Modifying the prompt too much can completely break the integration.

Note

Since the integration is based on ChatGPT, there may be unexpected behaviors. If any problems occur, try rerunning the tool.

Google Translate

No additional configuration is required. It uses a free Google Translate API.

Warning

Since it is a free API, translations may not be perfect. Careful verification is advised.

Commands

In the Visual Studio top menu, under Extensions, a submenu Resx Generator is available with the following commands.

Generate

Analyzes the source code, collects all localized strings grouped by resource type and generates or updates the corresponding .resx files.

The strings are translate if a translator is set.

Export to Excel

Exports both the .resx entires as well as the codes strings to an Excel file. Each row represents a key and includes the translated values and comments for each language. See Excel Format for details.

Import from Excel

Reads a previously exported Excel file and updates the corresponding .resx files. The resource type is resolved from the Resource Name column using the same rules as the Generate command.

Note

Importing always overwrites existing translations.

Important

The Excel file must follow the format produced by the Export command. Do not rename or reorder columns.

Add ChatGPT Configuration

Adds the default ChatGPT configuration block to the resx-generator.json file.

Excel Format

The exported Excel file contains a single sheet named Translations with the following columns:

Column Description
Resource Name The fully qualified name of the resource type (e.g. MyApp.Resources.SharedResource)
Key The resource key
Occurrences Number of times the key appears in the source code. 0 means the key exists in the .resx but is not referenced in code
{lang} - Value The translated value for the language (one pair of columns per language)
{lang} - Comment The comment for the language

Monitoring

A detailed log of all operations can be found in View → Output. Select Resx Generator from the list of sources.

About

No description, website, or topics provided.

Resources

Readme
Activity
Custom properties

Stars

1 star

Watchers

1 watching

Forks

0 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.