Code Workshop 'Article Title URL Alias Generator' Launched

Code Workshop 'Article Title URL Alias Generator' Launched

Code Workshop is a new open-source project by the webmaster that provides web online tools, cross-platform desktop and mobile applications. The webmaster is committed to bringing you a more efficient and convenient experience. Today, the webmaster is proud to launch the 'Article Title URL Alias Generator', helping you easily create URL aliases for article titles, improving SEO and user experience. Come to Code Workshop and explore more practical tools!

Last updated 1/7/2024 12:12 AM
沙漠尽头的狼
6 min read
Category
Blazor
Tags
.NET Blazor Avalonia UI Open Source Project Open Source

Hello everyone, I'm the Wolf at the End of the Desert. Today, I want to introduce a very practical tool: Code World Workshop.

Code World Workshop is an open-source project newly developed by the site admin. It provides online web tools, as well as cross-platform desktop and mobile app versions. Our goal is to bring you a more efficient and convenient online tool experience. Today, I am very honored to launch the "Article Title URL Alias Generator", which helps you easily create URL aliases for article titles, improving SEO effectiveness and user experience.

What is this tool?

Have you ever encountered this problem: You wrote a great article, but because the title is too long or contains special characters, the URL looks unattractive or even affects SEO? Now, with the "Article Title URL Alias Generator", these issues will be resolved.

This tool provides three main functions:

  1. Chinese-English Translation: Translate Chinese titles into English, or 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.

It is especially suitable for when you have a Chinese title ready and need to generate a URL for posting on your website. For example, converting the title of this article《码坊“文章标题URL别名生成器”上线》into code-world-workshop-article-title-url-alias-generator-launched using the tool. The conversion effect is as follows:

Title alias conversion demo

After publishing the article on the Dotnet9 website using this tool, the access URL on the site is https://dotnet9.com/2024/01/code-world-workshop-article-title-url-alias-generator-launched.

Implementation of Chinese-English Translation and Alias Conversion

The implementation principle of this tool is very simple. We use the YandexTranslator class from the GTranslate package for Chinese-English translation, because it uses neural machine translation technology to provide higher quality translation results. For URL alias conversion, we use the Slugify.Core package, which is simple to use and provides good conversion results.

Let's take a look at the implementation code. First, we define the translation interface:

namespace CodeWF.Core;

/// <summary>
/// Article title translation
/// </summary>
public interface ITranslationService
{
	/// <summary>
	/// Chinese to English translation
	/// </summary>
	/// <param name="chineseText"></param>
	/// <returns></returns>
	public Task<string> ChineseToEnglishAsync(string? chineseText);

	/// <summary>
	/// English to Chinese translation
	/// </summary>
	/// <param name="englishText"></param>
	/// <returns></returns>
	public Task<string> EnglishToChineseAsync(string? englishText);

	/// <summary>
	/// English to URL slug conversion
	/// </summary>
	/// <param name="englishText"></param>
	/// <returns></returns>
	public string EnglishToUrlSlug(string? englishText);
}

Implement the translation interface:

namespace CodeWF.Core;

/// <summary>
/// Chinese-English translation uses Yandex Translation, which employs neural machine translation technology (NMT)
/// to provide higher quality translation results. Yandex Translation supports many language pairs, including some
/// less common languages, and is particularly good at handling translations between European languages.
/// </summary>
public class TranslationService : ITranslationService
{
    private readonly YandexTranslator _translator = new();
    private readonly SlugHelper _slugHelper = new();

    /// <summary>
    /// Chinese to English translation
    /// </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>
    /// English to Chinese translation
    /// </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>
    /// English to URL slug conversion
    /// </summary>
    /// <param name="englishText"></param>
    /// <returns></returns>
    public string EnglishToUrlSlug(string? englishText)
    {
        return string.IsNullOrWhiteSpace(englishText) ? string.Empty : _slugHelper.GenerateSlug(englishText);
    }
}

Let's do a unit test to verify:

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");
	}
}

Final Words

The screenshot in the article is from the cross-platform desktop version, developed using Avalonia UI and the control library Neumorphism.Avalonia. Once the functionality is developed to a certain extent, the site admin will introduce the framework of the tool. Below are the tool's screenshots:

Tool screenshot

Tool screenshot

If you are interested in this tool, you can visit the Code World Workshop official website at https://codewf.com to see more practical tools. The source code of Code World Workshop is also open source on GitHub. Contributions and attention are welcome.

Thank you for your support. It is your support and encouragement that keep me moving forward. If you have any questions or suggestions, feel free to contact us. Let's build a better Code World Workshop together!

Keep Exploring

Related Reading

More Articles
Same category / Same tag 11/6/2024

Why My Blog Website Returned to Blazor

The development of the blog website has gone through many hardships, with nearly 10 versions including MVC, Vue, Go, etc. Now it has returned to Blazor and adopted static SSR, resulting in a significant speed increase and successful launch.

Continue Reading
Same category / Same tag 2/29/2024

Data Display Can Also Be Done Like This in Winform

In the process of developing Winform, data display functionality is often required. Previously, the gridcontrol control was commonly used. Today, through an example, I would like to introduce how to use the table component from Ant Design Blazor for data display in a Winform Blazor Hybrid application.

Continue Reading
Same category / Same tag 2/29/2024

Can the Winform interface also look good?

A few days ago, I introduced using Blazor Hybrid in Winform, and mentioned that with the Blazor UI, our Winform programs can be designed to look better. Next, I will illustrate with an example of drawing in Winform Blazor Hybrid, hoping it helps you.

Continue Reading