]> git.openstreetmap.org Git - nominatim.git/blob - src/nominatim_api/localization.py
Locales and localization refactor with Locales as a localizer object.
[nominatim.git] / src / nominatim_api / localization.py
1 # SPDX-License-Identifier: GPL-3.0-or-later
2 #
3 # This file is part of Nominatim. (https://nominatim.org)
4 #
5 # Copyright (C) 2024 by the Nominatim developer community.
6 # For a full list of authors see the git log.
7 """
8 Helper functions for localizing names of results.
9 """
10 from typing import Mapping, List, Optional
11 from .config import Configuration
12 from .results import AddressLines, BaseResultT
13
14 import re
15
16
17 class Locales:
18     """ Helper class for localization of names.
19
20         It takes a list of language prefixes in their order of preferred
21         usage.
22     """
23
24     def __init__(self, langs: Optional[List[str]] = None):
25         self.config = Configuration(None)
26         self.languages = langs or []
27         self.name_tags: List[str] = []
28
29         parts = self.config.OUTPUT_NAMES.split(',')
30
31         for part in parts:
32             part = part.strip()
33             if part.endswith(":XX"):
34                 self._add_lang_tags(part[:-3])
35             else:
36                 self._add_tags(part)
37
38     def __bool__(self) -> bool:
39         return len(self.languages) > 0
40
41     def _add_tags(self, *tags: str) -> None:
42         for tag in tags:
43             self.name_tags.append(tag)
44             self.name_tags.append(f"_place_{tag}")
45
46     def _add_lang_tags(self, *tags: str) -> None:
47         for tag in tags:
48             for lang in self.languages:
49                 self.name_tags.append(f"{tag}:{lang}")
50                 self.name_tags.append(f"_place_{tag}:{lang}")
51
52     def display_name(self, names: Optional[Mapping[str, str]]) -> str:
53         """ Return the best matching name from a dictionary of names
54             containing different name variants.
55
56             If 'names' is null or empty, an empty string is returned. If no
57             appropriate localization is found, the first name is returned.
58         """
59         if not names:
60             return ''
61
62         if len(names) > 1:
63             for tag in self.name_tags:
64                 if tag in names:
65                     return names[tag]
66
67         # Nothing? Return any of the other names as a default.
68         return next(iter(names.values()))
69
70     @staticmethod
71     def from_accept_languages(langstr: str) -> 'Locales':
72         """ Create a localization object from a language list in the
73             format of HTTP accept-languages header.
74
75             The functions tries to be forgiving of format errors by first splitting
76             the string into comma-separated parts and then parsing each
77             description separately. Badly formatted parts are then ignored.
78         """
79         # split string into languages
80         candidates = []
81         for desc in langstr.split(','):
82             m = re.fullmatch(r'\s*([a-z_-]+)(?:;\s*q\s*=\s*([01](?:\.\d+)?))?\s*',
83                              desc, flags=re.I)
84             if m:
85                 candidates.append((m[1], float(m[2] or 1.0)))
86
87         # sort the results by the weight of each language (preserving order).
88         candidates.sort(reverse=True, key=lambda e: e[1])
89
90         # If a language has a region variant, also add the language without
91         # variant but only if it isn't already in the list to not mess up the weight.
92         languages = []
93         for lid, _ in candidates:
94             languages.append(lid)
95             parts = lid.split('-', 1)
96             if len(parts) > 1 and all(c[0] != parts[0] for c in candidates):
97                 languages.append(parts[0])
98
99         return Locales(languages)
100
101     def localize(self, lines: AddressLines) -> None:
102         """ Sets the local name of address parts according to the chosen
103             locale.
104
105             Only address parts that are marked as isaddress are localized.
106
107             AddressLines should be modified in place.
108         """
109         for line in lines:
110             if line.isaddress and line.names:
111                 line.local_name = self.display_name(line.names)
112
113     def localize_results(self, results: List[BaseResultT]) -> None:
114         """ Set the local name of results according to the chosen
115             locale.
116         """
117         for result in results:
118             result.locale_name = self.display_name(result.names)
119             if result.address_rows:
120                 self.localize(result.address_rows)