tarjim / tarjim-php
Serve text released from Tarjim from a local copy, refreshed from a Tarjim delivery distribution.
Requires
- php: >=7.4
- ext-curl: *
- ext-json: *
Requires (Dev)
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^9.6
Suggests
- ext-dom: Needed by tHtml().
- ext-intl: Lets export pulls tell apart file names that differ only in Unicode form.
- ext-mbstring: Needed by keyCase 'lower'.
- ext-zip: Needed by export pulls.
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-11-24 09:26:32 UTC
README
Serve the text you release in Tarjim from a PHP application.
The package keeps a verified copy of a release in a folder on each server, and your pages read text from that copy. Pages never call Tarjim: the copy is refreshed only when you ask, from a deploy step, a cron job or a command.
It can also pull a bundle export (for example Laravel PHP language files) into a folder.
Status
Alpha. Versions are -alpha.N until a real site runs on the package, then -beta.N until
the release checklist in RELEASING.md is met. The API may change before 1.0.0:
pin the version ("tarjim/tarjim-php": "^0.1@alpha", or an exact tag) and read the
changelog before upgrading.
Requirements
- PHP 7.4 or later, with
ext-jsonandext-curl. No other Composer packages. ext-domfortHtml(),ext-mbstringforkeyCase: 'lower',ext-zipfor export pulls.ext-intllets export pulls tell apart file names that differ only in Unicode form.
Install
composer require "tarjim/tarjim-php:^0.1@alpha"
Quick start
In Tarjim, create a distribution for your site and copy its delivery URL and its keys: the
Development key reads the dev stage, the Production key the prod stage.
use Tarjim\Client; $tarjim = Client::create([ 'dir' => '/var/lib/myapp/tarjim', // absolute; outside the web root 'deliveryUrl' => getenv('TARJIM_DELIVERY_URL'), 'key' => getenv('TARJIM_DELIVERY_KEY'), 'locale' => 'fr', 'fallbackLocale' => 'en', ]); echo $tarjim->tHtml('welcome_title'); // safe HTML echo htmlspecialchars($tarjim->t('page_title')); // plain text: escape it yourself
Fill the folder before the site serves pages, and keep it fresh:
# in the deploy, as the user the site runs as vendor/bin/tarjim-php refresh --config=config/tarjim.php --require-copy # from cron, every few minutes */5 * * * * cd /var/www/myapp && vendor/bin/tarjim-php refresh --config=config/tarjim.php
config/tarjim.php returns the same array you pass to Client::create().
The local copy
- One folder per server, outside the web root, holding one distribution. Two servers may differ
for a few minutes after a promote. In containers the folder lives as long as the container: run
refreshwhen the container starts, or during the image build into the same folder. - Run
refreshas the user the site runs as (for php-fpm usuallywww-data). A copy written by root cannot be updated by the web server;refreshchecks the folder is writable first and answersmisconfiguredif not. Under php-fpm,getenv()sees the environment only withclear_env = no; otherwise put the values in the config file from your own secret store. - Atomic. A release is installed only when every file is verified, and the pointer to it is written last. A reader sees the old release or the new one, never a mix. A failed refresh never removes what is held.
- At most one check a minute per folder, across processes;
Retry-Afteris honoured. - The key is never written to the folder, a log line or a message.
- The current and previous release are kept; anything else is removed a day after it stopped being used.
- A long-running process (a queue worker, Octane) notices a promote within a second of the next lookup.
refresh exit codes
| exit | |
|---|---|
| updated, unchanged, not due yet, another refresh running, nothing released yet | 0 |
| failed (network, server, a file that does not verify) | 1 |
| misconfigured (bad config, folder not writable, key refused, configured bundle missing or not flat) | 2 |
--wait=<seconds> ran out while waiting for the lock, the one-minute floor, Retry-After or a first release |
3 |
--require-copy and no usable copy held afterwards |
4 |
With --require-copy, the exit is 0 whenever a usable copy is held after the run, whatever the
run's outcome (a failure is still printed). Use it in a deploy that must not go live without text.
vendor/bin/tarjim-php status --config=… never calls Tarjim: 0 a fresh copy, 1 a stale one (no
successful check within staleAfter), 2 bad config, 4 nothing held.
Configuration
| Key | Default | Meaning |
|---|---|---|
dir |
— | Absolute folder for the copy. Needed to read and to refresh. |
deliveryUrl |
— | The distribution's delivery URL, https://…/delivery/<id>/meta, used exactly as given: https (http only on localhost), no query, fragment or trailing slash. |
key |
— | The delivery key. Needed only to refresh. |
bundle |
'default' |
The bundle lookups read. A custom bundle of that name first, then a namespace. It must be a flat bundle. |
locale |
— | The language lookups serve. With no locale, no fallbackLocale and no per-call locale, lookups answer with the key text. |
fallbackLocale |
— | Served when locale has no text in the bundle. |
keyCase |
'exact' |
'lower' folds key names on both sides. |
legacyPercentPlaceholders |
false |
Also fill %%name%%, as the old client did. |
staleAfter |
86400 |
Seconds without a successful check before status reports stale. |
logger |
none | function (string $level, string $message, array $context). |
caBundle |
system | A CA file for TLS. |
strict |
false |
Throw on a bad config instead of degrading. |
apiUrl, apiKey |
— | For export pulls only. |
Client::create() never throws (unless strict). A bad dir, deliveryUrl or key makes
refresh() answer misconfigured; a bad optional value falls back to its default. Both are logged,
and lookups answer with the key text.
Lookups
Lookups read only the local copy: they never call Tarjim, never write and never throw.
$tarjim->t('cart.empty'); // text as written $tarjim->t('cart.count', ['count' => 3]); // fills {count} $tarjim->tHtml('terms.intro', ['name' => $userName]); // safe HTML; inserted values escaped $tarjim->has('promo.banner'); // true only for a non-empty value $tarjim->image('home.hero'); // ?Image: url (https only) and attributes $tarjim->locales(); // the languages the bundle has $tarjim->t('cart.empty', [], ['locale' => 'ar', 'bundle' => 'emails']);
- A missing key, or an empty value, answers with the key text, unfilled.
- Language: the exact tag, then a less specific or sibling form of the same language and script
(as the JavaScript SDK chooses), then
fallbackLocale.zh-TWis servedzh-Hant, neverzh-Hans. The chosen language is served whole: a key it lacks answers with the key text, not another language's text.fr_CAandfr-CAare the same tag. - Placeholders:
{name}from a named array, only when arguments are given. Inserted text is never scanned again. Strings, numbers and objects with__toString()are inserted; anything else leaves the placeholder as written. tHtml()keepsa[href](https, mailto, tel),b strong i em u br p span ul ol li h1–h6 hr div table thead tbody tr th tdandimg[src](https)[alt], plusclasson any of them andtarget="_blank"on a link (always withrel="noopener noreferrer"). Every other tag and attribute is removed,styleincluded;script,style,iframe,svgand similar are removed with their content. Use its output in an HTML body only; for attributes, JavaScript, JSON, email subjects and SMS uset()and escape for that context.image()returns an image only for anhttpsURL, withalt,title,width,heightandloadingat most.- Staleness is not logged by lookups. A site that refreshes only at deploy is "stale" a day
later by design;
status()andtarjim-php statusreport it.
$status = $tarjim->status(); if ($status->stale) { // alert: $status->lastSuccessAt, $status->releaseId }
Errors
- Lookups never throw. A missing key, an empty value, a bad config or a missing or corrupt copy
answers with the key text (
t(),tHtml()),false(has()),null(image()) or[](locales()). Client::create()never throws, unless you pass'strict' => true; then a bad config throwsInvalidArgumentException, whose message never contains the key.refresh()never throws either. It returns aRefreshResultwhosestatussays what happened and whosemessagesays why; the command line maps it to the exit codes.- Failures are logged through the
loggeryou configure: a bad config atcreate()and a misconfigured refresh aserror, a failed refresh aswarning. Lookups log only a missing extension (ext-dom,ext-mbstring), once per client. - An export pull returns an
ExportResultwithstatusupdated,failedormisconfiguredand amessage; a failed pull leaves the folder as it was.
Export pulls
Pull a bundle export (any format, including Laravel PHP files) into a folder:
$tarjim = Client::create(['apiUrl' => 'https://api.tarjim.io', 'apiKey' => getenv('TARJIM_API_KEY')]); $result = $tarjim->exports()->pull(12, 34, '/var/www/myapp/lang'); // project 12, bundle 34 // $result->status: 'updated' | 'failed' | 'misconfigured'; $result->written, $result->removed
vendor/bin/tarjim-php export --config=config/tarjim.php --project=12 --bundle=34 --out=/var/www/myapp/lang
- It owns the files it wrote, not the folder. It records them in
.tarjim-export.jsonand the next pull removes only those the new export no longer has. Files it never wrote are left alone, and a pull that would overwrite one fails, naming it; pass['adoptExisting' => true]to take such files over (for example files the old client wrote). - All or nothing: a failed pull leaves the folder as it was.
- A zip entry that could land outside the folder, a link, an encrypted entry or an export larger
than
maxBytes(64 MiB, unpacked) refuses the whole export. - Options:
timeout(300 s),poll(2 s),maxBytes,adoptExisting. tarjim-php exportexits 0 when files were written, 1 when the pull failed, 2 on a bad config or bad arguments.
Moving from joylab/tarjim-php-client
See docs/moving-from-tarjim-php-client.md, with a helper
file that keeps _T, _TS, _TM, _TD and _TSEO working: examples/tarjim-helpers.php.
Documentation & support
- Questions and bugs: GitHub issues.
- Security issues: report them privately to support@tarjim.io, not in a public issue. See SECURITY.md.
- Moving from
joylab/tarjim-php-client, what changed in each version, and how releases are made.
Development
composer update
vendor/bin/phpunit # unit tests and real-curl tests against a local php -S server
vendor/bin/phpstan analyse
Tests never call a Tarjim server.
Licence
MIT; see LICENSE.