The WebAbility® Network Developers - Documentation
WebAbility Site
\core\WALanguage reference

I. Structure:


- namespace:
\core\

- Direct superclass:
\core\WAClass

- Direct known subclasses:
none

II. Description:


This is the basic class to compile a language, then use the different entries to display.
The language is a native XML file.
All the entries have a unique ID and a corresponding string in the language pointed by the object.

\core\WALanguage is serializable.

The original XML file have this structure as an example:



<?xml version="1.0" encoding="UTF-8" ?>
<language id="daysofweek" lang="es">

<entry id="sunday"><![CDATA[Domingo]]></entry>
<entry id="monday"><![CDATA[Lunes]]></entry>
<entry id="tuesday"><![CDATA[Martes]]></entry>
<entry id="wednesday"><![CDATA[Miércoles]]></entry>
<entry id="thursday"><![CDATA[Jueves]]></entry>
<entry id="friday"><![CDATA[Viernes]]></entry>
<entry id="saturday"><![CDATA[Sábado]]></entry>

</language>



The language tag is mandatory as a root tag and have 2 attributes:
- the id of the translation table, it should be the same as the filename containing the XML file.
- the language of this table

Each language entry have an id to refer the translation, and the value itself of the field entry.

Once the language table is loaded in memory, you can use it with the getEntry($id), setEntry($id, $data) and delEntry($id) methods.

SetEntry and delEntry does NOT affect the original XML file on hard disk unless you write it again manually.

\core\WALanguage is also accesible as an array, iterable and countable:




$lang = new WALanguage(file_get_contents('path/to/xml_file.xml'));

// To access to an entry, you may:

print $lang->getEntry('sunday');
// is equivalent to:
print $lang['sunday'];
// is equivalent to:
print $lang->sunday; // works only when the id is strictly alphanumeric (no spaces, no extra chars)

print 'There are ' . count($lang) . ' elements:<br />';
foreach($lang as $id => $data)
{
print $id . ': ' . $data . '<br />';
}





III. Reference:


3.1. Contructor:


\core\WALanguage($data)
Creates the instance.
If $data is a string, the object will automatically compile the $data xml in memory.
If $data is an array, it will copy the 3 parts of the language attributes: array( id => , lang => , entries => array( ) )
  • $data: mixed: if string, the raw xml language. If array, a language object to copy.


3.2. Constants:

There is no constant into this class.


3.3. Attributes:


protected string $id
Is used to store the id of the language table.

protected string $lang
Is used to store the language of the messages into the language table.

protected array $entries
Is used to store the language messages with their ids.


3.4. Methods:


public string getId()
This method is used to return the ID of the loaded language table.

public string getLang()
This method is used to return the language of the loaded language table.

public void setEntry($id, $data)
This method is used to set an entry in the language table. If the entry already exists, it will be overwritten. If not, it will be created.
  • $id: string, the unique identificator of the entry into the language table.
  • $data: string, the value of the language entry.

public void delEntry($id)
This method is used to delete an entry from the language table. If the entry does not exists, nothing happen.
  • $id: string, the unique identificator of the entry into the language table.

public string getEntry($id)
This method is used to get an entry in the language table. If the entry exists, it will return the value. If not, it will return the same $id.
  • $id: string, the unique identificator of the entry into the language table.