Bolt\Translation\TranslationFile::buildNewContent PHP Method

buildNewContent() private method

Builds the translations file data with added translations.
private buildNewContent ( array $newTranslations, array $savedTranslations, array $hinting = [] ) : string
$newTranslations array New translation data to write
$savedTranslations array Translation data read from file
$hinting array Translation data that can be used as hinting
return string
    private function buildNewContent($newTranslations, $savedTranslations, $hinting = [])
    {
        // Presort
        $unusedTranslations = $savedTranslations;
        $transByType = ['Unused' => [' unused messages', []], 'TodoReal' => [' untranslated messages', []], 'TodoKey' => [' untranslated keyword based messages', []], 'DoneReal' => [' translations', []], 'DoneKey' => [' keyword based translations', []]];
        foreach ($newTranslations as $key => $translation) {
            $set = ['trans' => $translation];
            if (preg_match('%^[a-z0-9-]+\\.[a-z0-9-.]+$%', $key)) {
                $type = 'Key';
                $set['key'] = preg_split('%\\.%', $key);
            } else {
                $type = 'Real';
            }
            $done = $translation === '' ? 'Todo' : 'Done';
            $transByType[$done . $type][1][$key] = $set;
            if (isset($unusedTranslations[$key])) {
                unset($unusedTranslations[$key]);
            }
        }
        foreach ($unusedTranslations as $key => $translation) {
            $transByType['Unused'][1][$key] = ['trans' => $translation];
        }
        // Build List
        $indent = '    ';
        $status = '# ' . $this->relPath . ' – generated on ' . date('Y-m-d H:i:s e') . "\n\n" . '# Warning: Translations are in the process of being moved to a new keyword-based translation' . "\n" . '#          at the moment. This is an ongoing process. Translations currently in the ' . "\n" . '#          repository are automatically mapped to the new scheme. Be aware that there ' . "\n" . '#          can be a race condition between that process and your PR so that it\'s ' . "\n" . '#          eventually necessary to remap your translations. If you\'re planning on ' . "\n" . '#          updating your translations, it\'s best to ask on IRC to time your contribution' . "\n" . '#          in order to prevent merge conflicts.' . "\n\n";
        $content = '';
        // Set this to true to get nested output.
        $nested = false;
        foreach ($transByType as $type => $transData) {
            list($text, $translations) = $transData;
            // Header
            $count = count($translations) > 0 ? sprintf('%3s', count($translations)) : ' no';
            $status .= '# ' . $count . $text . "\n";
            if (count($translations) > 0) {
                $content .= "\n" . '#--- ' . str_pad(ltrim($count) . $text . ' ', 74, '-') . "\n\n";
            }
            // List
            $lastKey = [];
            $linebreak = '';
            // We want an empty line before each 1st level key
            foreach ($translations as $key => $tdata) {
                // Key
                if ($type == 'DoneKey') {
                    if ($nested) {
                        $differs = false;
                        for ($level = 0, $end = count($tdata['key']) - 1; $level < $end; $level++) {
                            if ($differs || $level >= count($lastKey) - 1 || $lastKey[$level] != $tdata['key'][$level]) {
                                $differs = true;
                                if ($level === 0) {
                                    $content .= $linebreak;
                                    $linebreak = "\n";
                                }
                                $content .= str_repeat($indent, $level) . $tdata['key'][$level] . ':' . "\n";
                            }
                        }
                        $lastKey = $tdata['key'];
                        $content .= str_repeat($indent, $level) . $tdata['key'][$level] . ': ';
                    } else {
                        $key2 = $tdata['key'][0] . (isset($tdata['key'][1]) ? '.' . $tdata['key'][1] : '');
                        if ($key2 !== $lastKey) {
                            $content .= $linebreak;
                            $linebreak = "\n";
                            $lastKey = $key2;
                        }
                        $content .= $key . ': ';
                    }
                } else {
                    $content .= Escaper::escapeWithDoubleQuotes($key) . ': ';
                }
                // Value
                if ($tdata['trans'] === '') {
                    $thint = Trans::__($key);
                    if ($thint === $key) {
                        $thint = isset($hinting[$key]) ? $hinting[$key] : '';
                    }
                    $content .= '#' . ($thint ? ' ' . Escaper::escapeWithDoubleQuotes($thint) : '') . "\n";
                } else {
                    $content .= Escaper::escapeWithDoubleQuotes($tdata['trans']) . "\n";
                }
            }
        }
        return $status . $content;
    }