It's a starter project which is a modified version of Slim-Skeleton. Added slim's official twig helper and translation extension with some modifications.
It can help to build small web applications which needs basic routing system, template engine and multilingual functionality.
git clone https://github.com/alperenersoy/slim-twig-translate.git
cd slim-twig-translate
composer install
You can configure twig and translation settings from app/settings.php
'twig' => [
'templateDir' => '../templates',
'cache' => false, /*'../var/cache'*/
],
'translator' => [
'locale' => 'en',
'fallback' => 'en',
'folderPath' => '../lang'
]
To serve this application, run serve command in public folder:
cd public
php -S localhost:8080
Put your language files to the directory you specified. You can use JSON files such as lang/en.json or php files like lang/en/home.php.
In your twig template files, call these functions:
trans('key')
translate('key')
__('key')
trans_choice('key', $number) //returns singular or plural translation according to the number variable.
You can specify other parameters:
Replaceable variables
__("Our favorite color is :variable.",{'variable':"blue"})
Full usage ($key, array $replace = [], $locale = null, $fallback = true)
__("Our favorite color is :variable.", {'variable':"blue"}, "en", true)
getLocale() //gets locale e.g. "en".
translator() //returns translator instance itself.
hasTranslation($key, $locale = null, $fallback = true) //checks if translation exists. returns true or false.
This command helps to export translatable strings from your twig templates.
php command export-translations <templateDirectory> <languageDirectory> <targetLanguage>
#default example
php command export-translations templates lang en
This will create or update lang/en.json with untranslated keys.