layout | class | title | description | menu_title | menu | ||||
---|---|---|---|---|---|---|---|---|---|
documentation |
page-docs |
i18n - Extras - Cradle |
Internationalization |
i18n |
|
Covers Language translations and timezone conversions.
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.
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.
//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.
//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');
Returns the translated key. if the key is not set it will set the key to the value of the key
$language->get(string );
string
Returns string
$language->get();
Return the language set
$language->getLanguage();
Returns array
Saves the language to a file
$language->save(string|null $file);
string|null $file
- The file to save to
Returns this
$language->save();
Sets the translated value to the specified key
$language->translate(*string $key, *string $value);
*string $key
- The translation key*string $value
- The default value if we cannot find the translation
Returns this
$language->translate('foo', 'foo');
use Cradle\i18n\Timezone;
$timezone = new Timezone(time(), 'GMT');
- convertTo
- getGMT
- getGMTDates
- getOffset
- getOffsetDates
- getTime
- getUTC
- getUTCDates
- toRelative
- setTime
- validation
Convert current time set here to another time zone
$timezone->convertTo(*string $zone, string|null $format);
*string $zone
- valid UTC, GMT, PHP Location or TZ Abbreviationstring|null $format
- format
Returns string|int
$timezone->convertTo('Asia/Manila');
Returns the GMT Format
$timezone->getGMT(string $prefix);
string $prefix
- Prefix to add before the returned value
Returns string
$timezone->getGMT();
Returns a list of GMT formats and dates in a 24 hour period
$timezone->getGMTDates(*string $format, int $interval, string|null $prefix);
*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
$timezone->getGMTDates('F d, Y');
Returns the current offset of this timezone
$timezone->getOffset();
Returns int
Returns a list of offsets and dates in a 24 hour period
$timezone->getOffsetDates(*string $format, int $interval);
*string $format
- The format of each date to displayint $interval
- The frequency of rows
Returns array
$timezone->getOffsetDates('F d, Y');
Returns the time or date
$timezone->getTime(string|null $format);
string|null $format
- Time format
Returns string|int
$timezone->getTime();
Returns the UTC Format
$timezone->getUTC(string|null $prefix);
string|null $prefix
- The prefix to add before the returned value
Returns string
$timezone->getUTC();
Returns a list of UTC formats and dates in a 24 hour period
$timezone->getUTCDates(*string $format, int $interval, string|null $prefix);
*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
$timezone->getUTCDates('F d, Y');
Returns the relative distance $time > this->time = ago
$timezone->toRelative(int|string $time, int $level, string $default);
int|string $time
- The time to make relativeint $level
- The granular levelstring $default
- The default date format
Returns Cradle\i18n\Timezone
$timezone->toRelative();
Sets a new time
$timezone->setTime(*int|string $time);
*int|string $time
- The time value
Returns Cradle\i18n\Timezone
$timezone->setTime(time() + 123);
Returns timezone's validation methods
$timezone->validation();
Returns Cradle\i18n\Timezone