Click here to Skip to main content
Click here to Skip to main content

WP7 App Resources Translator using Bing services

By , 13 Oct 2010
 
I have to translate my AppResources from english to the other 4 languages available at the moment in WP7. So I decided to create a snippet to translate it, Of course simply change the TargetLanguage to get the translated section.
 
What you have to do is:
1.- Create the AppResources.es-ES.resx,AppResources.it-IT.resx and else in your WP7 app.
2.- Have an AppID for Bing Services.
3.- Add the following code to a WPF/SL app with the Bing Service reference.
4.- Copy from the first to the last from the resx.file like:
 
</data lang="xml">data name="alanguage" xml:space="preserve">
    <value>Answer Language</value>
  </data>
  <data name="animage" xml:space="preserve">
    <value>An image!</value>
  </data>
  <data name="bonus" xml:space="preserve">
    <value>BONUS </value>
  </data>
 
The Code:
 
String source;
        List<String> words;
        String TargetLanguage = "it";
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            source = textBox1.Text;
            List<String> input = (from prod in source.Split(new string[] { "<data ", "</data>" }, StringSplitOptions.None).ToList()
                                  where prod.Contains("name")
                                  select prod).ToList();
            words = new List<string>();
            Int32 odx, fdx = -1;
            input.ForEach(inp =>
            {
                odx = inp.IndexOf("<value>") + "<value>".Length;
                fdx = inp.IndexOf("</value>");
                words.Add(inp.Substring(odx, fdx - odx));
            });
            StringBuilder sb = new StringBuilder(" ");
            words.ForEach(s => sb.Append(String.Concat("| ", s, " ")));
            TranslateWord(sb.ToString(),"en",TargetLanguage,ProcessTranslated);
        }
        public static void TranslateWord(string source,string SourceL,string TargetL,Func<List<String>,bool> ProcessTranslated)
        {
            List<string> ret = new List<string>();
            SearchRequest request = new SearchRequest();
            request.Sources = new SourceType[] { SourceType.Translation };
            request.AppId = "YOURAPPID";
            request.Query = source;
            TranslationRequest tr = new TranslationRequest();
            tr.SourceLanguage = SourceL;
            tr.TargetLanguage = TargetL;
            request.Translation = tr;
            BingPortTypeClient client = new BingPortTypeClient();
            client.SearchCompleted += (s, e) =>
                            {
                                String result = e.Result.Translation.Results[0].TranslatedTerm;
                                ret = result.Split('|').ToList();
                                ret.ForEach(s2 =>
                                {
                                    if (s2.Length > 0 && s2[0] == ' ')
                                        s2 = s2.Trim();
                                });
                                ret.RemoveAt(0);
                                ProcessTranslated(ret);
                            };
            client.SearchAsync(request);
        }
        public bool ProcessTranslated(List<string> input)
        {
            if(input.Count == words.Count)
            {
                words.ForEach(w => source = source.Replace("<value>"+w, "<value>"+input[words.IndexOf(w)].Substring(1)));
            }
            textBox2.Text = source.Replace(" </value>","</value>");
            return true;
        }
 
And that's all, replace textBox2.Text the result in the .it-IT.resx and else. It may work in WPF and SL too.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Juan Pablo G.C.
Software Developer
Spain Spain
Member
I'm Electronic Engineer, I did my end degree project at Astrophysical Institute and Tech Institute. I'm HP Procurve AIS and ASE ,Microsoft 3.5 MCTS
I live in Canary Islands ,developing customized solutions
 
I'm developing with WPF4, SL4 MVVM, MVC3 Razor and WP7 projects, more info at my websites. Improving with Android and IOS.
 
Websites:
MemCards
Mareinsula


Take a look to my blog Juan Pablo G.C.
Mareinsula

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralI wouldn't use this in any real scenariomembertec-goblin18 Oct '10 - 11:23 
While technically it's quite cute, experience with automatic translation, particularly for short context-dependent terms like resource files is... let's say tragic. I mean you won't even get to 50% success.
GeneralRe: I wouldn't use this in any real scenariomemberJuan Pablo G.C.18 Oct '10 - 23:02 
You are right in a 70% I know English and Spanish and for a WP7 application I had to translate 2200 words and about 20 sentences. I had to review my xml generated (to take a look how good it was) and I can say that I was really surprised, medical words were translated perfect, compounds words too and trees, thinks not really obvius were great.
 
The other 30% or less it's what I had to check by hand, Thank you for the vote Smile | :)
Juan Pablo G.C.
Overrider Blog
 

GeneralRe: I wouldn't use this in any real scenariomembertec-goblin19 Oct '10 - 11:39 
I've seen many strange things in German, but where it gets really funny is Greek (or even worse, French to Greek, I think the translation uses English as an intermediate step) Poke tongue | ;-P
PS: The vote wasn't mine, I just found where do we vote for tips, I'm going right now.
GeneralRe: I wouldn't use this in any real scenariomemberJuan Pablo G.C.19 Oct '10 - 21:35 
I'm agree with you I think English have to be always the source.
Juan Pablo G.C.
Overrider Blog
 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 13 Oct 2010
Article Copyright 2010 by Juan Pablo G.C.
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid