Created
November 6, 2017 01:03
-
-
Save inspoy/9e4c955987b76c114c2eadf3f1bb6d14 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// 得到处理参数和转义后的最终字符串 | |
/// </summary> | |
/// <param name="rawStr">未处理的字符串</param> | |
/// <param name="args">参数</param> | |
/// <returns></returns> | |
public string GetFinalString(string rawStr, params object[] args) | |
{ | |
// 处理参数和转义 | |
// 填充参数 | |
var argMatches = m_argPattern.Matches(rawStr); | |
foreach (Match argMatch in argMatches) | |
{ | |
string argString = argMatch.ToString(); | |
argString = argString.Substring(1, argString.Length - 2); | |
int argIdx; | |
if (int.TryParse(argString, out argIdx)) | |
{ | |
if (args.Length > argIdx) | |
{ | |
string toReplace = args[argIdx].ToString(); | |
rawStr = rawStr.Replace(argMatch.ToString(), toReplace); | |
} | |
else | |
{ | |
Debug.LogWarningFormat("[本地化]参数不足:{0}", rawStr); | |
} | |
} | |
} | |
// 填充术语引用 | |
var refMatches = m_refPattern.Matches(rawStr); | |
foreach (Match refMatch in refMatches) | |
{ | |
string refString = refMatch.ToString(); | |
refString = refString.Substring(1, refString.Length - 2); | |
int locId; | |
if (int.TryParse(refString, out locId)) | |
{ | |
rawStr = rawStr.Replace(refMatch.ToString(), GetString(locId)); | |
} | |
else | |
{ | |
rawStr = rawStr.Replace(refMatch.ToString(), GetString(refString)); | |
} | |
} | |
// 处理转义 | |
rawStr = rawStr.Replace("{{", "{"); | |
rawStr = rawStr.Replace("}}", "}"); | |
rawStr = rawStr.Replace("##", "#"); | |
return rawStr; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
m_argPattern = new Regex(@"(?<=([^{]|^)){\d+}(?=([^}]|$))", RegexOptions.Compiled);
m_refPattern = new Regex(@"#\w+#", RegexOptions.Compiled);