none
How make the intelligent translate and switch language all the form , Windows form C# RRS feed

  • Question

  • Dear master,

    any one have idea building about the intelligent translate and switch language , Windows form C#

    I want make windows form like this below

    https://imgur.com/BRJjicj

    please suggest me

    thank you


    • Edited by Adisone Thursday, March 26, 2020 7:54 AM add picture to sample
    Thursday, March 19, 2020 8:38 AM

All replies

  • Hi Adisone,

    Thank you for posting here.

    If you want to modify the project language at runtime, you can refer to the following steps.

    1. After opening the form, set its "Localizable" property to True.

    2. Add a control, such as label, set the text to Hello, and save.

    3. Change the language attribute of the form to French, change the text of the label to "bonjour", and save.

    At this time, there will be two files with the extension ".resx" under the project properties.

    4. Change the language back to default.

    5. Add a combobox for testing, then use the following code.

      private void Form1_Load(object sender, EventArgs e)
            {
                comboBox1.Items.Add("English");
                comboBox1.Items.Add("French");
                comboBox1.SelectedIndex = 0;
            }
    
            private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                if (comboBox1.SelectedItem.ToString() == "English")
                {
                    ChangeLanguage("en");
                }
                else if (comboBox1.SelectedItem.ToString() == "French")
                {
                    ChangeLanguage("fr-FR");
                }
               
            }
    
            private void ChangeLanguage(string lang)
            {
                foreach (Control c in this.Controls)
                {
                    ComponentResourceManager resources = new ComponentResourceManager(typeof(Form1));
                    resources.ApplyResources(c, c.Name, new CultureInfo(lang));
                }
            }

    Result:

    Hope this could be helpful.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, March 19, 2020 10:07 AM
  • thank you for your suggestion,

    but I want to create one form that any user can input or edit or add  language , do you have any idea about this point?

    Thursday, March 19, 2020 1:57 PM
  • Hi Adisone,

    In my opinion, to develop a multi-language version of the application, developers need to prepare in advance.

    I mean, according to the above steps to prepare various versions before publishing, what language the user needs, we will provide the corresponding .resx file to him.

    Of course, this is not the only solution.

    Developers can also use httpclient to access google api to get various languages.

    For developers, this solution may be simpler, but I don't think it is good.

    Limited to user networks, translation may be slow;

    Limited to machine translation, sometimes the accuracy will be lower, which will bring a bad experience to users.

    I will provide you with the corresponding code examples, and you will decide which one to use.

      private void button1_Click(object sender, EventArgs e)
            {
                string url = String.Format
        ("https://translate.googleapis.com/translate_a/single?client=gtx&sl={0}&tl={1}&dt=t&q={2}",
         "en", "FR", Uri.EscapeUriString(label1.Text));
                HttpClient httpClient = new HttpClient();
                string result = httpClient.GetStringAsync(url).Result;
    
                var jsonData = JsonConvert.DeserializeObject<List<dynamic>>(result);
                var translationItems = jsonData[0];
                string translation = "";
    
                foreach (object item in translationItems)
                {
                    IEnumerable translationLineObject = item as IEnumerable;
                    IEnumerator translationLineString = translationLineObject.GetEnumerator();
                    translationLineString.MoveNext();
                    translation += string.Format(" {0}", Convert.ToString(translationLineString.Current));
                }
                if (translation.Length > 1) { translation = translation.Substring(1); };
                label1.Text = translation;
            }

    Country abbreviation

    There are also some other products for software localization, such as LingoHub and Lingobit Localizer, etc. You can also choose to use these tools to simplify development.

    Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, March 20, 2020 3:04 AM
  • but I want to create one form that any user can input or edit or add  language , do you have any idea about this point?

    I have one idea. Take a look at the .resx file that is generated by the designer when you add a language at design time. You will observe that it is an XML file that contains a code for each item that has to be translated along with the translation. You can add a new language by copying an existing resource file, renaming it (for example, if you have french as in the example above, copy Form1.fr-FR.resx into Form1.es-ES.resx) and then changing the translations inside (which you can easily do from your program since it is a plain simple XML file). Once you have the modified file, you can compile it into a DLL (you can do this at runtime by invoking resgen.exe and al.exe). Then place the DLL in the binaries folder for your program, and the program will be able to switch into the language in the dll by using the same call to the resource manager that was proposed in an earlier response.
    Saturday, March 21, 2020 11:57 AM
    Moderator
  • thank you for suggest, it is good idea too
    Monday, March 23, 2020 8:43 AM
  • this is good idea , I like it !
    I have  a idea  , i will do  success my part by translate any control in form then create one form that can replace value in file ".resx", how do you think , this is possible way?

    Monday, March 23, 2020 8:51 AM
  • Hi Adisone,

    Yes, this is a viable idea.

    What you need to do is open form2 in form1, load the resx file, modify the loaded file according to the values entered in form2 and save it as a new file, and add this file to the project.

    Then add the new language to the combobox of form1.

    Here is some code that modifies the value of the form1 control from form2.

    Form1:

            public string Label1Text
            {
                get { return this.label1.Text; }
                set { this.label1.Text = value; }
            }
    
            public void AddLanguage(string language)
            {
                this.comboBox1.Items.Add(language);
            }
            private void button1_Click(object sender, EventArgs e)
            {
                new Form2(this).ShowDialog();
            }

    Form2:

            Form1 CalledFrom;
            public Form2(Form1 viaParameter) 
            {
                InitializeComponent();
                CalledFrom = viaParameter;
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                addNewResx();
                CalledFrom.Label1Text = this.textBox1.Text;
                CalledFrom.AddLanguage(textBox2.Text);
    
                this.Close();
            }
            public void addNewResx() 
            {
                //
            }

    This is just a rough idea, there are more details you need to complete yourself.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, March 24, 2020 5:44 AM
  • I want to design like this below


    • Edited by Adisone Thursday, March 26, 2020 7:53 AM currupt picture
    Thursday, March 26, 2020 7:52 AM
  • Hi Adisone,

    This form is a bit like what I call form2.

    The default language is displayed on the left, and the user enters the corresponding new language on the right.

    After the user clicks save, create a new resx file and load it into the program.

    Microsoft provides documentation for using .resx files programmatically, you can take a look.

    Create resource files for .NET apps

    Working with .resx files programmatically

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, March 27, 2020 2:49 AM
  • Hi,

    Has your issue been resolved?

    If so, please click "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Thursday, April 2, 2020 8:25 AM