用有道翻译将BlogEngine的博客文章标题转换为英文

#region using

using System;
using System.Web;
using System.Web.UI;
using BlogEngine.Core.Web.Controls;
using BlogEngine.Core;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Net;
using LSW.Api.YouDao;

#endregion

/// <summary>
/// Transliterate post Chinese slug into English
/// </summary>
[Extension("使用 有道 Api 将标题自动翻译并转换为英文别名", "1.0", "<a href=\"http://blog.lishewen.com\">算神</a>")]
public class YouDaoSlugTransliterator
{
    /// <summary>
    /// Hooks up an event handler to the Post.Serving event.
    /// </summary>
    static YouDaoSlugTransliterator()
    {
        Post.Saving += new EventHandler<SavedEventArgs>(Post_Saving);
    }

    /// <summary>
    /// Handles the Post.Saving event.
    /// </summary>
    private static void Post_Saving(object sender, SavedEventArgs e)
    {
        Post post = (Post)sender;
        if (post.New)
        {
            try
            {
                Translation t = new Translation();
                string translatedText = t.Translate(post.Title);

                //if (responseStatus == "200")
                //{
                    string slug = Utils.RemoveIllegalCharacters(translatedText);
                    if (slug.Length > 30)
                        slug.Substring(0, 30);

                    post.Slug = slug.ToLower();
                //}
            }
            catch
            {
                // We maybe need to catch some exception
            }
        }
    }
}

添加评论

Loading