From 9108eb055d773d984cb43b49ee4904d98709e688 Mon Sep 17 00:00:00 2001 From: Matthieu Marcillaud <marcimat@rezo.net> Date: Mon, 5 Jun 2023 18:09:32 +0200 Subject: [PATCH] =?UTF-8?q?feat:=20Un=20fichier=20d=E2=80=99export=20au=20?= =?UTF-8?q?format=20SPIP=205=20(compatible=20SPIP=204.1+=20aussi)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- export_lang/spip5.php | 101 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 export_lang/spip5.php diff --git a/export_lang/spip5.php b/export_lang/spip5.php new file mode 100644 index 0000000..24f27a3 --- /dev/null +++ b/export_lang/spip5.php @@ -0,0 +1,101 @@ +<?php + +/** + * + * Trad-lang v2 + * Plugin SPIP de traduction de fichiers de langue + * © Florent Jugla, Fil, kent1 + * + * Fichier des fonctions spécifiques du plugin + */ + +if (!defined('_ECRIRE_INC_VERSION')) { + return; +} + +/** + * Fonction d'export d'une langue d'un module SPIP 5+ en php + * + * `return [...];` + * + * @param string $module + * Le module à exporter + * @param string $langue + * La langue à exporter + * @param string $dir_lang + * Le répertoire où stocker les fichiers de langue + * @param bool $tout + * Si true, exporte toutes les chaines même non traduites + * @return string $fichier + * Le fichier final + */ +function export_lang_spip5_dist($module, $langue, $dir_lang, $tout = false) { + $url_trad = null; + /** + * Le fichier final + * local/cache-lang/module_lang.php + */ + $fichier = $dir_lang . '/' . $module . '_' . $langue . '.php'; + + $tab = "\t"; + $where = 'module = ' . sql_quote($module) . ' AND lang = ' . sql_quote($langue); + if (!$tout) { + $where .= " AND statut IN ('OK','MODIF','RELIRE')"; + } + $res = sql_allfetsel('id,str,comm,statut', 'spip_tradlangs', $where, 'id'); + $x = []; + $prev = ''; + $tous = array_column($res, null, 'id'); + ksort($tous); + foreach ($tous as $row) { + if ($prev !== strtoupper($row['id'][0])) { + $x[] = "\n$tab// " . strtoupper($row['id'][0]); + } + $prev = strtoupper($row['id'][0]); + if (strlen($row['statut']) && $row['statut'] != 'OK') { + $row['comm'] .= ' ' . $row['statut']; + } + if (trim($row['comm'])) { + /** + * on rajoute les commentaires ? + */ + $row['comm'] = ' # ' . trim($row['comm']); + } + + $str = $row['str']; + + $oldmd5 = md5($str); + $str = unicode_to_utf_8(html_entity_decode(preg_replace('/&([lg]t;)/S', '&\1', $str), ENT_NOQUOTES, 'utf-8')); + $newmd5 = md5($str); + if ($oldmd5 !== $newmd5) { + sql_updateq('spip_tradlangs', ['md5' => $newmd5], 'md5 = ' . sql_quote($oldmd5) . ' AND module = ' . sql_quote($module)); + } + + $x[] = "$tab" . var_export($row['id'], 1) . ' => ' . var_export($str, 1) . ',' . $row['comm'] ; + } + + $fd = fopen($fichier, 'w'); + + $contenu = implode("\n", $x); + $contenu = str_replace("\r\n", "\n", $contenu); + + /** + * On écrit le fichier + */ + fwrite($fd, <<<PHP +<?php +// This is a SPIP language file -- Ceci est un fichier langue de SPIP +// extrait automatiquement de ' . $url_trad . ' +// ** ne pas modifier le fichier ** + +return [ +$contenu +]; +PHP + ); + + fclose($fd); + @chmod($fichier, 0666); + + return $fichier; +} -- GitLab