Skip to content

Instantly share code, notes, and snippets.

@inspoy
Created November 6, 2017 01:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save inspoy/9e4c955987b76c114c2eadf3f1bb6d14 to your computer and use it in GitHub Desktop.
Save inspoy/9e4c955987b76c114c2eadf3f1bb6d14 to your computer and use it in GitHub Desktop.
/// <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;
}
@inspoy
Copy link
Author

inspoy commented Nov 9, 2017

m_argPattern = new Regex(@"(?<=([^{]|^)){\d+}(?=([^}]|$))", RegexOptions.Compiled);
m_refPattern = new Regex(@"#\w+#", RegexOptions.Compiled);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment