How to set language in other page once we did the changes in main page

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • erag
    New Member
    • Sep 2007
    • 2

    How to set language in other page once we did the changes in main page

    Hi im newbie to java, and i want to set the language change according to main page..how to create it?

    here is my coding:

    main page:


    [CODE=java] import org.eclipse.swt .SWT;
    import org.eclipse.swt .widgets.Button ;
    import org.eclipse.swt .widgets.Displa y;
    import org.eclipse.swt .widgets.Label;
    import org.eclipse.swt .widgets.Shell;
    import org.eclipse.swt .widgets.Text;
    import org.eclipse.swt .events.Selecti onAdapter;
    import org.eclipse.swt .events.Selecti onEvent;


    public class Trial {

    private static Text text;

    static String main = "main";
    static String jadual = "jadual";
    static String tarikh = "tarikh";
    static String cari = "cari";


    public static void main(String[] args) {
    final Local local = new Local();
    final Display display = Display.getDefa ult();
    final Shell shell = new Shell();
    shell.setSize(5 00, 375);
    shell.open();
    shell.setText(( "")+local.bahas a(main));

    final Button pressButton = new Button(shell, SWT.NONE);
    pressButton.set Text(("")+local .bahasa(jadual) );
    pressButton.set Bounds(64, 63, 77, 34);
    pressButton.add SelectionListen er(new SelectionAdapte r() {
    public void widgetSelected( SelectionEvent e) {

    Trial2 trial = new Trial2();
    trial.run(displ ay);
    }}
    );

    final Label tarikhLabel = new Label(shell, SWT.CENTER);
    tarikhLabel.set Text(("")+local .bahasa(tarikh) );
    tarikhLabel.set Bounds(44, 197, 49, 22);

    text = new Text(shell, SWT.BORDER);
    text.setBounds( 99, 194, 80, 25);


    final Button cariButton = new Button(shell, SWT.NONE);
    cariButton.setT ext(("")+local. bahasa(cari));
    cariButton.setB ounds(204, 197, 44, 23);
    cariButton.addS electionListene r(new SelectionAdapte r() {
    public void widgetSelected( SelectionEvent e) {
    Kalender2 kal = new Kalender2();
    kal.run(display );
    text.setText("" +kal.open());
    }}
    );

    final Button bmButton = new Button(shell, SWT.RADIO);
    bmButton.setTex t("bm");
    bmButton.setBou nds(75, 273, 83, 16);
    bmButton.setSel ection(true);
    bmButton.addSel ectionListener( new SelectionAdapte r() {
    public void widgetSelected( SelectionEvent e) {

    shell.setText(( "")+local.bahas a(main));
    pressButton.set Text(("")+local .bahasa(jadual) );
    tarikhLabel.set Text((""+local. bahasa(tarikh)) );
    cariButton.setT ext(("")+local. bahasa(cari));
    }
    }
    );



    final Button enButton = new Button(shell, SWT.RADIO);
    enButton.setTex t("en");
    enButton.setBou nds(230, 273, 83, 16);
    enButton.addSel ectionListener( new SelectionAdapte r() {
    public void widgetSelected( SelectionEvent e) {

    shell.setText(( "")+local.engli sh(main));
    pressButton.set Text(("")+local .english(jadual ));
    tarikhLabel.set Text((""+local. english(tarikh) ));
    cariButton.setT ext(("")+local. english(cari));
    }
    }
    );



    shell.layout();
    while (!shell.isDispo sed()) {
    if (!display.readA ndDispatch())
    display.sleep() ;
    }
    }
    }






    the 2nd page:

    import org.eclipse.swt .widgets.Displa y;
    import org.eclipse.swt .widgets.Shell;
    import org.eclipse.swt .SWT;
    import org.eclipse.swt .events.Selecti onAdapter;
    import org.eclipse.swt .events.Selecti onEvent;
    import org.eclipse.swt .widgets.Button ;


    public class Trial2 {

    public static Display display2;

    static String main2 = "main2";
    static String tutup = "tutup";

    static Local local = new Local();
    static Trial trial = new Trial();

    public void run(Display display2){

    final Shell shell = new Shell();
    shell.setSize(5 00, 375);

    //I want to set the language change according to first page
    shell.setText(( "")+);


    shell.open();

    final Button closeButton = new Button(shell, SWT.NONE);
    closeButton.set Text(("")+local .bahasa(tutup)) ;
    closeButton.set Bounds(176, 246, 44, 23);
    closeButton.add SelectionListen er(new SelectionAdapte r() {
    public void widgetSelected( SelectionEvent e) {
    shell.close();


    }}
    );

    shell.layout();
    while (!shell.isDispo sed()) {
    if (!display2.read AndDispatch())
    display2.sleep( );
    }

    }

    }



    Class for setting language:

    Local.java

    import java.util.*;


    public class Local {


    static Locale currentLocale;
    static ResourceBundle messages;



    public Object bahasa(String key){
    String p = key;
    currentLocale = new Locale("bm", "MS");
    messages = ResourceBundle. getBundle("Mess agesBundle",cur rentLocale);
    p=(String) messages.getStr ing(key);
    //messages.getStr ing("main");
    return p;
    }


    public Object english(String key){
    String en = key;
    currentLocale = new Locale("en","US A");
    messages = ResourceBundle. getBundle("Mess agesBundle", currentLocale);
    en = (String) messages.getStr ing(key);
    return en;
    }


    }

    [/CODE]

    now i want to mention the changes in "shell.setText( )" in Trial2.java. how to set it.Thanks in advance
    Last edited by r035198x; Sep 26 '07, 06:20 AM. Reason: added code tags
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    #2
    Originally posted by erag
    Hi im newbie to java, and i want to set the language change according to main page..how to create it?

    here is my coding:

    main page:
    [CODE=java] import org.eclipse.swt .SWT;
    import org.eclipse.swt .widgets.Button ;
    import org.eclipse.swt .widgets.Displa y;
    import org.eclipse.swt .widgets.Label;
    import org.eclipse.swt .widgets.Shell;
    import org.eclipse.swt .widgets.Text;
    import org.eclipse.swt .events.Selecti onAdapter;
    import org.eclipse.swt .events.Selecti onEvent;


    public class Trial {

    private static Text text;

    static String main = "main";
    static String jadual = "jadual";
    static String tarikh = "tarikh";
    static String cari = "cari";


    public static void main(String[] args) {
    final Local local = new Local();
    final Display display = Display.getDefa ult();
    final Shell shell = new Shell();
    shell.setSize(5 00, 375);
    shell.open();
    shell.setText(( "")+local.bahas a(main));

    final Button pressButton = new Button(shell, SWT.NONE);
    pressButton.set Text(("")+local .bahasa(jadual) );
    pressButton.set Bounds(64, 63, 77, 34);
    pressButton.add SelectionListen er(new SelectionAdapte r() {
    public void widgetSelected( SelectionEvent e) {

    Trial2 trial = new Trial2();
    trial.run(displ ay);
    }}
    );

    final Label tarikhLabel = new Label(shell, SWT.CENTER);
    tarikhLabel.set Text(("")+local .bahasa(tarikh) );
    tarikhLabel.set Bounds(44, 197, 49, 22);

    text = new Text(shell, SWT.BORDER);
    text.setBounds( 99, 194, 80, 25);


    final Button cariButton = new Button(shell, SWT.NONE);
    cariButton.setT ext(("")+local. bahasa(cari));
    cariButton.setB ounds(204, 197, 44, 23);
    cariButton.addS electionListene r(new SelectionAdapte r() {
    public void widgetSelected( SelectionEvent e) {
    Kalender2 kal = new Kalender2();
    kal.run(display );
    text.setText("" +kal.open());
    }}
    );

    final Button bmButton = new Button(shell, SWT.RADIO);
    bmButton.setTex t("bm");
    bmButton.setBou nds(75, 273, 83, 16);
    bmButton.setSel ection(true);
    bmButton.addSel ectionListener( new SelectionAdapte r() {
    public void widgetSelected( SelectionEvent e) {

    shell.setText(( "")+local.bahas a(main));
    pressButton.set Text(("")+local .bahasa(jadual) );
    tarikhLabel.set Text((""+local. bahasa(tarikh)) );
    cariButton.setT ext(("")+local. bahasa(cari));
    }
    }
    );



    final Button enButton = new Button(shell, SWT.RADIO);
    enButton.setTex t("en");
    enButton.setBou nds(230, 273, 83, 16);
    enButton.addSel ectionListener( new SelectionAdapte r() {
    public void widgetSelected( SelectionEvent e) {

    shell.setText(( "")+local.engli sh(main));
    pressButton.set Text(("")+local .english(jadual ));
    tarikhLabel.set Text((""+local. english(tarikh) ));
    cariButton.setT ext(("")+local. english(cari));
    }
    }
    );



    shell.layout();
    while (!shell.isDispo sed()) {
    if (!display.readA ndDispatch())
    display.sleep() ;
    }
    }
    }






    the 2nd page:

    import org.eclipse.swt .widgets.Displa y;
    import org.eclipse.swt .widgets.Shell;
    import org.eclipse.swt .SWT;
    import org.eclipse.swt .events.Selecti onAdapter;
    import org.eclipse.swt .events.Selecti onEvent;
    import org.eclipse.swt .widgets.Button ;


    public class Trial2 {

    public static Display display2;

    static String main2 = "main2";
    static String tutup = "tutup";

    static Local local = new Local();
    static Trial trial = new Trial();

    public void run(Display display2){

    final Shell shell = new Shell();
    shell.setSize(5 00, 375);

    //I want to set the language change according to first page
    shell.setText(( "")+);


    shell.open();

    final Button closeButton = new Button(shell, SWT.NONE);
    closeButton.set Text(("")+local .bahasa(tutup)) ;
    closeButton.set Bounds(176, 246, 44, 23);
    closeButton.add SelectionListen er(new SelectionAdapte r() {
    public void widgetSelected( SelectionEvent e) {
    shell.close();


    }}
    );

    shell.layout();
    while (!shell.isDispo sed()) {
    if (!display2.read AndDispatch())
    display2.sleep( );
    }

    }

    }



    Class for setting language:

    Local.java

    import java.util.*;


    public class Local {


    static Locale currentLocale;
    static ResourceBundle messages;



    public Object bahasa(String key){
    String p = key;
    currentLocale = new Locale("bm", "MS");
    messages = ResourceBundle. getBundle("Mess agesBundle",cur rentLocale);
    p=(String) messages.getStr ing(key);
    //messages.getStr ing("main");
    return p;
    }


    public Object english(String key){
    String en = key;
    currentLocale = new Locale("en","US A");
    messages = ResourceBundle. getBundle("Mess agesBundle", currentLocale);
    en = (String) messages.getStr ing(key);
    return en;
    }


    }

    [/CODE]
    Originally posted by erag
    now i want to mention the changes in "shell.setText( )" in Trial2.java. how to set it.Thanks in advance
    Hi erag! Welcome to TSDN!

    How about adding a setLanguage method like this:
    [CODE=java]
    public class Trial2 {
    private String language = "english";
    ...
    public void setLanguage(Str ing language)
    {
    this.language = language;
    }

    public void run(Display display2){
    ...
    //I want to set the language change according to first page
    if (language.equal s("english"))
    shell.setText(" Hello!");
    else if (language.equal s("german"))
    shell.setText(" Hallo!");
    else if (language.equal s("french"))
    shell.setText(" Bonjour!");
    else ...
    ...
    }
    [/CODE]You could, if you don't have many languages, use a char to define the language and use switch to set the Text correctly (because char is a basic Type, so you can use switch, but String isn't).

    Greetings,
    Nepomuk

    Comment

    • erag
      New Member
      • Sep 2007
      • 2

      #3
      Thanks nepomuk but i still have problems....the thing is i want to know whether if i select a button from one class file n want to get that selection for the other class file, can do like that? it is because when i run the application i want to set the language using button in main class n when i open the other class, it automatically
      set language followed by the button selection from other class..
      can i do that in java swt? do i hv to import any library?
      PLEASE answer coz i have no idea at all....THANKS.. ..

      Comment

      Working...