Auxiliary functions
The CtNls class has several auxiliary member functions. These are not normally used, but can
come in handy.
come in handy.
The SetCatalogPath function overrides the path to the catalog directory given in the
configuration part of the CtNls class.
configuration part of the CtNls class.
SetDefaultLanguage does just that. It sets the code of the language to be tried in case all else
fails. This is normally English.
fails. This is normally English.
SetLanguageId and GetLanguageId set and return the id/code of the first language to check.
This language is checked before any of those found in the user preferences. Upon
initialization, it is the same as the first language in the user list. Once LoadPage() or
LoadMessages() is invoked, GetLanguageId() would return the ID of the language that was
used in delivering the content to the browser. It is also useful if the user has stored
his/her preference in a cookie.
This language is checked before any of those found in the user preferences. Upon
initialization, it is the same as the first language in the user list. Once LoadPage() or
LoadMessages() is invoked, GetLanguageId() would return the ID of the language that was
used in delivering the content to the browser. It is also useful if the user has stored
his/her preference in a cookie.
How it actually works
We have already explained what happens during the content negotiation phase. The browser
sends the user’s choice of languages in the Accept-Language header. This information is
available to any PHP script in the $HTTP_ACCEPT_LANGUAGE global variable.
sends the user’s choice of languages in the Accept-Language header. This information is
available to any PHP script in the $HTTP_ACCEPT_LANGUAGE global variable.
When you create a new instance of the CtNls object, the constructor function CtNls() takes
control. In turn, it initializes some internal settings and calls setLanguage(). This internal
member function goes through the list of languages in $HTTP_ACCEPT_LANGUAGE
(delimited by commas), by using only the language code and stripping the region code. The
language code is for selecting the appropriate file. In this process, we convert the list to an
array, but preserve the language priority.
control. In turn, it initializes some internal settings and calls setLanguage(). This internal
member function goes through the list of languages in $HTTP_ACCEPT_LANGUAGE
(delimited by commas), by using only the language code and stripping the region code. The
language code is for selecting the appropriate file. In this process, we convert the list to an
array, but preserve the language priority.
When an application invokes either LoadPage() or LoadMessages(), these methods will call
the internal search() function.
the internal search() function.
The search() member function walks the language preference array from start to finish and if
all of them fail to produce results, it checks for the default language. This function keeps
track of which languages it tried so that two attempts are not done on the same language.
all of them fail to produce results, it checks for the default language. This function keeps
track of which languages it tried so that two attempts are not done on the same language.
The getFile() member function is called every time the search() function generates a new
language candidate. It does this by passing it a base name, a language candidate and
optionally a file extension.
language candidate. It does this by passing it a base name, a language candidate and
optionally a file extension.
By passing the base name, the language code and the file extension in that order, the
getFile() function generates a list of candidate file names. If we are using LoadMessages()
and pass it an empty extension name, it produces a list by using all known valid PHP script
extensions by prefixing them with the path to the catalog directory. These PHP extension
names are currently “.php3” and “.php”. If, on the other hand, we pass the file extension to the
function, then, we reduce the list of candidate file names to one.
getFile() function generates a list of candidate file names. If we are using LoadMessages()
and pass it an empty extension name, it produces a list by using all known valid PHP script
extensions by prefixing them with the path to the catalog directory. These PHP extension
names are currently “.php3” and “.php”. If, on the other hand, we pass the file extension to the
function, then, we reduce the list of candidate file names to one.
getFile() then continues by walking through the list of candidate file names until it finds one
that exists. Based on this, we determine the current language and the object reads in that file.
that exists. Based on this, we determine the current language and the object reads in that file.
— Didimo