<commit_before><?php

namespace cmd\i18n;

use aurora\file\util as futl;
use Gettext\Loader\PoLoader;


class po2js extends po2php
{
	const desc = "Create js translation from po";


	protected function escape($str)
	{
		return str_replace(["\n", "\r"], ["\\n", "\\r"], parent::escape($str));
	}


	public function exec($poFile, $jsFile="php://stdout")
	{
		$loader = new PoLoader();

		$trans = @$loader->loadFile($poFile);

		$lang     = basename($jsFile, ".js");
		$plurForm = $trans->getHeaders()->getPluralForm();
		$nplurals = (int)($plurForm[0] ?? 2);
		$plural   = $this->checkPlural($plurForm[1] ?? "n != 1");
		$msgs     = "";

		foreach ($trans->getTranslations() as $tr) {

			if ($tr->isDisabled())
				continue;

			$id  = $this->escape($tr->getOriginal());
			$msg = $this->escape($tr->getTranslation());

			$msgs .= "\n\t'$id': ";

			if ($tr->getPlural() !== NULL) {<commit_msg>i18n/po2js: skip untranslated entries<commit_after><?php

namespace cmd\i18n;

use aurora\file\util as futl;
use aurora\i18n\util;
use Gettext\Loader\PoLoader;


class po2js extends po2php
{
	const desc = "Create js translation from po";


	protected function escape($str)
	{
		return str_replace(["\n", "\r"], ["\\n", "\\r"], parent::escape($str));
	}


	public function exec($poFile, $jsFile="php://stdout")
	{
		$loader = new PoLoader();

		$trans = @$loader->loadFile($poFile);

		$lang     = basename($jsFile, ".js");
		$plurForm = $trans->getHeaders()->getPluralForm();
		$nplurals = (int)($plurForm[0] ?? 2);
		$plural   = $this->checkPlural($plurForm[1] ?? "n != 1");
		$msgs     = "";

		foreach ($trans->getTranslations() as $tr) {

			if ($tr->isDisabled() || !util::isTranslated($tr, $nplurals))
				continue;

			$id  = $this->escape($tr->getOriginal());
			$msg = $this->escape($tr->getTranslation());

			$msgs .= "\n\t'$id': ";

			if ($tr->getPlural() !== NULL) {
{'fraction_non_alphanumeric': 0.22346666666666667, 'fraction_numerical': 0.0112, 'mean_word_length': 3.8854166666666665, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 25, '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': '1502592', 'n_tokens_mistral': 771, 'n_tokens_neox': 690, 'n_words': 141}