Azure 翻译 notranslate 的字符串替换
我一直在努力尝试让 Azure 翻译器转换存储在数据库列中的文本。以下是当前如何存储文本的几个示例: 例如1。 “从预定义的 %% 目标 %% 集中添加 %% 目标 %%” 例如2。 %%风险%% 例如3。这里有一些文字%%模型%%。请刷新页面。
我的目标是翻译除 % % 内的数据之外的所有内容。问题是 Azure 翻译它必须在
english = "Add %%objectives%% from predefined sets of %%objectives%%";
if (english.Contains("%%"))
{
Dictionary<int, int> positions = new Dictionary<int, int>(); // this is to hold the locations of where delims are in string
ArrayList l = new ArrayList();
char[] letters = english.ToCharArray();
// get the first location of %
for (int i = 0; i < english.Length; i++)
{
if (letters[i] == '%')
{
l.Add(i);
}
}
string temp = "";
// only works if theres 1 % in the string
if (l.Count == 4)
{
int loc = english.IndexOf('%'); //%%Model%% = 0
int lastloc = english.LastIndexOf('%');
temp = " <div class=\"notranslate\">" + english.Substring(loc + 2, (lastloc - 3) - loc) + "</div>";
var lang = Translate(convert(english, temp), "en", "it");
// need to convert back to %%
Console.WriteLine(lang);
dataNode.SelectSingleNode("value").InnerText = lang;
}
else if (l.Count > 4) //this means that there are more than 1 delimted
{
foreach(int i in l) // 4 , 5 , 16 ,17, 43, 44
// % % text % % text % %
{
}
感谢任何帮助!
I have been working on trying to get Azure translator to convert text stored in a database column. Here is a couple of examples of how the text is currently stored:
eg1. "Add %%objectives%% from predefined sets of %%objectives%%"
eg2. %%Risk%%
eg3. some text here %%model%%. Please refresh the page.
My goal is to translate everything but the data within the % %. The problem is with Azure translate it has to be within the syntax of <div class="notranslate">" "" which means I have to replace all of the %% with that syntax. I was able to convert this and it works with only 1 within the string but everything else seemed to go down a rabbit hole. Here is my code:
english = "Add %%objectives%% from predefined sets of %%objectives%%";
if (english.Contains("%%"))
{
Dictionary<int, int> positions = new Dictionary<int, int>(); // this is to hold the locations of where delims are in string
ArrayList l = new ArrayList();
char[] letters = english.ToCharArray();
// get the first location of %
for (int i = 0; i < english.Length; i++)
{
if (letters[i] == '%')
{
l.Add(i);
}
}
string temp = "";
// only works if theres 1 % in the string
if (l.Count == 4)
{
int loc = english.IndexOf('%'); //%%Model%% = 0
int lastloc = english.LastIndexOf('%');
temp = " <div class=\"notranslate\">" + english.Substring(loc + 2, (lastloc - 3) - loc) + "</div>";
var lang = Translate(convert(english, temp), "en", "it");
// need to convert back to %%
Console.WriteLine(lang);
dataNode.SelectSingleNode("value").InnerText = lang;
}
else if (l.Count > 4) //this means that there are more than 1 delimted
{
foreach(int i in l) // 4 , 5 , 16 ,17, 43, 44
// % % text % % text % %
{
}
Any help is appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要替换“%%”。在任何奇数序列号 '%%' 之前插入
并在任何偶数序列号 '%' 之后插入
%'。这样,翻译就保留了 %% 标记。
Do not replace the '%%'. Insert the
<div class="notranslate">
before any odd sequence number '%%' and insert the</div>
after any even sequence number '%%'. This way, the translation retains the %% markup.如果您可以选择在将变量替换为真实的、人类可读的值之后翻译字符串,那么您将获得更好的翻译。
If you have the option to translate the string after the variables have been replaced with real, human-readable values, you will get a better translation out of it.