Jarves\Translation\Utils::saveLanguage PHP Method

saveLanguage() public method

public saveLanguage ( string $bundle, string $lang, array $translation ) : boolean
$bundle string
$lang string
$translation array
return boolean
    public function saveLanguage($bundle, $lang, $translation)
    {
        $file = $this->getJarves()->resolvePath("@{$bundle}/{$lang}.po", 'Resources/translations');
        @mkdir(dirname($file), 777, true);
        if (!is_writable($file)) {
            throw new FileNotWritableException(sprintf('File `%s` is not writable.', $file));
        }
        $translations = json_decode($translation, true);
        $current = $this->parsePo($file);
        $fh = fopen($file, 'w');
        if ($fh == false) {
            return false;
        }
        $pluralForms = $this->getPluralForm($lang) ?: 'nplurals=2; plural=(n!=1);';
        if ($current) {
            $current['container']['Plural-Forms'] = $pluralForms;
            $current['container']['PO-Revision-Date'] = date('Y-m-d H:iO');
            fwrite($fh, 'msgid ""' . "\n" . 'msgstr ""' . "\n");
            foreach ($current['container'] as $k => $v) {
                fwrite($fh, '"' . $k . ': ' . $v . '\\n"' . "\n");
            }
            fwrite($fh, "\n\n");
        } else {
            //write initial container
            fwrite($fh, '
               msgid ""
               msgstr ""
               "Project-Id-Version: Jarves cms - ' . $bundle . '\\n"
"PO-Revision-Date: ' . date('Y-m-d H:iO') . '\\n"
"Content-Type: text/plain; charset=UTF-8\\n"
"Content-Transfer-Encoding: 8bit\\n"
"Language: ' . $lang . '\\n"
"Plural-Forms: ' . $pluralForms . '\\n"' . "\n\n");
        }
        if (count($translations) > 0) {
            foreach ($translations as $key => $translation) {
                if (strpos($key, "") !== false) {
                    //we have a context
                    $context = self::toPoString(substr($key, 0, strpos($key, "")));
                    $id = self::toPoString(substr($key, strpos($key, "") + 1));
                    fwrite($fh, 'msgctxt ' . $context . "\n");
                    fwrite($fh, 'msgid ' . $id . "\n");
                } else {
                    fwrite($fh, 'msgid ' . self::toPoString($key) . "\n");
                }
                if (is_array($translation)) {
                    fwrite($fh, 'msgid_plural ' . self::toPoString($translation['plural']) . "\n");
                    unset($translation['plural']);
                    foreach ($translation as $k => $v) {
                        fwrite($fh, 'msgstr[' . $k . '] ' . self::toPoString($v) . "\n");
                    }
                } else {
                    fwrite($fh, 'msgstr ' . self::toPoString($translation) . "\n");
                }
                fwrite($fh, "\n");
            }
        }
        fclose($fh);
        $this->cacher->invalidateCache('core/lang');
        return true;
    }