Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Language translation

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Language translation

    Hi,
    I used the following code for language translation of text to other languages in java. when i run the code i couldnt get panel in the output. am using netbeans ide. also help me where i have to change in my code to translate my own text file. thanks. here is the code...

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.io.File;
    import java.io.IOException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
     
    import javax.swing.JOptionPane;
    import javax.swing.JRadioButtonMenuItem;
     
    import com.google.api.Files;
     
    /**
     *
     * @author rich.midwinter
     */
    public class TranslatorFrame extends javax.swing.JFrame {
     
        /**
             * Serial version UID.
             */
            private static final long serialVersionUID = 7916697355146649532L;
     
            private static final String REFERRER_PATH = System.getProperty("user.home") +"/.gtReferrer";
     
            private Language languageFrom = Language.FRENCH;
            private Language languageTo = Language.ENGLISH;
     
            /** Creates new form TranslatorFrame */
        public TranslatorFrame() throws IOException {
            initComponents();
            setLocationRelativeTo(null);
     
            String referrer = null;
     
            final File ref = new File(REFERRER_PATH);
            if (ref.exists()) {
                    referrer = Files.read(ref).trim();
            } else {
                    referrer = (String) JOptionPane.showInputDialog(this,
                                    "Please enter the address of your website.\n(This is just to help Google identify how their translation tools are used).",
                                    "Website address", JOptionPane.OK_OPTION);
                    Files.write(ref, referrer);
            }
     
            if (referrer.length() > 0) {
                    Translate.setHttpReferrer(referrer);
            } else {
                    System.exit(1);
            }
        }
     
        private void translate() {
            try {
                toTextArea.setText(Translate.translate(fromTextArea.getText().trim(), languageFrom, languageTo));
            } catch (Exception ex) {
                Logger.getLogger(TranslatorFrame.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
     
        /**
         * This method is called from within the constructor to
         * initialize the form.
         */
        private void initComponents() {
            buttonGroup1 = new javax.swing.ButtonGroup();
            buttonGroup2 = new javax.swing.ButtonGroup();
            jPanel2 = new javax.swing.JPanel();
            jScrollPane1 = new javax.swing.JScrollPane();
            fromTextArea = new javax.swing.JTextArea();
            jPanel3 = new javax.swing.JPanel();
            jScrollPane2 = new javax.swing.JScrollPane();
            toTextArea = new javax.swing.JTextArea();
            jMenuBar1 = new javax.swing.JMenuBar();
            jMenu1 = new javax.swing.JMenu();
            jMenuItem1 = new javax.swing.JMenuItem();
            jMenuTo = new javax.swing.JMenu();
            jMenuFrom = new javax.swing.JMenu();
     
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setTitle("Translator");
            getContentPane().setLayout(new javax.swing.BoxLayout(getContentPane(), javax.swing.BoxLayout.PAGE_AXIS));
     
            fromTextArea.setColumns(20);
            fromTextArea.setLineWrap(true);
            fromTextArea.setRows(5);
            fromTextArea.setWrapStyleWord(true);
            fromTextArea.addKeyListener(new java.awt.event.KeyAdapter() {
                public void keyPressed(java.awt.event.KeyEvent evt) {
                    fromTextAreaKeyPressed(evt);
                }
            });
            jScrollPane1.setViewportView(fromTextArea);
     
            javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
            jPanel2.setLayout(jPanel2Layout);
            jPanel2Layout.setHorizontalGroup(
                jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 309, Short.MAX_VALUE)
                    .addContainerGap())
            );
            jPanel2Layout.setVerticalGroup(
                jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 93, Short.MAX_VALUE)
                    .addContainerGap())
            );
     
            getContentPane().add(jPanel2);
     
            toTextArea.setColumns(20);
            toTextArea.setEditable(false);
            toTextArea.setLineWrap(true);
            toTextArea.setRows(5);
            toTextArea.setWrapStyleWord(true);
            jScrollPane2.setViewportView(toTextArea);
     
            javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
            jPanel3.setLayout(jPanel3Layout);
            jPanel3Layout.setHorizontalGroup(
                jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel3Layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 309, Short.MAX_VALUE)
                    .addContainerGap())
            );
            jPanel3Layout.setVerticalGroup(
                jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel3Layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 93, Short.MAX_VALUE)
                    .addContainerGap())
            );
     
            getContentPane().add(jPanel3);
     
            jMenu1.setText("File");
     
            jMenuItem1.setText("Exit");
            jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jMenuItem1ActionPerformed(evt);
                }
            });
            jMenu1.add(jMenuItem1);
     
            jMenuBar1.add(jMenu1);
     
            jMenuFrom.setText("From");
            jMenuTo.setText("To");
     
            for (final Language language : Language.values()) {
                    JRadioButtonMenuItem menuItem = new JRadioButtonMenuItem();
                    menuItem.setText(language.name());
                    if (language.equals(languageFrom)) {
                            menuItem.setSelected(true);
                    }
                    menuItem.addActionListener(new ActionListener() {
                            public void actionPerformed(final ActionEvent evt) {
                                    languageFrom = language;
                            }
                    });
                    buttonGroup1.add(menuItem);
                    jMenuFrom.add(menuItem);
     
                    if (language != Language.AUTO_DETECT) {
                            menuItem = new JRadioButtonMenuItem();
                            menuItem.setText(language.name());
                            if (language.equals(languageTo)) {
                                    menuItem.setSelected(true);
                            }
                            menuItem.addActionListener(new ActionListener() {
                                    public void actionPerformed(final ActionEvent evt) {
                                            languageTo = language;
                                    }
                            });
                            buttonGroup2.add(menuItem);
                            jMenuTo.add(menuItem);
                    }
            }
     
            jMenuBar1.add(jMenuFrom);
            jMenuBar1.add(jMenuTo);
     
            setJMenuBar(jMenuBar1);
     
            pack();
        }
     
        private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
            System.exit(0);
        }
     
        private void fromTextAreaKeyPressed(java.awt.event.KeyEvent evt) {
            if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
                translate();
                evt.consume();
            }
        }
     
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                                            new TranslatorFrame().setVisible(true);
                                    } catch (IOException e) {
                                            e.printStackTrace();
                                    }
                }
            });
        }
     
        // Variables declaration
        private javax.swing.ButtonGroup buttonGroup1;
        private javax.swing.ButtonGroup buttonGroup2;
        private javax.swing.JTextArea fromTextArea;
        private javax.swing.JMenu jMenu1;
        private javax.swing.JMenu jMenuFrom;
        private javax.swing.JMenu jMenuTo;
        private javax.swing.JMenuBar jMenuBar1;
        private javax.swing.JMenuItem jMenuItem1;
        private javax.swing.JPanel jPanel2;
        private javax.swing.JPanel jPanel3;
        private javax.swing.JScrollPane jScrollPane1;
        private javax.swing.JScrollPane jScrollPane2;
        private javax.swing.JTextArea toTextArea;
        // End of variables declaration   
    }


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Language translation

    For people to be able to compile this, they need the google api import. I don't have this to hand so please give as much information as possible.
    Are any errors generated?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. Natural language processing in java
    By nikki. in forum Java Theory & Questions
    Replies: 5
    Last Post: March 19th, 2012, 11:34 PM
  2. saving data in persian language in mysql db
    By java_cs in forum JDBC & Databases
    Replies: 2
    Last Post: January 19th, 2011, 12:26 PM
  3. What does SCOPE actually mean in java language?
    By wAdeski in forum Java Theory & Questions
    Replies: 3
    Last Post: December 1st, 2010, 10:44 AM
  4. Replies: 2
    Last Post: November 25th, 2010, 01:59 PM
  5. Replies: 5
    Last Post: September 6th, 2009, 04:39 AM