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" verified="yes"><![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" translated="no"><![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, some optional attributes 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.

You can access to the attribute with getAttribute($id, $attr) and setAttribute($id, $attr, $value) methods.

setEntry, delEntry and setAttribute 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)

// To access to an attribute, you may:
print $lang->getAttribute('tuesday', 'verified');

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




To write again the file to the hard disk after creation or modifications, you just need to print it to the file:




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

$lang->setEntry('anotherday', '8th day');
$lang->setAttribute('anotherday', 'status', 'wrong');

file_put_contents('path/to/xml_file.xml', ''.$lang);




The object is serializable, for instance if you do not want to write it to hard disk but keep it in memory or database instead




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

$somedb->write('id', serialize($lang));

$newlang = unserialize($somedb->read('id'));






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.

public string getAttribute($id, $attribute)
This method is used to get an attribute of an entry in the language table. If the entry and the attribute exist, it will return the value. If not, it will return null.
  • $id: string, the unique identificator of the entry into the language table.
  • $attribute: string, the attribute name.

public string setAttribute($id, $attribute, $value)
This method is used to set an attribute of and entry in the language table. If the entry exists, it will set the new attribute or replace its value.
  • $id: string, the unique identificator of the entry into the language table.
  • $attribute: string, the attribute name.
  • $value: string, the value of the attribute to set.