Thanks to visit codestin.com
Credit goes to Github.com

Skip to content

AiursoftWeb/Dotlang

Repository files navigation

ASP.NET Core App Translator

MIT licensed Pipeline stat Test Coverage NuGet version (Aiursoft.DotLang) Man hours

This app helps you generate translated .cshtml files and resources files. Based on the Ollama AI model, it translates the content inside @Localizer[""] tags in your ASP.NET Core project.

Installation

Requirements:

  1. .NET 10 SDK

Run the following command to install this tool:

dotnet tool install --global Aiursoft.Dotlang

Usage

After getting the binary, run it directly in the terminal.

dotlang translate-aspnet --path ~/Code --instance http://ollama:11434/api/chat --model "qwen:32b" --token "your-ollama-token"
Description:
  The command to start translation on an ASP.NET Core project.

Usage:
  dotlang translate-aspnet [options]

Options:
  -v, --verbose                           Show detailed log
  -d, --dry-run                           Preview changes without actually making them
  -p, --path <path> (REQUIRED)            Path of the videos to be parsed.
  -l, --languages <languages> (REQUIRED)  The target languages code. Connect with ','. For example: zh_CN,en_US,ja_JP [default: 
                                          zh-CN,zh-TW,zh-HK,ja-JP,ko-KR,vi-VN,th-TH,de-DE,fr-FR,es-ES,ru-RU,it-IT,pt-PT,pt-BR,ar-SA,nl-NL,sv-SE,pl-PL,tr-TR]
  --instance <instance> (REQUIRED)        The Ollama instance to use.
  --model <model> (REQUIRED)              The Ollama model to use.
  --token <token> (REQUIRED)              The Ollama token to use.
  -?, -h, --help                          Show help and usage information

For example, if you want to localize entire project with deepseek, you can run:

DEEPSEEK_API_KEY="sk-aaaaaaaa"
dotlang auto-generate-view-injections-for-aiursoft-template --path .
dotlang generate-resx-csharp      --path . --model "deepseek-chat" --token $DEEPSEEK_API_KEY --instance "https://api.deepseek.com/chat/completions" -c 8
dotlang generate-resx-view        --path . --model "deepseek-chat" --token $DEEPSEEK_API_KEY --instance "https://api.deepseek.com/chat/completions" -c 8
dotlang generate-resx-annotations --path . --model "deepseek-chat" --token $DEEPSEEK_API_KEY --instance "https://api.deepseek.com/chat/completions" -c 8

For example, if you want to localize entire project with Aiursoft Ollama server, you can run:

OLLAMA_API_KEY="CONFIDENTIAL"
dotlang auto-generate-view-injections-for-aiursoft-template --path .
dotlang generate-resx-csharp      --path . --model "qwen3:30b-a3b-instruct-2507-q8_0" --token $OLLAMA_API_KEY --instance "https://ollama.aiursoft.com/api/chat/completions" -c 8
dotlang generate-resx-view        --path . --model "qwen3:30b-a3b-instruct-2507-q8_0" --token $OLLAMA_API_KEY --instance "https://ollama.aiursoft.com/api/chat/completions" -c 8
dotlang generate-resx-annotations --path . --model "qwen3:30b-a3b-instruct-2507-q8_0" --token $OLLAMA_API_KEY --instance "https://ollama.aiursoft.com/api/chat/completions" -c 8

Will help you generate .resx files for all the .cshtml files in the current folder.

Use as a simple translator

This tool can also be used to simply translate a folder of files from one language to another.

dotlang folder-translate --source "./src" --destination "./dist" --language "zh-CN" --extensions "txt" --extensions "md" --recursive --model "qwen:32b" --token "your-ollama-token" --instance "http://ollama:11434/api/chat"

Options:

  • -s, --source (REQUIRED): Path of the folder to translate files.
  • -d, --destination (REQUIRED): Path of the folder to save translated files.
  • -l, --language (REQUIRED): The target language code.
  • -r, --recursive: Recursively search for files in subdirectories.
  • -e, --extensions: Extensions of files to translate. Can be used multiple times. Default is html.
  • --instance: The Ollama instance to use.
  • --model: The Ollama model to use.
  • --token: The Ollama token to use.
  • -k, --skip-existing-files: Skip existing files.

Use as a library

You can also use the core logic as a library in your own project.

First, install the package:

dotnet add package Aiursoft.Dotlang.AspNetTranslate

Then, register the services in your IServiceCollection:

using Aiursoft.Dotlang.AspNetTranslate;
using Aiursoft.Dotlang.Shared;
using Aiursoft.Canon;
using Aiursoft.GptClient;

// ...
services.AddLogging();
services.AddHttpClient();
services.AddMemoryCache();
services.AddTaskCanon();
services.AddGptClient();

services.AddScoped<MarkdownShredder>();
services.AddScoped<OllamaBasedTranslatorEngine>();
services.AddScoped<CachedTranslateEngine>();
services.AddScoped<FolderFilesTranslateEngine>();
services.AddScoped<TranslateEntry>();

// Add necessary specialized services for TranslateEntry
services.AddScoped<DataAnnotationKeyExtractor>();
services.AddScoped<CshtmlLocalizer>();
services.AddScoped<CSharpKeyExtractor>();
services.AddScoped<RenderInNavBarExtractor>();
services.AddTransient<DocumentAnalyser>();

new StartUp().ConfigureServices(services);
services.Configure<TranslateOptions>(options =>
{
    options.OllamaInstance = "https://ollama.example.com/api/chat/completions";
    options.OllamaModel = "qwen3:30b";
    options.OllamaToken = "your-token";
});

Finally, you can use TranslateEntry to perform translation tasks:

var entry = serviceProvider.GetRequiredService<TranslateEntry>();

Or use FolderFilesTranslateEngine for simple file translation:

var folderEngine = serviceProvider.GetRequiredService<FolderFilesTranslateEngine>();

await folderEngine.TranslateAsync(
    sourceFolder: "./src",
    destinationFolder: "./dist",
    language: "zh-CN",
    recursive: true,
    extensions: [".txt", ".md"],
    skipExistingFiles: false);

Translate plain text

You can also use the OllamaBasedTranslatorEngine to translate plain text strings directly.

var translator = serviceProvider.GetRequiredService<OllamaBasedTranslatorEngine>();

var englishText = "Hello, world!";
var chineseText = await translator.TranslateAsync(englishText, "zh-CN");

Console.WriteLine(chineseText); // 你好,世界!

Advanced Usage with TranslateEntry

var entry = serviceProvider.GetRequiredService<TranslateEntry>();

// Localize all .cshtml files in a project
await entry.StartLocalizeContentInCsHtmlAsync(
    path: "./MyProject", 
    langs: ["zh-CN", "ja-JP"], 
    takeAction: true, 
    concurentRequests: 8);

// Localize all .cs files
await entry.StartLocalizeContentInCSharpAsync(path, langs, true, 8);

// Localize DataAnnotations
await entry.StartLocalizeDataAnnotationsAsync(path, langs, true, 8);

// Wrap plain text with @Localizer[""]
await entry.StartWrapWithLocalizerAsync(path, true);

Run locally

Requirements about how to run

  1. .NET 10 SDK
  2. Ollama.
  3. Execute dotnet run to run the app

Run in Microsoft Visual Studio

  1. Open the .sln file in the project path.
  2. Press F5.

How does this work?

  • Find all files ends with .cshtml.
  • foreach cshtml file, replace all text in tag surrounded with @Localizer[""] with the content inside.
  • Call ollama to translate all the content. (The entire file will be sent to ollama to make sure AI understands the context.)
  • Save the translated file as Resource file in the Resources folder.

The Core Translator won't override any existing translation nor resources files. If your content was already surrounded with @Localizer[""], we won't touch it.

Before running the translator

Use the following code to register the localizer service:

// In StartUp.cs ConfigureServices method:
services.AddLocalization(options => options.ResourcesPath = "Resources");

services.AddMvc()
    .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
    .AddDataAnnotationsLocalization();

Use the following code to add localizer middleware:

// In StartUp.cs Configure method
var SupportedCultures = new CultureInfo[]
{
    new CultureInfo("en"),
    new CultureInfo("zh")
};
app.UseRequestLocalization(new RequestLocalizationOptions
{
    DefaultRequestCulture = new RequestCulture(defaultLanguage),
    SupportedCultures = SupportedCultures,
    SupportedUICultures = SupportedCultures
});

Use the following code to inject localizer:

@using Microsoft.AspNetCore.Mvc.Localization
@inject IViewLocalizer Localizer
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

Now run this app!

Caution

Running this under your project folder may ruin your project! It may change your cshtml! Do run git commit under your project before running this app.

How to contribute

There are many ways to contribute to the project: logging bugs, submitting pull requests, reporting issues, and creating suggestions.

Even if you with push rights on the repository, you should create a personal fork and create feature branches there when you need them. This keeps the main repository clean and your workflow cruft out of sight.

We're also interested in your feedback on the future of this project. You can submit a suggestion or feature request through the issue tracker. To make this process more effective, we're asking that these include more information to help define them more clearly.

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •