joylab / tarjim-php-client
PHP client to to use tarjim translation functions
1.1.13
2023-12-07 12:31 UTC
README
Setup
- run composer require joylab/tarjim-php-client
- create php tarjim config file in your project containing
<?php ## Required $project_id = ''; $cache_dir = full path to tarjim cache dir; $logs_dir = full path to tarjim logs dir; $apikey = ''; $default_namespace = '';
Optional
$additional_namespaces = [];
curl timeout for update cache api calls
$update_cache_timeout = 30;
key_case
defaults to 'lower'
'lower' => converts all keys from tarjim and keys passed to _T() functions to lowercase
'original' => preserves the keys' cases
$key_case = 'lower';
3. create tarjim cache and log files in the dir specified above
cd CACHE_DIR; touch translations.json translations_backup.json sanitized_html.json; cd LOGS_DIR; touch errors.log update_cache.log;
4. give permissions for cache and logs and config file
chmod -R 777 CACHE_DIR; chmod -R 777 LOGS_DIR; chmod 777 CONFIG_FILE;
## Usage
### Init
use Joylab\TarjimPhpClient\TarjimClient;
$TarjimClient = new TarjimClient(FULL_PATH_TO_CONFIG_FILE.CONFIG_FILE_NAME_WITH_EXTENSION); $language = 'en'; $TarjimClient->setTranslations($language);
### _T()
* For page titles add config = ['is_page_title' => true];
ex:
<?=_T($title_for_layout, ['is_page_title' => true])?> | Panda7
and if title was set in controller remove call to _T() from controller
* For placeholders, dropdown/select options, email subject, and swal pass in config skip_assign_tid = true
true])?> />
skip_assign_tid can also be used for page titles
### To use variables in translation value
* In tarjim.io add the variables you want as %%variable_name%%
* In view pass the mapping in config
_T($key, [
'mappings' => [
'var1' => 'var1 value',
]
]);
### Using tarjim for media
* call _TM($key, $attributes=[]) function
* _TI() is an alias of _TM()
* usage ex:
// optional $attributes = [
class => 'img-class-name',
width => '100px'
] />
renders
* **Important note for media attributes**:
attributes received from tarjim.io will overwrite attributes received from the function call if same attribute exists in both
so in previous example if this key has attributes: {class: 'class-from-tarjim', height:'200px'} __TM will return
notice that width and height are both added
### Using tarjim for datasets
* _TD($key, $config = []);
* returns values for all languages for a key ex:
[
'en' => 'en values,
'fr' => 'fr value'
]
* config can be ['namespace' => $namespace] if $namespace == 'all_namespaces' returns the values for all namespaces ex:
[
'namespace 1' => [
'en' => 'en values,
'fr' => 'fr value'
],
'namespace 2' => [
'en' => 'en value',
'fr' => 'fr value'
]
]