062. How to display calendar in your native language?

Problem

In the article How to let user enter date with a popup calendar - Part 2?, you allow users to enter date through a popup calendar.

You would like to display the calendar in your native language as shown below:

How to display calendar in your native language?


Solution

Note that this has only been tested on winxp only. Not sure if it works on linux.

The method described here will work for both English and non-English winxp.

  1. First you need to go to Control Panel - Regional and Language Options - Advanced, and select your native language, as shown below. Let's take Russian as an example.
  2. Click <OK> and you will be prompted to reboot your system.

  3. Add setlocale(LC_ALL, 'rus') before creating the GtkCalendar widget.
    • For Russian, use 'rus'
    • For German, use 'deu'
    • For French, use 'fra'
    • For Portuguese (Brazilian), use 'ptb'
    • For Chinese (simplified), use 'chs'
    • For Chinese (traditional), use 'cht'
    • For Japanese, use 'jpn'
    • For Korean, use 'kor'

    For other countries, you may refer to: here.

  4. Then add the following 3 lines after creating the GtkCalendar widget.
  5.     $style = $this->calendar->style->copy();
        $style->font = new PangoFontDescription('Arial 12');
        $this->calendar->set_style($style);

Sample Code

Note: If you have installed php-gtk2 using Gnope Installer on Windows, and if running the sample code below gives you warning that the Symbolic names for keys (e.g. Gdk::KEY_Return) is not defined, you might want to update your php-gtk2 with the latest php-gtk2.dll available here. Simply download the php-gtk2.dll and replace the copy in the folder php-gtk2xt. The latest compilation has put in the Symbolic names for keys listed here.

1   
2   
3   
4   
5   
6   
7   
8   
9   
10   
11   
12   
13   
14   
15   
16   
17   
18   
20   
21   
22   
34   
35   
36   
37   
39   
41   
42   
43   
44   
45   
47   
48   
51   
52   
53   
54   
55   
56   
57   
58   
59   
60   
61   
62   
66   
71   
72   
73   
74   
75   
80   
82   
83   
84   
85   
88   
89   
91   
92   
100   
101   
102   
103   
104   
105   
106   
112   
113   
115   
119   
121   
122   
124   
126   
127   
128   
129   
131   
132   
133   
135   
136   
137   
138   
139   
140   
141   
142   
<?php
$window = new GtkWindow();
$window->set_size_request(400, 150);
$window->set_position(Gtk::WIN_POS_CENTER_ALWAYS);
$window->connect_simple('destroy', array('Gtk','main_quit'));
$window->add($vbox = new GtkVBox());

// display title
$title = new GtkLabel("Press F1 to choose a date");
$title->modify_font(new PangoFontDescription("Times New Roman Italic 10"));
$title->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("#0000ff"));
$title->set_size_request(-1, 40);
$vbox->pack_start($title, 0, 0);

$hbox = new GtkHBox();
$vbox->pack_start($hbox, 0, 0);
$hbox->pack_start(new GtkLabel("Select Date: "), 0, 0);
$hbox->pack_start($entry = new GtkEntry(), 0, 0);

// let us know when user press F1 key
$entry->connect('key-press-event', 'on_keypress', $entry);

function on_keypress($widget, $event, $entry) {
    // we only want F1. return all the rest
    if ($event->keyval!=Gdk::KEY_F1) return false;
    $selected_date = get_date();
    $entry->set_text($selected_date);
    $entry->grab_focus();
}

function get_date() {
    $getdate_dialog = new GetDate();
    $date = $getdate_dialog->calendar->get_date();
    $selected_date = 1+$date[1].'/'.$date[2].'/'.$date[0];
    return $selected_date;
}

class GetDate{

    var $calendar;

    function GetDate() {
        $dialog = new GtkDialog('Get Date', null, Gtk::DIALOG_MODAL);
        $dialog->set_position(Gtk::WIN_POS_CENTER_ALWAYS);
        $top_area = $dialog->vbox;
        $top_area->pack_start($hbox = new GtkHBox());

        setlocale(LC_ALL, 'chs'); // note 1
        $this->calendar = new GtkCalendar();
        $top_area->pack_start($this->calendar, 0, 0);

        // then add these 3 lines note 2
        $style = $this->calendar->style->copy();
        $style->font = new PangoFontDescription('Arial 12');
        $this->calendar->set_style($style);
        

        $this->calendar->connect('day-selected', array(&$this, 'on_select'), 'day-selected');
        $this->calendar->connect('month-changed', array(&$this, 'on_select'), 'month-changed');
        $dialog->connect('button-press-event', array(&$this, 'on_button_press'));

        $this->dialog = $dialog;
        $dialog->set_has_separator(false);
        $dialog->show_all();
        $dialog->run();
        $dialog->destroy();
    }

    // user selects a date or change a month
    function on_select($calendar, $signal_name) {
        if ($signal_name=='month-changed') {
            $this->month_changed = 1;
            return false;
        }
        $this->month_changed = 0;
        return false;
    }//

    function on_button_press($widget, $event) {
        if (!$this->month_changed) {
            $this->dialog->destroy(); // destroy only if user selects a date
        }
        $this->month_changed = 0;
    }

}

$window->show_all();
Gtk::main();
?>

Output

setlocale(LC_ALL, 'rus')

 

setlocale(LC_ALL, 'deu')

 

setlocale(LC_ALL, 'fra')

 

setlocale(LC_ALL, 'ptb')

 

setlocale(LC_ALL, 'chs') / setlocale(LC_ALL, 'cht')

 

setlocale(LC_ALL, 'jpn')

 

setlocale(LC_ALL, 'kor')

 

Explanation

  1. This sets the locale.
  2. Frankly speakly, I don't know why this works. Just happen to discover this while working on the example code for How to let user enter date with a popup calendar - Part 2?

Add comment


Security code
Refresh