• Модуль: translate
  • Путь к файлу: ~/bitrix/modules/translate/lib/file.php
  • Класс: BitrixTranslateFile
  • Вызов: File::save
public function save(): bool
{
	// language id
	$langId = $this->getLangId();
	if (empty($langId))
	{
		throw new MainSystemException("Language Id must be filled");
	}

	// encoding
	$operatingEncoding = $this->getOperatingEncoding();
	$sourceEncoding = $this->getSourceEncoding();
	$convertEncoding = (mb_strtolower($operatingEncoding) != mb_strtolower($sourceEncoding));
	if ($convertEncoding)
	{
		$path = MainLocalizationTranslation::convertLangPath($this->getPhysicalPath(), $this->getLangId());

		if (MainLocalizationTranslation::getDeveloperRepositoryPath() !== null)
		{
			$convertEncoding = (stripos($path, MainLocalizationTranslation::getDeveloperRepositoryPath()) === 0);
		}
		if (!$convertEncoding && MainLocalizationTranslation::useTranslationRepository())
		{
			$convertEncoding = (stripos($path, MainLocalizationTranslation::getTranslationRepositoryPath()) === 0);
		}
	}

	$content = '';
	foreach ($this->messages as $phraseId => $phrase)
	{
		if (empty($phrase) && $phrase !== '0')
		{
			// remove empty
			continue;
		}
		$phrase = str_replace(["rn", "r"], ["n", ''], $phrase);
		if ($convertEncoding)
		{
			$phrase = MainTextEncoding::convertEncoding($phrase, $operatingEncoding, $sourceEncoding);
		}
		$enclosure = '"';
		if (isset($this->messageEnclosure[$phraseId]))
		{
			$enclosure = $this->messageEnclosure[$phraseId];// preserve origin quote
		}

		$phraseId = StringHelper::escapePhp($phraseId, '"', "\\");
		if (StringHelper::hasPhpTokens($phraseId, '"'))
		{
			$this->addError(new MainError("Phrase code contains php tokens"));
			return false;
		}

		$phrase = StringHelper::escapePhp($phrase, $enclosure);
		if (StringHelper::hasPhpTokens($phrase, $enclosure))
		{
			$this->addError(new MainError("Phrase contains php tokens"));
			return false;
		}

		$row = '$MESS["'. $phraseId. '"] = ';
		if ($enclosure === '<<<')
		{
			$row .= "<< '')
	{
		set_error_handler(
			function ($severity, $message, $file, $line)
			{
				throw new ErrorException($message, $severity, $severity, $file, $line);
			}
		);

		try
		{
			$result = parent::putContents('getMessage());
		}

		restore_error_handler();

		if ($result === false)
		{
			$filePath = $this->getPath();
			throw new MainIOIoException("Couldn't write language file '{$filePath}'");
		}
	}
	else
	{
		// todo: Add module setting that will allow / disallow drop empty lang files.
		if ($this->isExists())
		{
			$this->markWritable();
			$this->delete();
		}
	}

	return true;
}