tailieunhanh - PHP 5 Recipes A Problem-Solution Approach 2005 phần 4

Mô hình hướng đối tượng này cũng hỗ trợ kế thừa và cho phép một lớp (và đối tượng) gộp cả phương thức và các thuộc tính lại với nhau, nhưng không đầy đủ. KhiZeev và Andi viết lại phần mềm xử lý kịch bản cho PHP 4, thì đó là một phần mềm xử lý hoàn toàn mới, chạy nhanh hơn, ổn định hơn và nhiều tính năng hơn. | 220 5-9 DISPLAYING TIMES AND DATES IN OTHER LANGUAGES You might consider writing your own functions to deal with this type of situation but this is not very appealing not only because it is extra work for a German-language site but because the same task would then have to be repeated for each language. Fortunately a much better way to accomplish this task exists. You can use the setlocale function to change PHP s language and related settings in a number of ways. Here you are concerned primarily with how dates and times are represented but if internationalization is of any concern to you in your work with PHP you should investigate this function more thoroughly. setlocale takes two arguments a category a predefined constant and a language code a string . To localize time and date settings you can use either LC_ALL or LC_TIME for the category. You can determine whether the locale was set successfully by testing the return value of setlocale which is either the language string on success or FALSE on failure. The languages or locales actually supported will vary from system to system if you cannot find a value for a desired language or locale check with your system administrator or consult the operating system documentation. On Unix systems you can often find out what is supported by examining the contents of the usr share locale directory. On Windows use Regional Settings in the Control Panel. If you do not know ahead of time what is supported you can pass multiple language locale strings to setlocale and PHP will test each one in succession until you hope one is used successfully. For example php if lc setlocale LC_ALL de_DE de_DE@euro deu deu_deu german echo p Locale setting is lc . p else echo p Couldn t change to a German locale. p Once you have set the locale you are ready to output dates and times in the target language without resorting to brute-force translation and possible transliteration . However you cannot use date for this. Instead you must use a .