Sitellite Application Framework
Class Tree         Index         All Elements

Class: I18n

Source Location: Program_Root/I18n/I18n.php

Class Overview


I18n is a class that makes it easier to add multiple language support to PHP programs. It is lightweight and not very sophisticated, attempting to keep it straight-forward while emulating some of the more elegant features of other internationalization systems, such as gettext.


Author(s)

Version

  • 3.0, 2003-04-20, $Id: I18n.php,v 1.3 2005/12/29 22:04:54 lux Exp $

Copyright

  • Copyright (C) 2001-2003, Simian Systems Inc.

Variables

Methods


Child Classes

SQL
SQL extends I18n to become an SQL abstraction layer for Sitellite.

Inherited Variables

Inherited Methods


Class Details

[line 120]
I18n is a class that makes it easier to add multiple language support to PHP programs. It is lightweight and not very sophisticated, attempting to keep it straight-forward while emulating some of the more elegant features of other internationalization systems, such as gettext.

Since I18n is simply PHP code, it is also cross-platform, which was the reason for not using an implementation like gettext in the first place.

New in 1.2:

  • Added a $charset property.
  • Added a $fullname property.
New in 1.4:
  • Added a $method property, which determines the method used to create the $lang_hash keys.
  • Added a list of vowels to the 'metaphone' serialization method, making it more unique.
  • Added an optional second parameter to the get() method, which if passed an object, will know to call the global $tpl object on the string, allowing substitutions to occur. Note: This requires a global $tpl template object to be available.
New in 1.6:
  • Added an 'old' method, so serialize() will create old-style keys, useful for rebuilding language files from older versions.
  • Added a build_keylist() method. See below for details.
New in 1.8:
  • Added a $load_new parameter to the constructor method. See below for details.
New in 2.0:
  • Added support for arrays, not just objects, as the second parameter to get().
  • Added a getf() method, which uses the sprintf() function instead of saf.Template to substitute values into strings.
New in 2.2:
  • Improved the pattern matching in the build_keylist() method.
New in 2.4:
  • Switched the sprintf() calls in getf() to vsprintf(), which simplified the method and made it more flexible in the process.
  • Added a getLanguages() method that retrieves a list of languages from an XML file. The XML file must be of the format:
<languages> <lang> <name>English</name>

1 en
<charset>ISO-8859-1</charset> </lang> <lang> <name>Russian</name>

1 ru
<charset>windows-1259</charset> </lang> </languages>

New in 2.6:

  • Switched to using saf.Template.Simple in get(), so substitution tags now take the form {tagname}.
  • Added a getLanguage() method.
New in 2.8:
  • Added functions as aliases of methods of a global $intl I18n object. These are: i18n_get(), i18n_getf(), and i18n_serialize(), intl_get(), intl_getf(), and intl_serialize(). The latter 3 are identical to the first three.
New in 3.0:
  • New language fallback mechanism.
  • New languages.php file format, now uses INI instead of XML (MUCH faster).
  • New negotiate() method which supports HTTP, Cookie, and Session methods of determining the language of choice.
  • Changed the parameters accepted by the constructor, removing the $language and $page parameters and adding $negotiationMethod.

1 <?php
2
3 $i18n = new I18n ('inc/lang', 'http') {
4
5 echo $i18n->get ('Hello, welcome to our site!');
6
7 ? >




Tags:

access:  public
version:  3.0, 2003-04-20, $Id: I18n.php,v 1.3 2005/12/29 22:04:54 lux Exp $
license:  http://www.sitellite.org/index/license
copyright:  Copyright (C) 2001-2003, Simian Systems Inc.
author:  John Luxford <mailto:lux@simian.ca>


[ Top ]


Class Variables

$charset =  'iso-8859-1'

[line 183]

The charset of the current language, which can be used to tell the browser, or any language-aware PHP functions, which to use.



Tags:

access:  public

Type:   mixed


[ Top ]

$cookieName =  'sitellite_lang_pref'

[line 218]


Type:   mixed


[ Top ]

$directory =

[line 146]

The location of the language files.



Tags:

access:  public

Type:   mixed


[ Top ]

$error =

[line 227]

If an error occurs during any portion of this class, this will contain the message.



Tags:

access:  public

Type:   mixed


[ Top ]

$fallbacks = array ()

[line 208]

Contains fallback text replacements.



Tags:

access:  public

Type:   mixed


[ Top ]

$fullname =  ''

[line 191]

The full name of language in use (ie. 'English' for 'en').



Tags:

access:  public

Type:   mixed


[ Top ]

$language =

[line 129]

The language code, corresponding to the name of the language file.



Tags:

access:  public

Type:   mixed


[ Top ]

$languages = array ()

[line 216]

2-D list of available languages, retrieved from getLanguages().



Tags:

access:  public

Type:   mixed


[ Top ]

$lang_hash = array ()

[line 164]

The language hash, a key/value list.



Tags:

access:  public

Type:   mixed


[ Top ]

$load_new =  false

[line 174]

Determines whether getIndex() should call include() or include_once() to retrieve the language keys. Default is false, which calls include_once().



Tags:

access:  public

Type:   mixed


[ Top ]

$locale =

[line 138]

The locale code, corresponding to the name of the country to localize numbers and dates for.



Tags:

access:  public

Type:   mixed


[ Top ]

$method =  'metaphone'

[line 200]

The method to use to create the $lang_hash keys. Can be 'metaphone', 'md5', and 'plain'.



Tags:

access:  public

Type:   mixed


[ Top ]

$page =

[line 156]

The name of the current page, which can be used in the language file in a switch statement to keep the $lang_hash array shorter, instead of loading more than is necessary.



Tags:

access:  public

Type:   mixed


[ Top ]



Class Methods


constructor I18n [line 244]

I18n I18n( [string $directory = 'inc/lang'], [string $negotiationMethod = 'html'], [boolean $load_new = false])

Constructor Method. Includes the appropriate language file

in the provided $directory. This file is intended to fill out the $lang_hash array. $load_new determines whether to call include() or include_once(). True calls include(), so that multiple language files may be opened. $negotiationMethod determines the method whereby the language of choice is determined. See the negotiate() method for more info on this.




Tags:

access:  public


Parameters:

string   $directory  
string   $negotiationMethod  
boolean   $load_new  

[ Top ]

method build_keylist [line 509]

associative build_keylist( [string $basedir = '.'], [mixed $except = false])

Generates an associative array of all the occurrences of ->get ('...'), ->getf ('...'), and [I18n: ...] in all files in the directory specified (recursively). This essentially creates a serializeable keylist that can be used as a point of reference for building language files.



Tags:

return:  array
access:  public


Parameters:

string   $basedir  
mixed   $except  

[ Top ]

method get [line 344]

string get( [object $original = ''], [mixed $obj = ''], [mixed $isHtml = false])

Takes a string, serializes it to generate a key, and performs

a key/value lookup on the $lang_hash array. Returns the value found, or the original string if not found. This is the method used in I18n to return translated text. Optionally includes a second parameter, which if included, tells I18n to use a global $tpl object to parse the current string and feed it the specified object (or associative array). This allows developers to make elements dynamic, and even re-order elements, inside a language string.




Tags:

access:  public


Parameters:

object   $original  

[ Top ]

method getf [line 410]

string getf( string $original, mixed $[many])

Takes a string, serializes it to generate a key, and performs

a key/value lookup on the $lang_hash array. Returns the value found, or the original string if not found. This method is very similar to the get() method, except instead of using saf.Template to insert values into the string, it uses the vsprintf() function to fill in the values. If you pass an array as the second value, it will use that instead of however many additional arguments you fed it. This is a good thing, because if you already have all your values in an array, you can simply say get($original, $array) instead of getf($original, $array[0], $array[1], $array[2]).




Tags:

access:  public


Parameters:

string   $original  
mixed   $[many]  

[ Top ]

method getIndex [line 289]

void getIndex( )



[ Top ]

method getLanguage [line 533]

object getLanguage( string $id, [string $langfile = 'inc/lang/languages.php'])

Returns a single language node as an object, taken from the specified languages.php file.



Tags:

access:  public


Parameters:

string   $id  
string   $langfile  

[ Top ]

method getLanguages [line 568]

array getLanguages( [string $langfile = 'inc/lang/languages.php'])

Returns a 2-D array from the specified language file, which is an INI file. Each section name in the file corresponds to a different available language. Keys in each section include 'name', 'code', 'locale', 'charset', 'fallback', and 'default'.



Tags:

access:  public


Parameters:

string   $langfile  

[ Top ]

method negotiate [line 590]

string negotiate( [string $method = 'http'])

Returns the preferred language of the current visitor.

If the $method is 'http' then it uses the HTTP Accept-Language string for this info. If the $method is 'cookie' it uses a cookie (specified by the $cookieName property) to determine, and if the $method is 'session' it relies on the global $session object. Default is 'http'.




Tags:

access:  public


Parameters:

string   $method  

[ Top ]

method serialize [line 460]

string serialize( [string $string = ''])

Generates a key for use in a key/value lookup on the $lang_hash array. Uses the metaphone () of the first few words in the string, as well as the length of the string to generate the key.



Tags:

access:  public


Parameters:

string   $string  

[ Top ]

method setLocale [line 279]

void setLocale( )



[ Top ]

method writeIndex [line 667]

void writeIndex( mixed $file, mixed $data)



[ Top ]

method writeLanguage [line 678]

void writeLanguage( mixed $file, mixed $data, mixed $code, [mixed $locale = ''])



[ Top ]


Copyright © 2007, SIMIAN systems Inc.
All rights reserved. Privacy policy
Documentation generated on Tue, 13 Feb 2007 17:17:44 -0600 by Sitellite AppDoc and phpDocumentor 1.2.2