Jump to content
Larry Ullman's Book Forums

Chapter 14 Transliteration And Locale


Recommended Posts

I have a couple of questions.

 

First, in script 14.4 transliteration does not work. Namely, the function str_translitearte() is undefined. I checked on the web and it looks like this function does not exist. I updated my library to include internationalization package and so far I had no problems (even with Collator) but this function is nevertheless undefined. In php manual I was able to find transliterator_transliterate() function but it looks like this function is in its development, for no arguments and implementations are given. Can you give some advise on how I can have the function to work? My php version is 5.3.8.

 

Second, in script 14.5 locales, strtotitle() and date_format_locale() are undefined. As I understand these functions did not come to existence yet since no PHP 6 is released. However, I tried to follow advise on Book's sidebar "Locales in PHP 5" and used a setlocale() function, which did not work as well. Is there an alternative solution? Thank you in advance.

Link to comment
Share on other sites

This is a change from the version of PHP6 I used in writing the book, and PHP 6 was never released, to what got put into PHP5.3. The str_transliterate() function has been replaced with the Transliterate class: http://us2.php.net/manual/en/class.transliterator.php You can see an example of its transliterate() method here: http://us2.php.net/manual/en/transliterator.transliterate.php

 

I believe date_format_locale() functionality can now be accomplished using the IntlDateFormatter class: http://us2.php.net/manual/en/class.intldateformatter.php

 

Let me know if you have problems working with either and I can try to find the time to provide specific code.

Link to comment
Share on other sites

I still vaguely understand how to implement transliterator method. I guess, I have to construct a transliterator object first but I don't know how to perform this task. As for the locale script, I got it to work without errors but it does not show any other locales except for the default one. Here's what I've tried so far:

 

<?php

 

//Set the default timezone:

date_default_timezone_set('UTC');

 

//Need a date object:

//$d = new DateTime();

 

//Create a list of locales:

$locales = array ('en_US', 'fr_FR', 'es_BO', 'zh_Hans_CN', 'ru_RU',

'el_GR', 'is_IS');

 

//Print the date in each locale:

foreach ($locales as $locale){

 

//Set the locale:

locale_set_default($locale);

 

//Print the date:

setlocale(LC_ALL, $locale);

echo "<p>$locale: ".

strftime('%A, %#d %B %Y'). "</p>\n";

 

}

 

?>

Link to comment
Share on other sites

  • 1 month later...

Terribly, terribly sorry for the delay on this. I've been wrapping up a book (way behind schedule) that I just finished two days ago. Then I had to install a custom version of PHP. As for the transliteration, I just posted an explanation here: http://www.larryullm...-using-php-538/

 

I'll also post a longer discussion of this on my blog in a couple of days.

 

As for the locales for the date formatting, here's the solution:

 

// Set the default timezone:
date_default_timezone_set('UTC');

// Need a date object:
$d = new DateTime();

// Create a list of locales:
$locales = array('en-US', 'fr-FR', 'es-BO', 'zh-Hans-CN', 'ru-RU', 'el-GR', 'is-IS');

// Print the date in each locale:
foreach ($locales as $locale) {

// Create a date format object:
$fmt = datefmt_create($locale, IntlDateFormatter::LONG, IntlDateFormatter::SHORT);


// Print the date:
echo "<p>$locale: " . datefmt_format($fmt, $d) . "</p>\n";
}

 

Note that the locales use hyphens instead of underscores. And this does require PHP 5.3 or greater. Let me know if you have any questions. Apologies again for the delay!

Link to comment
Share on other sites

Just a late reply here to. Using an iterator object is not efficient unless you work with large collections of objects. (N > 1000) traverse an array normally instead If possible.

 

Php has such amazing array possibilities. Hash takles? That's an associative array! NullPointerExceptions? Never heard of. :) stacks or queques? Array_push/pop() and normal array!

 

Php is really sweet sometimes. Give me return type hinting, and pretty please base type parameter hinting... I promise i'll never change language again!

Link to comment
Share on other sites

Thanks for your post. Out of curiosity, why do you mention that thing about iterator objects?

 

As for return type hinting and parameter hinting, I'm not an expert on designing languages, but I don't know how that'd be possible in a weakly typed language.

Link to comment
Share on other sites

Just for efficiency. If possible, means without to much hassle. If you really need an iterator, of course you should use one.

 

I'm thinking the same way as with parameter hinting. You are allowed to specify an interface or a class there. I would really like the same possibility with methods. It was discussed for PHP 6, if I'm not mistaken. :)

Link to comment
Share on other sites

 Share

×
×
  • Create New...