Gettext PHP

The Gettext functions are based on work by the GNU Translation Project. They aim to make it easier to write programs that send messages to users in their preferred language.

The Gettext functions rely on translation tables. These are usually given either .po or .mo extensions and are collected into directories. There is a global area for these files, but you can override that directory using bindtextdomain. The Gettext functions were added to PHP by Alex Plotnick.

string bindtextdomain(string domain, string directory)
The bintextdomain function sets the path for a domain.

string dcgettext(string domain, string message, int category)
The dcgettext function allows you to override the current domain for a single message lookup, and it also allows you to specify a category. The gettext manual questions the usefulness of this function.

string dgettext(string domain, string message)
The dgettext function allows you to override the current domain for a single message lookup.

string gettext(string message) string _(string message)
The Gettext function attempts to return a translated string. The message argument is used as both the key to the translation table and the default text if a translation is not found. The underscore character is an alias for the gettext function to help reduce cluttering your code.

//set language to Spanish
//specify location of translation tables
bindtextdomain("error_messages", "./locale");
//choose domain
//print a test message
print(gettext("This book is named Core PHP

string textdomain(string domain)
The textdomain function sets the domain to search within when calls are made to gettext. Thedomain is usually the name of your application. The previous domain is returned. If you just ant to get the current setting, pass an empty string. The default domain is called messages.

« Previous Topics
Next Topics »

Face Book Twitter Google Plus Instagram Youtube Linkedin Myspace Pinterest Soundcloud Wikipedia

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

PHP Topics