在 jQuery timeago.js 中本地化字符串

Localize strings in jQuery timeago.js

本文关键字:本地化 字符串 js jQuery timeago      更新时间:2023-09-26

>我正在使用J查询时间前显示像这个网站这样的日期时间,我有一个元语言网站,我想向 en 用户展示:1 分钟前和 FA 1 دقیقه قبل .我想在timeago.min.js中使用资源键?

prefixAgo: null,
    prefixFromNow: null,
    suffixAgo: '<%= Resources.IPortal.Ago %>'//something like this,
    suffixFromNow: "from now",
    seconds: "less than a minute",
    minute: "about a minute",
    minutes: "%d minutes",
    hour: "about an hour",
    hours: "about %d hours",
    day: "a day",
    days: "%d days",
    month: "about a month",
    months: "%d months",
    year: "about a year",
    years: "%d years",

我找到了解决方案:我使用了一个页面GetLocalisedScript.aspx来提供我的js文件。

代码隐藏:

protected void Page_Load(object sender, EventArgs e)
{
    string retval = "";
    string file = Request["JsFileName"].ToString();
    using(StreamReader sr = new StreamReader(Server.MapPath(string.Format("~''scripts''{0}.js",file))))
    {
        retval = sr.ReadToEnd();
        sr.Close();
    }
    Regex rx = new Regex("##Translate(.+?)##",RegexOptions.Singleline);
    MatchCollection mc =  rx.Matches(retval,0);
    foreach (Match m in mc)
    {
        string strResxKey = m.Value.Replace("##Translate(", "").Replace(")##", "");
        string val = GetGlobalResourceObject("myResource", strResxKey).ToString();
        retval = retval.Replace(m.Value, val); 
    }
    //Just write out the XML data
    Response.ContentType = "text/xml";
    //NOTE THAT THIS PAGE DOESN'T CONTAIN ANY HTML TAG, WHATSOEVER
    Response.Output.Write(retval);
}

网页标记 :

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GetLocalisedScript.aspx.cs" Inherits="TestMulti.GetLocalisedScript" %>

在我的页面中,将标准SRC替换为以下内容:

<script src="GetLocalisedScript.aspx?JsFileName=JsFileNameWithoutExtension" type="text/jscript" ></script>

现在在我的js文件(Js文件名没有扩展名)中,我将像这样更改字符串:

function alert2(val) {
alert("##Translate(MyStringToTranslate)##");
}