7.A Internationalization (i18n)
Covers Language translations and timezone conversions.
7.A.1. Languages
Languages in Cradle are fairly robust, simply defined and designed to work with other translating services such as Google Translate. The follow figure shows how to set up a french translator.
7.A.1.1. Usage
Figure 1. French Translator
use Cradle\i18n\Language;
//initial translations
$translations = [
'Hello' => 'Bonjour',
'How are you?' => 'Como tale vous?'
];
//load up the translation
$french = new Language($translations);
//you can add translations on the fly
$french->translate('I am good thank you, and you?', 'Bien mercy, et vous?');
//now echo some translations
echo $french->get('Hello'); //--> Bonjour
echo $french->get('How are you?'); //--> Como tale vous?
echo $french->get('I am good thank you, and you?'); //--> Bien mercy, et vous?
For ease of use we also made the language object accessible as an array. The next figure shows the same as Figure 1
except that it’s using arrays to manipulate the object.
Figure 2. Languages as Arrays
//initial translations
$translations = [
'Hello' => 'Bonjour',
'How are you?' => 'Como tale vous?'
];
//load up the translation
$french = new Language($translations);
//you can add translations on the fly
$french['I am good thank you, and you?'] = 'Bien mercy, et vous?';
//now echo some translations
echo $french['Hello']; //--> Bonjour
echo $french['How are you?']; //--> Como tale vous?
echo $french['I am good thank you, and you?']; //--> Bien mercy, et vous?
foreach($french as $default => $translation) {
echo $translation;
}
Most commonly, websites using languages usually start and end with loading and saving translation files. Using Cradle’s language object through the life of the page and later saving will keep your languages file always up to date.
Note: Cradle does not do the actual translating on its own. Once the file is generated and saved you should run it through a translating service.
Figure 3. Loading and Saving a Language File
//load up a translation file
$translations = include '/path/to/french.php';
$french = new Language($translations);
//add to translation
$french['I am good thank you, and you?'] = 'Bien mercy, et vous?';
//save back to file
$french->save('/path/to/french.php');
7.A.1.2. Language API
get
Returns the translated key. if the key is not set it will set the key to the value of the key
Usage
$language->get(string );
Parameters
string
Returns string
Example
$language->get();
getLanguage
Return the language set
Usage
$language->getLanguage();
Parameters
Returns array
save
Saves the language to a file
Usage
$language->save(string|null $file);
Parameters
string|null $file
- The file to save to
Returns this
Example
$language->save();
translate
Sets the translated value to the specified key
Usage
$language->translate(*string $key, *string $value);
Parameters
*string $key
- The translation key*string $value
- The default value if we cannot find the translation
Returns this
Example
$language->translate('foo', 'foo');
7.A.2. Timezone
7.A.2.1. Usage
use Cradle\i18n\Timezone;
$timezone = new Timezone(time(), 'GMT');
7.A.2.2. API
- convertTo
- getGMT
- getGMTDates
- getOffset
- getOffsetDates
- getTime
- getUTC
- getUTCDates
- toRelative
- setTime
- validation
convertTo
Convert current time set here to another time zone
Usage
$timezone->convertTo(*string $zone, string|null $format);
Parameters
*string $zone
- valid UTC, GMT, PHP Location or TZ Abbreviationstring|null $format
- format
Returns string|int
Example
$timezone->convertTo('Asia/Manila');
getGMT
Returns the GMT Format
Usage
$timezone->getGMT(string $prefix);
Parameters
string $prefix
- Prefix to add before the returned value
Returns string
Example
$timezone->getGMT();
getGMTDates
Returns a list of GMT formats and dates in a 24 hour period
Usage
$timezone->getGMTDates(*string $format, int $interval, string|null $prefix);
Parameters
*string $format
- The format of each date to displayint $interval
- The frequency of rowsstring|null $prefix
- The prefix to add before each date display
Returns array
Example
$timezone->getGMTDates('F d, Y');
getOffset
Returns the current offset of this timezone
Usage
$timezone->getOffset();
Parameters
Returns int
getOffsetDates
Returns a list of offsets and dates in a 24 hour period
Usage
$timezone->getOffsetDates(*string $format, int $interval);
Parameters
*string $format
- The format of each date to displayint $interval
- The frequency of rows
Returns array
Example
$timezone->getOffsetDates('F d, Y');
getTime
Returns the time or date
Usage
$timezone->getTime(string|null $format);
Parameters
string|null $format
- Time format
Returns string|int
Example
$timezone->getTime();
getUTC
Returns the UTC Format
Usage
$timezone->getUTC(string|null $prefix);
Parameters
string|null $prefix
- The prefix to add before the returned value
Returns string
Example
$timezone->getUTC();
getUTCDates
Returns a list of UTC formats and dates in a 24 hour period
Usage
$timezone->getUTCDates(*string $format, int $interval, string|null $prefix);
Parameters
*string $format
- The format of each date to displayint $interval
- The frequency of rowsstring|null $prefix
- The prefix to add before each date display
Returns array
Example
$timezone->getUTCDates('F d, Y');
toRelative
Returns the relative distance $time > this->time = ago
Usage
$timezone->toRelative(int|string $time, int $level, string $default);
Parameters
int|string $time
- The time to make relativeint $level
- The granular levelstring $default
- The default date format
Returns Cradle\i18n\Timezone
Example
$timezone->toRelative();
setTime
Sets a new time
Usage
$timezone->setTime(*int|string $time);
Parameters
*int|string $time
- The time value
Returns Cradle\i18n\Timezone
Example
$timezone->setTime(time() + 123);
validation
Returns timezone’s validation methods
Usage
$timezone->validation();
Parameters
Returns Cradle\i18n\Timezone