Code Workshop "Article Title URL Alias Generator" is launched

Code Workshop "Article Title URL Alias Generator" is launched

Code Workshop is a newly opened open source project by the webmaster that provides online tools, cross-platform desktop and mobile applications. Webmasters will ultimately strive to bring you a more efficient and convenient experience. Today, webmasters are honored to launch the "Article Title URL Alias Generator" to help you easily create URL aliases for article titles and improve SEO effects and user experience. Come to the code workshop and explore more practical tools!

最后更新 1/7/2024 12:12 AM
沙漠尽头的狼
预计阅读 5 分钟
分类
Blazor
标签
.NET Blazor Avalonia UI open source projects open source

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:

  1. Translation between Chinese and English: Translate Chinese titles into English, or translate English titles into Chinese.
  2. English to URL alias: Convert English titles into concise URL aliases for easy use on websites.
  3. 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!

Keep Exploring

延伸阅读

更多文章
同分类 / 同标签 11/6/2024

Why is my blog site back in Blazor?

The blog website has gone through hardships in development. It has tried nearly 10 versions such as MVC, Vue, and Go. Now it has returned to Blazor and adopts static SSR. The speed has skyrocketed and it has been successfully launched.

继续阅读
同分类 / 同标签 2/29/2024

Data display can also be done in Winform

In the process of developing winform, data display functions are often needed. I have been using the gridcontrol before. Today, I want to use an example to introduce to you how to use the table component in ant design blazor hybrid to display data.

继续阅读
同分类 / 同标签 2/29/2024

Can Winform's interface become better?

A few days ago, I introduced to you the use of blazor hybrid in winform, and I also said that with blazor's ui, our winform program design can be more beautiful. Next, I want to use an example of drawing in winform blazor hybrid to illustrate it, hoping to be helpful to you.

继续阅读