US Tray Menu 2: Language support

From The Uniform Server Wiki
Jump to navigation Jump to search

 

UniServer 5-Nano
US Tray Menu 2.
UniServer 6-Carbo

US Tray Menu 2 Language support

Introduction

Real purpose of creating a new tray menu is multi-language support. WinBinder makes this easy. Our tray menu's configuration file is saved, as a UTF-8 file hence is inherently multilingual.

An interesting dilemma! Mentioned on the previous page are pop-up message boxes. These were hard coded with a specific language, even worse embedded within our scripts.

Other than being lazy there is no real excuse for this. Although it was not possible to implement on a old version of UniTray we now have a real opportunity to move forward. Uniform Server already has a structure in place for Apanel that supports a multilingual implementation.

This page looks at applying that solution to our new tray menu.

Example

Following small code extract demonstrates language support

Top

Hard coded example

The following code displays Apanel in the default browser. If Apache is not running and a user clicks menu item an alert pop-up is generated. Text displayed is hard coded within the nested if statments.

  //== Command cmd_apanel
  if($command_array[0] == "cmd_apanel"){          // Intercept command
      if(apache_running()){                       // Is Apache running
        $cmd = "php.exe  -n unitray_info.php 1" ; // Cmd to run hidden
        run_cmd_hidden($cmd);                     // Run command
      }
      else{                                       // No:
       $str =  "To enable and run this menu item\n";
       $str .= "please start Apache server.";
       wb_message_box (NULL, $str, "Unavailable");      
      }
   }
   //== End command cmd_apanel

String “str” can be translate and file saved in UTF-8 format.

This makes sense for a small number of strings. Translating a large number of strings scattered across several scripts is very inconvenient.

Top

Common language script

Solution is to relocate all strings into a common script. This removes tedium of finding all strings to be translate.

Common script contains a single array $TM (TrayMenu) with the following format:

<?php

$TM = array(
'title' => 'Uniform Server',
	
//-----------------------------------------------------------------------------
// Apanel Menu Item
//-----------------------------------------------------------------------------

'cmd_apanel-title' => 'Apanel Unavailable',
'cmd_apanel-str'   => '
要启用并运行此菜单项
请启动Apache服务器.',

//-----------------------------------------------------------------------------
// Add new sections as required
//-----------------------------------------------------------------------------

); //END 
?>

The above shows a small section original English version was:

//-----------------------------------------------------------------------------
// Apanel Menu Item
//-----------------------------------------------------------------------------

'cmd_apanel-title' => 'Apanel Unavailable',
'cmd_apanel-str'   => '
To enable and run this menu item
please start Apache server.',

Target language array

With the language script in place all that is required is to targets elements contained in the language array.

For example original script is modified to target strings in the language array as shown below:

  //== Command cmd_apanel
  if($command_array[0] == "cmd_apanel"){          // Intercept command
      if(apache_running()){                       // Is Apache running
        $cmd = "php.exe  -n unitray_info.php 1" ; // Cmd to run hidden
        run_cmd_hidden($cmd);                     // Run command
      }
      else{                                       // No:
       wb_message_box (NULL, $TM['cmd_apanel-str'], $TM['cmd_apanel-title']);       
      }
   }
   //== End command cmd_apanel

Language file location: UniServer\unicon\tray_menu_2\lang\English.php

Top


Implementation

OK! That file (English.php) would be hard coded making it difficult for language switching at a later date.

Instead of using file English.php use a generic name such as lang.php. Hard coding this has the advantage that any file translated can be renamed to lang.php.

Advantage we have file English.php for reference, translators can copy this and save with an appropriate name.

Switching is easy, delete file lang.php copy and rename one of the translated files to lang.php. This can be done either manually or; automatically by providing a menu with a list of language options.

Top

Summary

Adding language support is not difficult especially at the coding stage.

Essentially that covers modifications required to convert our static menu into a dynamic version.

Next page is a slight digression if covers dreaded command window pop-ups

Top