3

我使用 J Query time ago 来显示这个网站的日期时间,我有一个元语言网站,我想为 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",
4

1 回答 1

1

我找到了解决方案:我使用了一个页面 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);
}

HTML 标记:

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

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

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

现在在我的 js 文件(JsFileNameWithoutExtension)中,我将更改如下字符串:

function alert2(val) {
alert("##Translate(MyStringToTranslate)##");
}
于 2013-10-25T08:30:05.747 回答