import java.awt.GridLayout; import java.text.DateFormat; import java.text.MessageFormat; import java.text.NumberFormat; import java.util.Date; import java.util.Locale; import java.util.ResourceBundle; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.SwingConstants; public class Internationalisation2 extends JFrame { private JPanel contentPane; private JLabel paysLabel = new JLabel(); private JLabel dateLabel = new JLabel(); private JLabel tempsLabel = new JLabel(); private JLabel montantLabel = new JLabel(); private JTextField paysText = new JTextField(); private JTextField dateText = new JTextField(); private JTextField tempsText = new JTextField(); private JTextField montantText = new JTextField(); private ResourceBundle bundle; // on modifie la signature du constructeur pour qu’on puisse change les // langues dynamiquement public Internationalisation2(Locale locale) { // on ajoute un objet de définition de localisation "locale" bundle = ResourceBundle.getBundle("Internationalisation", locale); contentPane = (JPanel) this.getContentPane(); contentPane.setLayout(new GridLayout(4, 2)); Init(locale.getDisplayVariant()); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setResizable(false); setSize(293, 123); // on initialise les variable d’internationalisation setTitle(bundle.getString("PTitle")); paysLabel.setText(bundle.getString("PPays")); dateLabel.setText(bundle.getString("PDate")); tempsLabel.setText(bundle.getString("PTemps")); montantLabel.setText(bundle.getString("PMontant")); // on ajout des éléments relatives à la langues et le pays pour // mieux voir le résultat. DateFormat uneDate = DateFormat .getDateInstance(DateFormat.LONG, locale); DateFormat unTime = DateFormat.getTimeInstance(DateFormat.MEDIUM, locale); Object[] numero = new Object[] { NumberFormat.getCurrencyInstance( locale).format(1234.56) }; String montant = MessageFormat.format(bundle.getString("Addition"), numero); paysText.setText(locale.getISO3Country()); dateText.setText(uneDate.format(new Date())); tempsText.setText(unTime.format(new Date())); montantText.setText(montant); setVisible(true); } private void Init(String side) { if (side.equals("RIGHT")) { contentPane.add(paysText, null); paysText.setHorizontalAlignment(SwingConstants.RIGHT); contentPane.add(paysLabel, null); paysLabel.setHorizontalAlignment(SwingConstants.RIGHT); contentPane.add(dateText, null); dateText.setHorizontalAlignment(SwingConstants.RIGHT); contentPane.add(dateLabel, null); dateLabel.setHorizontalAlignment(SwingConstants.RIGHT); contentPane.add(tempsText, null); tempsText.setHorizontalAlignment(SwingConstants.RIGHT); contentPane.add(tempsLabel, null); tempsLabel.setHorizontalAlignment(SwingConstants.RIGHT); contentPane.add(montantText, null); montantText.setHorizontalAlignment(SwingConstants.RIGHT); contentPane.add(montantLabel, null); montantLabel.setHorizontalAlignment(SwingConstants.RIGHT); } else { contentPane.add(paysLabel, null); contentPane.add(paysText, null); contentPane.add(dateLabel, null); contentPane.add(dateText, null); contentPane.add(tempsLabel, null); contentPane.add(tempsText, null); contentPane.add(montantLabel, null); contentPane.add(montantText, null); } } public static void main(String args[]) { Locale locale = Locale.getDefault(); if (args.length == 3) locale = new Locale(args[0], args[1], args[2]); new Internationalisation2(locale); } }