sumolib.translation

 1# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
 2# Copyright (C) 2009-2026 German Aerospace Center (DLR) and others.
 3# This program and the accompanying materials are made available under the
 4# terms of the Eclipse Public License 2.0 which is available at
 5# https://www.eclipse.org/legal/epl-2.0/
 6# This Source Code may also be made available under the following Secondary
 7# Licenses when the conditions for such availability set forth in the Eclipse
 8# Public License 2.0 are satisfied: GNU General Public License, version 2
 9# or later which is available at
10# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
11# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
12
13# @file    translation.py
14# @author  Mirko Barthauer
15# @date    2023-05-10
16
17import gettext
18import os
19
20CURRENT_LANG = None
21
22
23def addLanguageOption(parser):
24    parser.add_option("--language", type=str, default="en", help="Defines the language of the GUI")
25
26
27def setLanguage(lang):
28    global CURRENT_LANG
29    try:
30        sumoRoot = os.environ.get('SUMO_HOME',
31                                  os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', '..'))
32        translation = gettext.translation("sumo", os.path.join(sumoRoot, 'data', 'locale'), languages=[lang])
33        translation.install()
34        CURRENT_LANG = translation
35    except:  # noqa
36        CURRENT_LANG = None
37
38
39def TL(msgid):
40    if CURRENT_LANG is None:
41        return msgid
42    return CURRENT_LANG.gettext(msgid)
43
44
45def TLF(msgid, *args):
46    msg = TL(msgid)
47    varCount = min(msg.count('%'), len(args))
48    for val in args[:varCount]:
49        msg = msg.replace('%', str(val), 1)
50    return msg
51
52
53def TLC(context, msgid):
54    if CURRENT_LANG is None:
55        return msgid
56    return CURRENT_LANG.pgettext(context, msgid)
CURRENT_LANG = None
def addLanguageOption(parser):
24def addLanguageOption(parser):
25    parser.add_option("--language", type=str, default="en", help="Defines the language of the GUI")
def setLanguage(lang):
28def setLanguage(lang):
29    global CURRENT_LANG
30    try:
31        sumoRoot = os.environ.get('SUMO_HOME',
32                                  os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', '..'))
33        translation = gettext.translation("sumo", os.path.join(sumoRoot, 'data', 'locale'), languages=[lang])
34        translation.install()
35        CURRENT_LANG = translation
36    except:  # noqa
37        CURRENT_LANG = None
def TL(msgid):
40def TL(msgid):
41    if CURRENT_LANG is None:
42        return msgid
43    return CURRENT_LANG.gettext(msgid)
def TLF(msgid, *args):
46def TLF(msgid, *args):
47    msg = TL(msgid)
48    varCount = min(msg.count('%'), len(args))
49    for val in args[:varCount]:
50        msg = msg.replace('%', str(val), 1)
51    return msg
def TLC(context, msgid):
54def TLC(context, msgid):
55    if CURRENT_LANG is None:
56        return msgid
57    return CURRENT_LANG.pgettext(context, msgid)