Azure 翻译 notranslate 的字符串替换

发布于 2025-01-11 19:03:34 字数 1958 浏览 4 评论 0原文

我一直在努力尝试让 Azure 翻译器转换存储在数据库列中的文本。以下是当前如何存储文本的几个示例: 例如1。 “从预定义的 %% 目标 %% 集中添加 %% 目标 %%” 例如2。 %%风险%% 例如3。这里有一些文字%%模型%%。请刷新页面。

我的目标是翻译除 % % 内的数据之外的所有内容。问题是 Azure 翻译它必须在

" "" 的语法范围内,这意味着我必须用该语法替换所有 %%。我能够转换它仅适用于字符串中的 1,但其他所有内容似乎都陷入了困境,这是我的代码:

            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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦年海沫深 2025-01-18 19:03:34

不要替换“%%”。在任何奇数序列号 '%%' 之前插入

并在任何偶数序列号 '%' 之后插入

%'。这样,翻译就保留了 %% 标记。

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.

旧时浪漫 2025-01-18 19:03:34

如果您可以选择在将变量替换为真实的、人类可读的值之后翻译字符串,那么您将获得更好的翻译。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文