我正在使用 make4ht 将 .tex 文件转换为 HTML,并将谷歌翻译 API 添加到生成的网站,但有些元素不应该翻译,而向元素添加 Translation="no" 属性可以实现这一点,例如:
<div>This will be translated</div>
<div translate="no">This will NOT be translated</div>
需要具有该属性的元素有:
\begin{lstlisting}
...
\end{lstlisting}
and
\lstinputlisting[]{}
我对 tex 文件的 MWE 如下:
\documentclass{article}
\usepackage{listings}
\begin{document}
This should be translated
\begin{lstlisting}
This shouldn't be translated
\end{lstlisting}
\end{document}
并添加谷歌翻译的.cfg文件:
\Preamble{xhtml,html5}
\Configure{BODY}{
\HCode{<div id="google_translate_element"></div>
\Hnewline
<script type="text/javascript">
function googleTranslateElementInit() {
new google.translate.TranslateElement({pageLanguage: 'en', layout: google.translate.TranslateElement.InlineLayout.HORIZONTAL}, 'google_translate_element');
}
</script>
\Hnewline
<script type="text/javascript" src="https://translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>}}{}
\begin{document}
\EndPreamble
答案1
尝试这个配置文件:
\Preamble{xhtml,html5}
\Configure{BODY}{
\HCode{<div id="google_translate_element"></div>
\Hnewline
<script type="text/javascript">
function googleTranslateElementInit() {
new google.translate.TranslateElement({pageLanguage: 'en', layout: google.translate.TranslateElement.InlineLayout.HORIZONTAL}, 'google_translate_element');
}
</script>
\Hnewline
<script type="text/javascript" src="https://translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>}}{}
\AddToHook{env/lstlisting/before}{\ifvmode\IgnorePar\fi\EndP\HCode{<div translate="no">}}
\AddToHook{env/lstlisting/after}{\ifvmode\IgnorePar\fi\EndP\HCode{</div>}}
\catcode`\:=11
\Configure{lstinputlisting}
{\ifvmode \IgnorePar\fi \EndP
\HCode{<!--l. \the\inputlineno-->}%
\gdef\start:LstLn{\HCode{<pre class="lstinputlisting" id="listing-\listingN" translate="no">}\gdef\start:LstLn{\HCode{\Hnewline}}}% ignore first newline, to prevent spurious line
\bgroup\ttfamily%\special{t4ht@(}%
\Configure{listings}%
{{\everypar{}\leavevmode}}%
{{\everypar{}\leavevmode}}%
{\start:LstLn\HCode{<span class="label">}}%
{\HCode{</span>}}%
}
{%\special{t4ht@)}
\egroup%
\ifvmode \IgnorePar\fi \EndP \HCode{</pre>}\par}%
\catcode`\:=12
\begin{document}
\EndPreamble
对于环境,我们使用\AddToHook
命令来添加和添加<div>
元素。对于\lstinputlisting
,我们需要更改原始的 TeX4ht 配置,因此代码更长。与原始版本相比,唯一的变化是属性translate="no"
。
结果如下: