From b9cf3c82a18a4782a3aa543c91392c6483f5d2a4 Mon Sep 17 00:00:00 2001 From: Markus Heiser Date: Thu, 3 Feb 2022 16:25:35 +0100 Subject: [mod] add i18n infrastructure for SearXNG message files (searxng.msg) With this patch ``searxng.msg`` files can be added to SearXNG. In ``searxng.msg`` files messages can be defined which are not captured by babel's gettext, like the generic names of the categories or messages that are stored in constants. Signed-off-by: Markus Heiser --- searx/babel_extract.py | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 searx/babel_extract.py (limited to 'searx/babel_extract.py') diff --git a/searx/babel_extract.py b/searx/babel_extract.py new file mode 100644 index 000000000..5f575f6d4 --- /dev/null +++ b/searx/babel_extract.py @@ -0,0 +1,51 @@ +# SPDX-License-Identifier: AGPL-3.0-or-later +# lint: pylint +"""This module implements the :origin:`searxng_msg ` extractor to +extract messages from: + +- None + +The ``searxng.msg`` files are selected by Babel_, see Babel's configuration in +:origin:`babel.cfg`:: + + searxng_msg = searx.babel_extract.extract + ... + [searxng_msg: **/searxng.msg] + +A ``searxng.msg`` file is a python file that is *executed* by the +:py:obj:`extract` function. Additional ``searxng.msg`` files can be added by: + +1. Adding a ``searxng.msg`` file in one of the SearXNG python packages and +2. implement a method in :py:obj:`extract` that yields messages from this file. + +.. _Babel: https://babel.pocoo.org/en/latest/index.html + +""" + +from os import path + +SEARXNG_MSG_FILE = "searxng.msg" +_MSG_FILES = [] + + +def extract( + # pylint: disable=unused-argument + fileobj, + keywords, + comment_tags, + options, +): + """Extract messages from ``searxng.msg`` files by a custom extractor_. + + .. _extractor: + https://babel.pocoo.org/en/latest/messages.html#writing-extraction-methods + """ + if fileobj.name not in _MSG_FILES: + raise RuntimeError("don't know how to extract messages from %s" % fileobj.name) + + namespace = {} + exec(fileobj.read(), {}, namespace) # pylint: disable=exec-used + + for name in namespace['__all__']: + for k, v in namespace[name].items(): + yield 0, '_', v, ["%s['%s']" % (name, k)] -- cgit v1.2.3 From 784bf9ed15aa6e303e3996ef45f8f4281b032928 Mon Sep 17 00:00:00 2001 From: Markus Heiser Date: Thu, 3 Feb 2022 16:33:58 +0100 Subject: [mod] move category and names of constants to searx/searxng.msg Closes: https://github.com/searxng/searxng/issues/814 Signed-off-by: Markus Heiser --- searx/babel_extract.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'searx/babel_extract.py') diff --git a/searx/babel_extract.py b/searx/babel_extract.py index 5f575f6d4..5bcb1f0e9 100644 --- a/searx/babel_extract.py +++ b/searx/babel_extract.py @@ -3,7 +3,7 @@ """This module implements the :origin:`searxng_msg ` extractor to extract messages from: -- None +- :origin:`searx/searxng.msg` The ``searxng.msg`` files are selected by Babel_, see Babel's configuration in :origin:`babel.cfg`:: @@ -25,7 +25,7 @@ A ``searxng.msg`` file is a python file that is *executed* by the from os import path SEARXNG_MSG_FILE = "searxng.msg" -_MSG_FILES = [] +_MSG_FILES = [path.join(path.dirname(__file__), SEARXNG_MSG_FILE)] def extract( -- cgit v1.2.3