1. Go to this page and download the library: Download srt/translation-validator library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
srt / translation-validator example snippets
rtValidator\SrtTranslationValidator;
$validator = new SrtTranslationValidator();
// Tune timestamp drift tolerance (default is 0.5 seconds)
$validator->setTimestampTolerance(0.2);
// Fail on any error-severity defect instead of judging by ratios
$validator->setStrict(true);
// Override any quality threshold
// (defaults: loss 0.01 / drift 0.02 / merge 0.10 /
// verbatim 0.50 / near-verbatim 0.50 / script 0.00;
// partial_translation is advisory)
$validator->setMaxLossRatio(0.02);
$validator->setMaxDriftRatio(0.05);
$validator->setMaxMergeRatio(0.20);
$validator->setMaxVerbatimRatio(0.60);
$validator->setMaxNearVerbatimRatio(0.60);
$validator->setMaxScriptRatio(0.001);
// Declare the source language (e.g. from the job request): when it
// matches the target, the verbatim/untranslated gates are skipped
// (same-language passthrough). For non-Latin targets this is also
// inferred from the source script automatically.
$validator->setSourceLanguage('en');
// Fail when more than N error-severity defects are found,
// regardless of the ratios
$validator->setMaxErrors(10);
$result = $validator->validate(
'path/to/original.srt', // reference subtitles
'path/to/translation.srt', // the translation being checked
'de' // expected translation language
);
if ($result['valid']) {
echo "Translation is OK.\n";
exit(0);
}
foreach ($result['defects'] as $i => $defect) {
printf(
"%d. [%s] %s\n",
$i + 1,
strtoupper(str_replace('_', ' ', $defect['type'])),
$defect['message']
);
}
$validator->validate($originalPath, $translationPath, 'de');
// Did the file fail BECAUSE of this defect type? (verdict-deciding only:
// an error-tier defect tolerated by the share gates does not count)
$validator->isFailing('untranslated_copy'); // true when the verbatim gates fired
$validator->isFailing('reading_speed'); // false on a merely slow-but-passing file
// Did this defect occur at all, regardless of the verdict?
$validator->hasDefect('missing_caption'); // any severity
$validator->hasDefect('partial_translation', 'warning'); // severity-filtered
rtValidator\SrtTranslationValidator;
$result = (new SrtTranslationValidator())->validate(
'path/to/original.srt',
'path/to/translation.srt',
'de'
);
// Same encoding the CLI uses. JSON_UNESCAPED_UNICODE keeps umlauts,
// cyrillic etc. readable; drop JSON_PRETTY_PRINT for compact output.
$json = json_encode(
$result,
JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
);
file_put_contents('report.json', $json . "\n");
// Or serve it directly:
// header('Content-Type: application/json');
// echo $json;
rtValidator\SubtitleFormatValidator;
$validator = new SubtitleFormatValidator();
// Content already in memory:
$result = $validator->validateContent($srtContent, 'srt');
// Or a file on disk:
$result = $validator->validateFile('subtitles.srt');
printf(
"format=%s valid=%s cues=%d\n",
$result['format'], // 'srt' or 'vtt'
$result['valid'] ? 'yes' : 'no',
$result['cue_count']
);
foreach ($result['errors'] as $error) {
printf(" line %d [%s] %s\n", $error['line'], $error['subtype'], $error['message']);
}
rtValidator\ReadabilityChecker;
// Option 1: bind a file at construction; analyze() then needs no arguments
$checker = new ReadabilityChecker('Movie.de.srt');
$analysis = $checker->analyze();
// Option 2: one-shot on a file
$analysis = (new ReadabilityChecker())->analyzeFile('Movie.de.srt');
// Option 3: content already in memory (downloaded, generated, ...)
$analysis = (new ReadabilityChecker())->analyzeContent($srtString);
$analysis = (new ReadabilityChecker())->analyzeFile('Movie.de.srt', 17.0, 37);
foreach ($analysis['problems'] as $problem) {
printf(
"#%d [%s] %s\n",
$problem['caption'], // 1-based caption number
$problem['severity'], // 'critical' or 'minor'
$problem['text']
);
foreach ($problem['issues'] as $issue) {
printf(
" - %s: %s exceeds limit %s (%s)\n",
$issue['type'], // reading_speed | line_length | line_count
$issue['value'],
$issue['limit'],
$issue['severity']
);
}
}
if ($analysis['problems'] === []) {
echo "Every caption is within the limits.\n";
}
bash
composer install # dev dependencies (only needed for tests)
php -d phar.readonly=0 build/build-phar.php
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.