Ratings | | Unique User Downloads | | Download Rankings |
65% | | Total: 319 | | All time: 7,239 This week: 79 |
|
Description | | Author |
This package can translate application texts using INI files.
It can determine the current page language from a given request variable or default to the first language in an array of supported language settings defined in the config.php script.
The class loads the texts for the current language from a INI file.
It also returns the application strings translated for the current language. The string may be formatted with optional parameters as defined for sprintf.
If the chosen language translations file does not exist, the class writes a new file for that language based on the default language texts. | |
|
|
Innovation award
Nominee: 1x |
|
Recommendations
Example
<?php
// include localization class
include 'localization.php';
$localization = Localization::instance();
?>
<html lang="<?php echo $localization->getCurrentLanguage(); ?>">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><?php echo __('Site title'); ?></title>
<script>
/* method getJson() can be used for javascript translations */
var translations = <?php echo $localization->getJson(['Welcome to our site']); ?>;
console.log(translations['Welcome to our site']);
</script>
</head>
<body>
<h1><?php echo __('Welcome to our site'); ?></h1>
<hr />
<ul>
<?php foreach($localization->getLanguages() as $code => $name): ?>
<li>
<a href="?language=<?php echo $code; ?>"><?php echo $name; ?></a>
</li>
<?php endforeach; ?>
</ul>
<hr />
Usage:
<?php echo __('Current language key code is %s', [$localization->getCurrentLanguage()]); ?>
<br /><br />
or
<?php echo __('Translate function can take multiple params like %s and %s', ['this', 'that']); ?>
<br /><br />
or
<?php echo Localization::instance()->translate('Welcome to our site'); ?> ;
<br /><br />
</body>
</html>
|
Details
PHP Localization Library
Setting it up config.php
return [
'path' => 'translations', // translation files directory path | translations is default
'input' => 'language', // url parameter | language is default
'languages' => [ // languages, first language is default
'en' => 'English',
'de' => 'Deutsch',
'it' => 'Italiano'
]
];
Translating strings
echo __('Site title');
echo __('Site %s', ['title']);
echo Localization::instance()->translate('Site %s', ['title']);
|
Applications that use this package |
|
No pages of applications that use this class were specified.
If you know an application of this package, send a message to the author to add a link here.