Developers Archive for February, 2008

Internationalization and Localization with PHP

Internationalization and Localization with PHP Thursday, February 28th, 2008

While everyone who programs in PHP has to learn some English eventually to get a handle on its function names and language constructs, PHP can create applications in just about any human language. Some applications need to be used by speakers of many different languages. PHP’s internationalization and localization support makes it easier to make an application written for French speakers useful for German speakers.

Internationalization (often abbreviated I18N–there are 18 letters between the first “i” and the last “n”) is the process of taking an application designed for just one locale and restructuring it so that it can be used in many different locales. Localization (often abbreviated L10N–there are 10 letters between the first “l” and the “n”) is the process of adding support for a new locale to an internationalized application.

Localizing different kinds of content requires different techniques. This article covers an object-oriented method for localizing plain text messages and images. The PHP Cookbook contains additional recipes for dates, times, and currency. There are also recipes on using GNU gettext and other I18N and L10N topics.

Locales

A locale is a group of settings that describe text formatting and language customs in a particular area of the world. A locale name generally has three components. The first, an abbreviation that indicates a language, is mandatory. For example, “en” stands for English and “pt” for Portuguese. An optional country specifier comes next, after an underscore, to distinguish between different versions of the same language spoken in different countries. For example, “en_US” and “en_GB” specify U.S. and British English respectively, while “pt_BR” and “pt_PT” identify Brazilian and Portugese Portuguese. Finally, after a period, comes an optional character-set specifier. Taiwanese Chinese using the Big5 character set is encoded as “zh_TW.Big5“. Note that while most locale names follow these conventions, some don’t.

Message Catalog

To incorporate I18N support into your program, maintain a message catalog of words and phrases and retrieve the appropriate string from the message catalog before printing it. Here’s a simple message catalog with foods in American and British English and a function to retrieve words from the catalog:

<?php
$messages = array (
    ‘en_US’=> array(
       ‘My favorite foods are’ =>
           ‘My favorite foods are’,
       ‘french fries’ => ‘french fries’,
       ‘biscuit’ => ‘biscuit’,
       ‘candy’ => ‘candy’,
       ‘potato chips’ => ‘potato chips’,
       ‘cookie’ => ‘cookie’,
       ‘corn’ => ‘corn’,
       ‘eggplant’ => ‘eggplant’
    ),

    ‘en_GB’=> array(
        ‘My favorite foods are’ =>
            ‘My favourite foods are’,
        ‘french fries’ => ‘chips’,
        ‘biscuit’ => ’scone’,
        ‘candy’ => ’sweets’,
        ‘potato chips’ => ‘crisps’,
        ‘cookie’ => ‘biscuit’,
        ‘corn’ => ‘maize’,
        ‘eggplant’ => ‘aubergine’
    )
);

function msg($s) {
    global $LANG;
    global $messages;
   
    if (isset($messages[$LANG][$s])) {
        return $messages[$LANG][$s];
    } else {
        error_log(”l10n error:LANG:” .
            “$lang,message:’$s’”);
    }
}
?>

Null-Coalescing Operator ( ?? )

Null-Coalescing Operator ( ?? ) Thursday, February 28th, 2008

If tempFileName is not null, fileName = tempFileName, else fileName = “Untitled“।

This can now be abbreviated as follows using the Null-Coalescing Operator:

string fileName = tempFileName ?? “Untitled”;
The logic is the same। If tempFileName is not null, fileName = tempFileName, else fileName = “Untitled“.

The Null-Coalescing Operator comes up a lot with nullable types, particular when converting from a nullable type to its value type:

int? count = null;

int amount = count ?? default(int);

Since count is null, amount will now be the default value of an integer type ( zero )।

These Conditional and Null-Coalescing Operators aren’t the most self-describing operators :) ,
but I do love programming in C#!

C# 2.0 Anonymous Methods

C# 2.0 Anonymous Methods Thursday, February 28th, 2008

C# 2.0 provides a new feature called Anonymous Methods, which allow you to create inline un-named ( i.e. anonymous ) methods in your code, which can help increase the readability and maintainability of your applications by keeping the caller of the method and the method itself as close to one another as possible. This is akin to the best practice of keeping the declaration of a variable, for example, as close to the code that uses it.
Here is a simple example of using an anonymous method to find all the even integers from 1…10:
private int[] _integers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

int[] evenIntegers = Array.FindAll(_integers, delegate(int integer)
{
return (integer%2 == 0);
}
);

The Anonymous Method is:
delegate(int integer)
{
return (integer%2 == 0);
}

which is called for each integer in the array and returns either true or false depending on if the integer is even।

If you don’t use an anonymous method, you will need to create a separate method as such:
private int[] _integers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int[] evenIntegers = Array.FindAll(_integers, IsEven);
private bool IsEven(int integer)
{
return (integer%2 == 0);
}

When you have very simple methods like above that won’t be reused, I find it much more elegant and meaningful to use anonymous methods. The code stays closer together which makes it easier to follow and maintain.
Here is an example that uses an anonymous method to get the list of cities in a state selected in a DropDownList ( called States ):
List citiesInFlorida =cities.FindAll(delegate(City city)
{
return city.State.Name.Equals(States.SelectedValue);
}
);

You can also use anonymous methods as such:
button1.Click +=
delegate
{
MessageBox.Show(”Hello”);
};

which for such a simple operation doesn’t “deserve“ a separate method to handle the event.
Other uses of anonymous methods would be for asynchronous callback methods, etc.
Anonymous methods don’t have the cool factor of Generics, but they do offer a more expressive in-line approach to creating methods that can make your code easier to follow and maintain.


All material @ copyrighted by chrisranjana.com. If you want to link to this article you are welcome to do so. Unauthorized publication is strictly prohibited. This developer tutorial website contains articles by Php programmers , Software developers, Mysql programmers and asp c# programmers. This website also contains ajax tutorials and advanced mysql sql stored procedures and functions tutorials and sample codes.