• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

rero / sonar / 17425918180

03 Sep 2025 07:11AM UTC coverage: 95.796% (-0.6%) from 96.378%
17425918180

push

github

PascalRepond
translations: extract messages

Co-Authored-by: Pascal Repond <pascal.repond@rero.ch>

7816 of 8159 relevant lines covered (95.8%)

0.96 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

86.96
/sonar/translations/rest.py
1
# Swiss Open Access Repository
2
# Copyright (C) 2021 RERO
3
#
4
# This program is free software: you can redistribute it and/or modify
5
# it under the terms of the GNU Affero General Public License as published by
6
# the Free Software Foundation, version 3 of the License.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU Affero General Public License for more details.
12
#
13
# You should have received a copy of the GNU Affero General Public License
14
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
15

16
"""Rest endpoints for translations."""
17

18
import os
1✔
19

20
import polib
1✔
21
from flask import Blueprint, abort, current_app, jsonify
1✔
22

23
api_blueprint = Blueprint("translations", __name__)
1✔
24

25

26
@api_blueprint.route("/translations/<lang>.json")
1✔
27
def get_translations(lang):
1✔
28
    """Exposes translations in JSON format.
29

30
    :param lang: language ISO 639-1 Code (two chars).
31
    """
32
    babel = current_app.extensions["babel"]
1✔
33
    paths = babel.default_directories
1✔
34
    try:
1✔
35
        path = next(p for p in paths if p.find("sonar/translations") > -1)
1✔
36
    except StopIteration:
×
37
        current_app.logger.error(f"translations for {lang} does not exist")
×
38
        abort(404)
×
39

40
    po_file_name = f"{path}/{lang}/LC_MESSAGES/{babel.default_domain}.po"
1✔
41
    if not os.path.isfile(po_file_name):
1✔
42
        abort(404)
1✔
43
    try:
1✔
44
        po = polib.pofile(po_file_name)
1✔
45
    except Exception:
1✔
46
        current_app.logger.error(f"unable to open po file: {po_file_name}")
1✔
47
        abort(404)
1✔
48
    data = {entry.msgid: entry.msgstr or entry.msgid for entry in po}
1✔
49
    return jsonify(data)
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc