Solved

How to translate from one language to another using java

Posted on 2013-11-24
11
9,188 Views
Last Modified: 2016-09-01
I want to write a core java program that converts the data from one langualge to another language.for example
English to French,french to German, etc..
for this i have tried with Google Translator API for Java, but this is commertial, i am not able to run my program with out HttpReferrer and Key.
And i downloaded the webtranslator api, but i dont know how to use. So kindly provide the example to use this api.

or else, kindly provide, if you have any other possible solution.
0
Comment
Question by:prabhualla
  • 5
  • 5
11 Comments
 
LVL 4

Expert Comment

by:smeghammer
ID: 39674020
Hi,

Does the package you indicate have API documentation/javadocs? Can you provide a URL to the package download?
0
 

Author Comment

by:prabhualla
ID: 39674184
i think you are asking about Google Translator API.

GoogleAPI.setHttpReferrer(/*your http referrere*/);
 GoogleAPI.setKey(/*key*/); String text =
Translate.DEFAULT.execute("HELLO", Language.ENGLISH,
Language.GERMAN); System.out.println(text);

Open in new window


i downloaded the jar from

http://www.java2s.com/Code/Jar/g/Downloadgoogleapitranslatejava095jar.htm
0
 
LVL 4

Expert Comment

by:smeghammer
ID: 39674211
Hi,

You stated you did not want to use the Google API. The way your post was phrased indicated that "And i downloaded the webtranslator api" - which to me said that you downloaded a non-Google API. I did a quick search for 'webtranslator api java', and there appear to be quite a few out there. e.g.:


My question was basically which one did you pick?
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

Author Comment

by:prabhualla
ID: 39674300
Thanks for your response smeghammer.

Yes i tried the below one.

http://sourceforge.net/projects/webtranslator/files/JavaWebTranslator-0.2a/Java%20WebTranslator%200.2a%20%28Alpha%20Release%29/

but i don't know how to use this, Kindly provide any sample program to use this api.
0
 
LVL 4

Accepted Solution

by:
smeghammer earned 500 total points
ID: 39674452
Hi,

I just had a quick look at this - if you open the source project you will find a class called
FreeTranslationTranslatorTester.java

Open in new window

in the com.javanetworkframework.rb.tester package.

This test file runs through the various combinations that are supported and fires 'hello world' in various languages at the services used. You can easily test this by altering the english input to something else (line 38).

I did not go through the package in detail, but you should be able to get what you need using this test class as a starting point.

[edit]
I modified the example code to simplify and use just one to/from language:
Locale srcLoc = new Locale("en");
Locale dstLoc = new Locale("es"); //try fr, de, es
AbstractWebTranslator res = (AbstractWebTranslator) ResourceBundle.getBundle("com.javanetworkframework.rb.com.freetranslation.FreeTranslationTranslatorRB",dstLoc);
String output = "en2fr\n";
String textToTranslate = "What time is it in Spain? Can I have a bacon sandwich?";
output += textToTranslate + " --> " + res.getString(textToTranslate, srcLoc);

Open in new window

Try amending your input string and the 'to' language.

One thing I would note - the source files appear to be created in 2004 so I suspect that this package is not being developed any more.
0
 
LVL 4

Expert Comment

by:smeghammer
ID: 39674945
Here is a better GUI based example. I borrowed the tutorial from here as the basis for the GUI.

The download includes a complete source development package ('WebTranslator-all-0.2a.zip'). This can be unzipped and imported as a new project into Eclipse.  

I have a single class file and a reference to the main WebTranslator-bin-0.2a.jar file. I made a very basic textarea, language dropdown, button and associated listener. The listener just takes the value in the textarea and submits it with the appropriate to/from languages. The translated result is then placed in the textarea:
package guiTest;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JLabel;

import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Locale;
import java.util.ResourceBundle;

//this is the key import for the translation package:
import com.javanetworkframework.rb.util.AbstractWebTranslator;

public class app
{
  public static void main(String[] args)
 {
      new app();
  }

  public app()
  {
      JFrame guiFrame = new JFrame();
      
      //make sure the program exits when the frame closes
      guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      guiFrame.setTitle("Example GUI");
      guiFrame.setSize(300,250);
    
      //This will center the JFrame in the middle of the screen
      guiFrame.setLocationRelativeTo(null);
      
      //Options for the JComboBox 
      String[] toLanguage = {"fr", "de", "es"};
      
      //The first JPanel contains a JLabel and JCombobox
      final JPanel comboPanel = new JPanel();
      final JLabel comboLbl = new JLabel("Select 'to' language:");
      final JComboBox<Object> toLanguages = new JComboBox<Object>(toLanguage);
      
      comboPanel.add(comboLbl);
      comboPanel.add(toLanguages);

      final javax.swing.JTextArea textArea = new javax.swing.JTextArea();
      
      JButton transLateBtn = new JButton( "Translate!");
      
      //The ActionListener class is used to handle the
      //event that happens when the user clicks the button.
      //As there is not a lot that needs to happen we can 
      //define an anonymous inner class to make the code simpler.
      transLateBtn.addActionListener(new ActionListener()
      {
          @Override
          public void actionPerformed(ActionEvent event)
          {
                //submit the text in the textarea to an instance of the translator:
     		Locale srcLoc = new Locale("en");  //hardcoded in this example
    		Locale dstLoc = new Locale(toLanguages.getSelectedItem().toString()); //eg fr, de, es
    		AbstractWebTranslator res = (AbstractWebTranslator) ResourceBundle.getBundle(
    				"com.javanetworkframework.rb.com.freetranslation.FreeTranslationTranslatorRB",
    				dstLoc);
    		String output = "";
    		String textToTranslate = textArea.getText();
    		output = res.getString(textToTranslate, srcLoc);
    		textArea.setText(output);
          }
      });
      
      //The JFrame uses the BorderLayout layout manager.
      guiFrame.add(comboPanel, BorderLayout.NORTH);
      guiFrame.add(textArea,BorderLayout.CENTER);
      guiFrame.add(transLateBtn,BorderLayout.SOUTH);
      
      //make sure the JFrame is visible
      guiFrame.setVisible(true);
  }
} 

Open in new window

0
 

Author Comment

by:prabhualla
ID: 39682736
Thanks smeghammer,

i tried the below code as u mentioned  in above.

	Locale srcLoc = new Locale("en");
		Locale dstLoc = new Locale("es"); //try fr, de, es
		AbstractWebTranslator res = (AbstractWebTranslator) ResourceBundle.getBundle("com.javanetworkframework.rb.com.freetranslation.FreeTranslationTranslatorRB",dstLoc);
		String output = null;
		String textToTranslate = "hello";
		output = res.getString(textToTranslate, srcLoc);
		System.out.println(output);

Open in new window


but i am getting one applet, And i have choosen Allow, but i am not getting output, the applet keep on running, it is not giving any output.

please help me.
0
 

Author Comment

by:prabhualla
ID: 39682742
log file attached for your reference.
log
0
 
LVL 4

Expert Comment

by:smeghammer
ID: 39682974
Hi,

You did not mention applet in your original post. The example I provided is for a standard GUI application.

However, it could easily be converted to an applet or a command-line utility. The code I provided simply demonstrates how to use the LIBRARY, and not how to build an application or applet around it.

As I noted above, you need to use the ENTIRE example Eclipse project, NOT just the code I provided. That is just the front-end on top of the translation library. You originally asked this
And i downloaded the webtranslator api, but i dont know how to use. So kindly provide the example to use this api

And that is what I provided. Other posts by yourself suggest you are an experienced java programmer and I am not going to describe the basics of building an applet or how to use Eclipse - there are plenty of resources for that out there.

So:

Did you import the entire sample project ('WebTranslator-all-0.2a.zip')? into Eclipse
If so ,did you successfully run this sample project before using my code?
Did you create a new project and reference the webtranslator JAR? [build path/add external archives/]
Did you add the import statement to actually use the webtranslator package?
I am certainly not a java guru, so the first thing I did when I looked at this was to import the example project into Eclipse and figure out how it worked by running the PROVIDED example code and tracing through what happens. It was only when I had worked out roughly what was going on that I created my own simple project that used the library.
0
 

Author Closing Comment

by:prabhualla
ID: 39779401
Thanks
0
 

Expert Comment

by:Janki Gadhiya
ID: 41781020
Hi, smeghammer

I have used your code for language translation in my simple java program, but my execution gets stuck at this line "output += textToTranslate + " --> " + res.getString(textToTranslate, srcLoc);"

I think "res.getString(textToTranslate, srcLoc);"  is not returning any thing.

Can you suggest me any solution ??
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
jar file executable 12 52
table example 4 29
Error in @AspectJ Based AOP with Spring 2 13
Delphi: barcode reading on android platform 1 30
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question