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
17from __future__ import print_function
18import gettext
19import os
20
21SUMO_HOME = os.environ.get('SUMO_HOME',
22                           os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', '..'))
23LOCALEDIR = os.path.join(SUMO_HOME, 'data', 'locale')
24CURRENT_LANG = None
25
26
27def addLanguageOption(parser):
28    parser.add_option("--language", type=str, default="en", help="Defines the language of the GUI")
29
30
31def setLanguage(lang):
32    global CURRENT_LANG
33    try:
34        translation = gettext.translation("sumo", localedir=LOCALEDIR, languages=[lang])
35        translation.install()
36        CURRENT_LANG = translation
37    except:  # noqa
38        CURRENT_LANG = None
39
40
41def TL(msgid):
42    if CURRENT_LANG is None:
43        return msgid
44    return CURRENT_LANG.gettext(msgid)
45
46
47def TLF(msgid, *args):
48    msg = TL(msgid)
49    varCount = min(msg.count('%'), len(args))
50    for val in args[:varCount]:
51        msg = msg.replace('%', str(val), 1)
52    return msg
53
54
55def TLC(context, msgid):
56    if CURRENT_LANG is None:
57        return msgid
58    return CURRENT_LANG.pgettext(context, msgid)
SUMO_HOME = $SUMO_HOME
LOCALEDIR = '/home/delphi/gcc/sumo/data/locale'
CURRENT_LANG = None
def addLanguageOption(parser):
28def addLanguageOption(parser):
29    parser.add_option("--language", type=str, default="en", help="Defines the language of the GUI")
def setLanguage(lang):
32def setLanguage(lang):
33    global CURRENT_LANG
34    try:
35        translation = gettext.translation("sumo", localedir=LOCALEDIR, languages=[lang])
36        translation.install()
37        CURRENT_LANG = translation
38    except:  # noqa
39        CURRENT_LANG = None
def TL(msgid):
42def TL(msgid):
43    if CURRENT_LANG is None:
44        return msgid
45    return CURRENT_LANG.gettext(msgid)
def TLF(msgid, *args):
48def TLF(msgid, *args):
49    msg = TL(msgid)
50    varCount = min(msg.count('%'), len(args))
51    for val in args[:varCount]:
52        msg = msg.replace('%', str(val), 1)
53    return msg
def TLC(context, msgid):
56def TLC(context, msgid):
57    if CURRENT_LANG is None:
58        return msgid
59    return CURRENT_LANG.pgettext(context, msgid)