Sample Code 62: How to display calendar in your native language?
Written by kksou   
Monday, 16 October 2006
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-gtk2\ext. 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   
19   
20   
21   
22   
23   
24   
25   
26   
27   
28   
29   
30   
31   
32   
33   
34   
35   
36   
37   
38   
39   
40   
41   
42   
43   
44   
45   
46   
47   
48   
49   
50   
51   
52   
53   
54   
55   
56   
57   
58   
59   
60   
61   
62   
<?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);
  • Note that this is only 70% of the sample code. You have to be a registered member to see the entire sample code. Please login or register.
  • Registration is free and immediate.
  • Have some doubt about the registration? Please read this forum article.
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?

User reviews

There are no user reviews yet.

Note: You have to be a registered member to leave a comment. Free registration here.

 
< Prev   Next >

Copyright © 2006-2008. kksou.com. All Rights Reserved