From 42e14cf61fcbd14b848bd22bba1b329764fdee8e Mon Sep 17 00:00:00 2001 From: Felix Fischer <f.fischer@ifas.rwth-aachen.de> Date: Tue, 29 Apr 2025 14:45:12 +0200 Subject: [PATCH] Exclude brackets from translation --- src/scientific_plots/__init__.py | 2 +- src/scientific_plots/plot_settings.py | 34 +++++++++++++++++++++++++++ src/scientific_plots/utilities.py | 12 +++++++--- tests/test_plot_manual.py | 2 +- 4 files changed, 45 insertions(+), 5 deletions(-) diff --git a/src/scientific_plots/__init__.py b/src/scientific_plots/__init__.py index f2f6973..8536ab9 100644 --- a/src/scientific_plots/__init__.py +++ b/src/scientific_plots/__init__.py @@ -5,4 +5,4 @@ useful across several different projects and repositories. It also contains stub-files with several data-type annotations for scipy and matplot-lib functions.""" -__version__ = "1.8.10" +__version__ = "1.8.11" diff --git a/src/scientific_plots/plot_settings.py b/src/scientific_plots/plot_settings.py index b3c152d..510db09 100644 --- a/src/scientific_plots/plot_settings.py +++ b/src/scientific_plots/plot_settings.py @@ -13,6 +13,7 @@ import csv from functools import wraps import locale from pathlib import Path +import re import sys from textwrap import dedent from typing import Callable, Generator, Optional, Union, overload @@ -137,6 +138,35 @@ mpl.colormaps.register(rwth_gradient_map) mpl.colormaps.register(rwth_gradient_map_simple) +def germanify_between(string: str, reverse: bool = False) -> str: + """Only apply the German translation to strings + outside of math areas indicated by dollar sings ($)""" + match = re.search(r"\$(.*?)\$", string) + if match: + before = str(string[:match.start()]) + between = str(match.group(0)) + after = str(string[match.end():]) + else: + before = "" + between = "" + after = string + + match = re.search(r"\[.*?\]$", string) + if match: + bracket = match.group(0) + bracket_start = after.find(bracket) + before_bracket = after[:bracket_start] + after_bracket = after[bracket_start + len(bracket):] + else: + bracket = "" + before_bracket = after + after_bracket = "" + + return translate(before, reverse=reverse)\ + + between + translate(before_bracket, reverse=reverse)\ + + bracket + translate(after_bracket, reverse=reverse) + + def _germanify(ax: Axes, reverse: bool = False) -> None: """ translate a figure from english to german. @@ -165,6 +195,10 @@ def _germanify(ax: Axes, reverse: bool = False) -> None: if axi.get_legend(): items += [*axi.get_legend().texts] for item in items: + if r"$" in item.get_text(): + item.set_text( + germanify_between( + item.get_text(), reverse=reverse)) item.set_text(translate(item.get_text(), reverse=reverse)) try: diff --git a/src/scientific_plots/utilities.py b/src/scientific_plots/utilities.py index 2475c1a..a0b6428 100644 --- a/src/scientific_plots/utilities.py +++ b/src/scientific_plots/utilities.py @@ -198,7 +198,7 @@ def use_translator(string: str, lang1: str, lang2: str) -> str: result = translator.translate(string, lang1, lang2) except (OSError, MaxRetryError): return string - return result + return str(result) def translate(string: str, reverse: bool = False) -> str: @@ -207,6 +207,9 @@ def translate(string: str, reverse: bool = False) -> str: @input reverse translate ger->en if set to true @return the german translation of the same string""" + if r"$" in string or not string: + return string + if reverse: string = re.sub(r'(\d+),(\d+)', r'\1\.\2', string) else: @@ -263,12 +266,15 @@ def translate(string: str, reverse: bool = False) -> str: "absolute": "absolut", "plot": "Graph", "orifice": "Blende", - "Hagen–Poiseuille": "Hagen-Poiseuille" + "Hagen–Poiseuille": "Hagen-Poiseuille", + "test": "Test" }) if not reverse and r"\times" not in string: for key, value in _dict.items(): if key in string.lower(): - string = re.sub(key, value, string, flags=re.IGNORECASE) + string = re.sub( + key, value, string, + flags=re.IGNORECASE) break else: string = use_translator(string, "german", "english") diff --git a/tests/test_plot_manual.py b/tests/test_plot_manual.py index 645eaa7..c8a5cf2 100644 --- a/tests/test_plot_manual.py +++ b/tests/test_plot_manual.py @@ -31,7 +31,7 @@ def test_two_plots() -> None: filename.parent.mkdir(exist_ok=True) two_plots(x_test, y_test1, "plot1", x_test, y_test2, "plot2", - "x", "y", + "distance", r"$\dot{m} = \frac{a}{b}$ leakage [bar]", filename) assert filename.exists() -- GitLab