• Artigos
  • Projetos
  • Download
  • Docs
  • Enviar Artigo
  • ?

Aplicação Multilang Com Glade e O.O

03/12/2006  Fernando Henrique Correa  Ler e Comentar

Quando tive te fazer uma aplicação dessas multilanguage para Web foi bem simples. Uma questão de só setar arrays ou constantes e estava tudo resolvido.

Para trazer essa simplicidade para o PHP-GTK2 dive de garimpar um pouco as APIs do GLADEXML.

O GladeXml tem um metodo estático para receber um buffer, desta forma pude traduzir com o str_replace as tags implementadas na interface do glade.

O resultado é bastante satisfatório, só pecando um pouco no quesito velocidade. Para isso aconcelho que um cache seja feito para a tradução de aplicações grandes.

Bom divertimento,
Abraços[]'s

--Aplicação --
<?php

/**
 * Dicionários
 */
$dic = array(
    
'pt_br' => array(
        
'#NOME#' => 'Nome',
        
'#TELEFONE#' => 'Telefone',
        
'#ENVIAR#' => '_Enviar'
    
),
    
'en' => array(
        
'#NOME#' => 'Name',
        
'#TELEFONE#' => 'Telephone',
        
'#ENVIAR#' => 'S_end'        
    
)
);

/**
 * Main
 */
class Main{
    
    private 
$dic_lang;
    
    public function 
__construct($dic_lang){
        
$this->dic_lang = $GLOBALS['dic'][$dic_lang];        
        
$this->buildInterface();
    }
    
    private function 
buildInterface(){
        
$glade_buffer = $this->translate('./multilang/multilang.glade');
        
$glade = GladeXml::new_from_buffer($glade_buffer);
    }

    private function 
translate($glade_file){
        
        if(
file_exists($glade_file)){
            
$fb = fopen($glade_file,'r');
            
$glade_file_buffer = fread($fb, filesize($glade_file));
            
fclose($fb);        
            
            if(
sizeof($this->dic_lang)>0){
                    foreach(
$this->dic_lang as $tranduzir => $traducao){
                           
$glade_file_buffer = str_replace($tranduzir, $traducao, $glade_file_buffer);
                    }
            }        
            return 
$glade_file_buffer;
        }
        else{
            return 
false;
        }
    }
    
}

new 
Main('');
new 
Main('pt_br');
new 
Main('en');
gtk::main();
?>

--XML do Glade--
<?xml version="1.0" standalone="no"?> <!--*- mode: xml -*-->
<!DOCTYPE glade-interface SYSTEM "http://glade.gnome.org/glade-2.0.dtd">

<glade-interface>

<widget class="GtkWindow" id="window">
  <property name="width_request">238</property>
  <property name="height_request">106</property>
  <property name="visible">True</property>
  <property name="title" translatable="yes">Fernando H.Correa</property>
  <property name="type">GTK_WINDOW_TOPLEVEL</property>
  <property name="window_position">GTK_WIN_POS_NONE</property>
  <property name="modal">False</property>
  <property name="resizable">True</property>
  <property name="destroy_with_parent">False</property>
  <property name="decorated">True</property>
  <property name="skip_taskbar_hint">False</property>
  <property name="skip_pager_hint">False</property>
  <property name="type_hint">GDK_WINDOW_TYPE_HINT_NORMAL</property>
  <property name="gravity">GDK_GRAVITY_NORTH_WEST</property>
  <property name="focus_on_map">True</property>
  <property name="urgency_hint">False</property>

  <child>
    <widget class="GtkTable" id="table1">
      <property name="border_width">5</property>
      <property name="visible">True</property>
      <property name="n_rows">3</property>
      <property name="n_columns">2</property>
      <property name="homogeneous">False</property>
      <property name="row_spacing">10</property>
      <property name="column_spacing">2</property>

      <child>
    <widget class="GtkLabel" id="label1">
      <property name="visible">True</property>
      <property name="label" translatable="yes">#NOME#:</property>
      <property name="use_underline">False</property>
      <property name="use_markup">False</property>
      <property name="justify">GTK_JUSTIFY_LEFT</property>
      <property name="wrap">False</property>
      <property name="selectable">False</property>
      <property name="xalign">0</property>
      <property name="yalign">0.5</property>
      <property name="xpad">0</property>
      <property name="ypad">0</property>
      <property name="ellipsize">PANGO_ELLIPSIZE_NONE</property>
      <property name="width_chars">-1</property>
      <property name="single_line_mode">False</property>
      <property name="angle">0</property>
    </widget>
    <packing>
      <property name="left_attach">0</property>
      <property name="right_attach">1</property>
      <property name="top_attach">0</property>
      <property name="bottom_attach">1</property>
      <property name="x_options">fill</property>
      <property name="y_options"></property>
    </packing>
      </child>

      <child>
    <widget class="GtkLabel" id="label2">
      <property name="visible">True</property>
      <property name="label" translatable="yes">#TELEFONE#:</property>
      <property name="use_underline">False</property>
      <property name="use_markup">False</property>
      <property name="justify">GTK_JUSTIFY_LEFT</property>
      <property name="wrap">False</property>
      <property name="selectable">False</property>
      <property name="xalign">0</property>
      <property name="yalign">0.5</property>
      <property name="xpad">0</property>
      <property name="ypad">0</property>
      <property name="ellipsize">PANGO_ELLIPSIZE_NONE</property>
      <property name="width_chars">-1</property>
      <property name="single_line_mode">False</property>
      <property name="angle">0</property>
    </widget>
    <packing>
      <property name="left_attach">0</property>
      <property name="right_attach">1</property>
      <property name="top_attach">1</property>
      <property name="bottom_attach">2</property>
      <property name="x_options">fill</property>
      <property name="y_options"></property>
    </packing>
      </child>

      <child>
    <widget class="GtkEntry" id="entry1">
      <property name="visible">True</property>
      <property name="can_focus">True</property>
      <property name="editable">True</property>
      <property name="visibility">True</property>
      <property name="max_length">0</property>
      <property name="text" translatable="yes"></property>
      <property name="has_frame">True</property>
      <property name="invisible_char">•</property>
      <property name="activates_default">False</property>
    </widget>
    <packing>
      <property name="left_attach">1</property>
      <property name="right_attach">2</property>
      <property name="top_attach">0</property>
      <property name="bottom_attach">1</property>
      <property name="y_options"></property>
    </packing>
      </child>

      <child>
    <widget class="GtkEntry" id="entry2">
      <property name="visible">True</property>
      <property name="can_focus">True</property>
      <property name="editable">True</property>
      <property name="visibility">True</property>
      <property name="max_length">0</property>
      <property name="text" translatable="yes"></property>
      <property name="has_frame">True</property>
      <property name="invisible_char">•</property>
      <property name="activates_default">False</property>
    </widget>
    <packing>
      <property name="left_attach">1</property>
      <property name="right_attach">2</property>
      <property name="top_attach">1</property>
      <property name="bottom_attach">2</property>
      <property name="y_options"></property>
    </packing>
      </child>

      <child>
    <widget class="GtkButton" id="button1">
      <property name="visible">True</property>
      <property name="can_focus">True</property>
      <property name="label" translatable="yes">#ENVIAR#</property>
      <property name="use_underline">True</property>
      <property name="relief">GTK_RELIEF_NORMAL</property>
      <property name="focus_on_click">True</property>
    </widget>
    <packing>
      <property name="left_attach">1</property>
      <property name="right_attach">2</property>
      <property name="top_attach">2</property>
      <property name="bottom_attach">3</property>
      <property name="x_options">fill</property>
      <property name="y_options"></property>
    </packing>
      </child>
    </widget>
  </child>
</widget>

</glade-interface>




Comentários

  aplicaçao multilang 

Gostei muito do artigo. Voce bem que poderia se aprofundar um pouquinho mais.
Tenho uma duvida intrigante. Seque que executamos aplicativos feitos em glade, apesar de encerra-los, parece que eles continuam na memoria a nao ser que mandemos gtk::main_quit(). O caso ocorreu com o escript acima.

No todo, seu script ta blz. Acho que ajudou muito e a muitos programadores.

J.Elias
Xambioa - To

  Enviado por José Elias em 2007-08-26  

 Adicionar Comentário
 login
 Senha
 Título
 Comentário

Livros




    Outros

    Busque conteúdo no site
    Participe de nosso grupo no google

    [Grupo]  [Arquivos] [Mapa Usuários]

    Google

    Parceiros

 
Designed by Wolfgang Bartelme Designed by Wolfgang Bartelme

© 2006 Wordpress Themes | Theme (Not so) Fresh
XHTML CSS RSS