2013-10-24 70 views
3

我正在使用J查詢時間前顯示日期時間喜歡這個網站, 和我有一個元語言網站,我想顯示爲en用戶:1分鐘前和爲fa 1دقیقهقبل 。 我想使用timeago.min.js中的資源密鑰?本地化jQuery中的字符串timeago.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", 

回答

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