<commit_before><?php
/**
 * This tool generates the .po files for each application.
 * The resulting files can be found in $ROOT/po/
 */

namespace Skeleton\I18n;

// Fetch paths for Applications
$applications = \Skeleton\Core\Application::get_all();

$paths = [];
foreach ($applications as $application) {
	$paths[$application->name] = $application->path;
}

// Manually add mails and PDF documents
//$paths['email'] = STORE_PATH . '/email/template';
//$paths['pdf'] = STORE_PATH . '/pdf/template';

// Translate all the applications
foreach ($paths as $application => $directory) {
	translate_application($application, $directory);
}

/**
 * Translate an application
 *
 * @param string $application Name of the application
 * @param string $directory Application path
 */
function translate_application($application, $directory) {
	echo 'translating ' . $application . ' (' . $directory . ')';

	// Fetch the templates in this directory
	$templates = get_templates($directory);
	$strings = [];

	// Parse all the files we found
	foreach ($templates as $template) {
		$strings = array_merge($strings, get_strings($template));
	}

	// Translate the strings
	$languages = Language::get_all();
	foreach ($languages as $language) {
		// Don't create a .po file if it is our base_language
		if ($language->name_short == Config::$base_language) {
			continue;
		}

		echo ' ' . $language->name_short;

		// If we already have a (partially) translated file, merge
		if (file_exists(PO_PATH . '/' . $language->name_short . '/' . $application . '.po')) {
			$translated = Util::load(PO_PATH . '/' . $language->name_short . '/' . $application . '.po');
			$old_translated = Util::load(PO_PATH . '/' . $language->name_short . '.po');
			$translated = array_merge($translated, $old_translated);
		} else {
			$translated = [];
		}

		// Create a new array with the merged translations
		$new_po = [];
		foreach ($strings as $string) {
			if (isset($translated[$string]) and $translated[$string] != '') {
				$new_po[$string] = $translated[$string];
			} else {
				$new_po[$string] = '';
			}
		}

		// Stop doing what we are doing if there are no strings anyway
		if (count($new_po) == 0) {
			continue;
		}

		// And save!
		Util::save(PO_PATH . '/' . $language->name_short . '/' . $application . '.po', $application, $language, $new_po);
	}

	echo "\n";
}

/**
 * Parse all translatable stings out of a file
 *
 * @param string $file The full path of the file to parse
 */
function get_strings($file) {
	$content = file_get_contents($file);

	/**
	 * {% trans "string" %}
	 */
	preg_match_all("/\{%\s*trans \"(.*?)\"\s*%\}/", $content, $matches);
	$twig_strings = unescape_strings($matches[1], '"');

	/**
	 * {% trans 'string' %}
	 */
	preg_match_all("/\{%\s*trans '(.*?)'\s*%\}/", $content, $matches);
	$twig_strings2 = unescape_strings($matches[1], '\'');

	/**
	 * 'string'|trans
	 */
	preg_match_all('/\'((?:[^\'\\\\]|\\\\.)*)\'\|trans/', $content, $matches);
	$twig_strings3 = unescape_strings($matches[1], '\'');<commit_msg>update script to create po files<commit_after><?php
/**
 * This tool generates the .po files for each application.
 * The resulting files can be found in $ROOT/po/
 */

namespace Skeleton\I18n;

// Fetch paths for Applications
$applications = \Skeleton\Core\Application::get_all();

$paths = [];
foreach ($applications as $application) {
	$paths[$application->name] = $application->path;
}

// Manually add mails and PDF documents
//$paths['email'] = STORE_PATH . '/email/template';
//$paths['pdf'] = STORE_PATH . '/pdf/template';

// Translate all the applications
foreach ($paths as $application => $directory) {
	translate_application($application, $directory);
}

/**
 * Translate an application
 *
 * @param string $application Name of the application
 * @param string $directory Application path
 */
function translate_application($application, $directory) {
	echo 'translating ' . $application . ' (' . $directory . ')';

	// Fetch the templates in this directory
	$templates = get_templates($directory);
	$strings = [];

	// Parse all the files we found
	foreach ($templates as $template) {
		$strings = array_merge($strings, get_strings($template));
	}

	// Translate the strings
	$languages = Language::get_all();
	foreach ($languages as $language) {
		// Don't create a .po file if it is our base_language
		if ($language->name_short == Config::$base_language) {
			continue;
		}

		echo ' ' . $language->name_short;

		// If we already have a (partially) translated file, merge
		if (file_exists(Config::$po_directory . '/' . $language->name_short . '/' . $application . '.po')) {
			$translated = Util::load(Config::$po_directory . '/' . $language->name_short . '/' . $application . '.po');
			$old_translated = Util::load(Config::$po_directory . '/' . $language->name_short . '.po');
			$translated = array_merge($translated, $old_translated);
		} else {
			$translated = [];
		}

		// Create a new array with the merged translations
		$new_po = [];
		foreach ($strings as $string) {
			if (isset($translated[$string]) and $translated[$string] != '') {
				$new_po[$string] = $translated[$string];
			} else {
				$new_po[$string] = '';
			}
		}

		// Stop doing what we are doing if there are no strings anyway
		if (count($new_po) == 0) {
			continue;
		}

		// And save!
		Util::save(Config::$po_directory . '/' . $language->name_short . '/' . $application . '.po', $application, $language, $new_po);
	}

	echo "\n";
}

/**
 * Parse all translatable stings out of a file
 *
 * @param string $file The full path of the file to parse
 */
function get_strings($file) {
	$content = file_get_contents($file);

	/**
	 * {% trans "string" %}
	 */
	preg_match_all("/\{%\s*trans \"(.*?)\"\s*%\}/", $content, $matches);
	$twig_strings = unescape_strings($matches[1], '"');

	/**
	 * {% trans 'string' %}
	 */
	preg_match_all("/\{%\s*trans '(.*?)'\s*%\}/", $content, $matches);
	$twig_strings2 = unescape_strings($matches[1], '\'');

	/**
	 * 'string'|trans
	 */
	preg_match_all('/\'((?:[^\'\\\\]|\\\\.)*)\'\|trans/', $content, $matches);
	$twig_strings3 = unescape_strings($matches[1], '\'');
{'fraction_non_alphanumeric': 0.19140943981708314, 'fraction_numerical': 0.0026130981544994283, 'mean_word_length': 3.710769230769231, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5355698', 'n_tokens_mistral': 2130, 'n_tokens_neox': 1959, 'n_words': 497}