Hello everyone, I am a wolf at the end of the desert. Today, I want to introduce to you a very practical tool, code workshop.
Code Workshop is an open source project newly developed by the webmaster. It provides web online tools and also provides cross-platform desktop and mobile App versions. Our goal is to provide you with a more efficient and convenient online tool experience. Today, I am very honored to launch the "Article Title URL Alias Generator", which can help you easily create URL aliases for article titles and improve SEO results and user experience.
What tool is this?
Have you ever encountered the problem of writing a good article, but the URL looks unsightly because the title is too long or contains special characters, and even affects the SEO effect? Now, with the "Article Title URL Alias Generator", these issues will be solved.
This tool provides three major functions:
- Translation between Chinese and English: Translate Chinese titles into English, or translate English titles into Chinese.
- English to URL alias: Convert English titles into concise URL aliases for easy use on websites.
- Chinese to URL alias: Convert Chinese titles into concise URL aliases for easy use on websites.
特别适合文章取好中文标题后,需要在网站发布取文章访问链接生成使用。比如将本文标题《码坊“文章标题URL别名生成器”上线》在工具中转换为code-world-workshop-article-title-url-alias-generator-launched,转换效果如下:

使用该工具在Dotnet9网站发布后,在网站上的访问地址是https://dotnet9.com/2024/01/code-world-workshop-article-title-url-alias-generator-launched。
Realization of Chinese-English translation and alias conversion
这个工具的实现原理很简单。我们使用了GTranslate包的YandexTranslator类来进行中英互译,因为它使用了神经网络机器翻译技术,可以提供更高质量的翻译结果。对于 URL 别名的转换,我们使用了Slugify.Core包,用法简单,转换效果也很好。
Let's take a look at the implementation code. Let's first define the transformation interface:
namespace CodeWF.Core;
/// <summary>
/// 文章标题翻译
/// </summary>
public interface ITranslationService
{
/// <summary>
/// 中英文翻译
/// </summary>
/// <param name="chineseText"></param>
/// <returns></returns>
public Task<string> ChineseToEnglishAsync(string? chineseText);
/// <summary>
/// 英中文翻译
/// </summary>
/// <param name="englishText"></param>
/// <returns></returns>
public Task<string> EnglishToChineseAsync(string? englishText);
/// <summary>
/// 英文与URL别名转换
/// </summary>
/// <param name="englishText"></param>
/// <returns></returns>
public string EnglishToUrlSlug(string? englishText);
}
Implement the conversion interface:
namespace CodeWF.Core;
/// <summary>
/// 中英互译使用Yandex Translation,Yandex使用了神经网络机器翻译技术(NMT),
/// 以提供更高质量的翻译结果。Yandex Translation 支持多种语言对,包括一些
/// 较少见的语言,并且特别擅长处理欧洲语言之间的翻译。
/// </summary>
public class TranslationService : ITranslationService
{
private readonly YandexTranslator _translator = new();
private readonly SlugHelper _slugHelper = new();
/// <summary>
/// 中英文翻译
/// </summary>
/// <param name="chineseText"></param>
/// <returns></returns>
public async Task<string> ChineseToEnglishAsync(string? chineseText)
{
return string.IsNullOrWhiteSpace(chineseText)
? string.Empty
: (await _translator.TranslateAsync(chineseText, "en")).Translation;
}
/// <summary>
/// 英中文翻译
/// </summary>
/// <param name="englishText"></param>
/// <returns></returns>
public async Task<string> EnglishToChineseAsync(string? englishText)
{
return string.IsNullOrWhiteSpace(englishText)
? string.Empty
: (await _translator.TranslateAsync(englishText, "zh-CN")).Translation;
}
/// <summary>
/// 英文与URL别名转换
/// </summary>
/// <param name="englishText"></param>
/// <returns></returns>
public string EnglishToUrlSlug(string? englishText)
{
return string.IsNullOrWhiteSpace(englishText) ? string.Empty : _slugHelper.GenerateSlug(englishText);
}
}
Let's take a unit test verification:
namespace CodeWF.Core.Test;
[TestClass]
public class TranslationServiceUnitTest
{
private readonly ITranslationService _translationService = new TranslationService();
[TestMethod]
public async Task Test_ChineseToEnglishAsync_SUCCESS()
{
const string chineseText = "码坊";
var englishText = await _translationService.ChineseToEnglishAsync(chineseText);
Assert.AreEqual(englishText, "Code World Workshop");
}
[TestMethod]
public async Task Test_EnglishToChineseAsync_SUCCESS()
{
const string englishText = "Code World Workshop";
var chineseText = await _translationService.EnglishToChineseAsync(englishText);
Assert.AreEqual(chineseText, "代码世界工作坊");
}
[TestMethod]
public void Test_EnglishToSlug_SUCCESS()
{
const string englishText = "Code World Workshop";
var urlSlug = _translationService.EnglishToUrlSlug(englishText);
Assert.AreEqual(urlSlug, "code-world-workshop");
}
[TestMethod]
public async Task Test_ChineseToSlugAsync_SUCCESS()
{
const string chineseText = "码坊";
var englishText = await _translationService.ChineseToEnglishAsync(chineseText);
var urlSlug = _translationService.EnglishToUrlSlug(englishText);
Assert.AreEqual(urlSlug, "code-world-workshop");
}
}
last words
文中截图是跨平台桌面版本,采用Avalonia UI和控件库Neumorphism.Avalonia开发,待功能开发到一定程度,站长会介绍工具框架的搭建,下面是工具效果图:


如果你对这个工具感兴趣,可以访问码坊官网https://codewf.com,查看更多实用工具。码坊的源码也在GitHub开源,欢迎大家关注和贡献。
Thank you for your support. It is your support and encouragement that keeps me moving forward. If you have any questions or suggestions, please feel free to contact us. Let's build a better code workshop together!