OLD | NEW |
1 # coding: utf-8 | 1 # coding: utf-8 |
2 | 2 |
3 # This file is part of the Adblock Plus web scripts, | 3 # This file is part of the Adblock Plus web scripts, |
4 # Copyright (C) 2006-2015 Eyeo GmbH | 4 # Copyright (C) 2006-2015 Eyeo GmbH |
5 # | 5 # |
6 # Adblock Plus is free software: you can redistribute it and/or modify | 6 # Adblock Plus is free software: you can redistribute it and/or modify |
7 # it under the terms of the GNU General Public License version 3 as | 7 # it under the terms of the GNU General Public License version 3 as |
8 # published by the Free Software Foundation. | 8 # published by the Free Software Foundation. |
9 # | 9 # |
10 # Adblock Plus is distributed in the hope that it will be useful, | 10 # Adblock Plus is distributed in the hope that it will be useful, |
(...skipping 19 matching lines...) Expand all Loading... |
30 parsed = urlparse.urlparse(url) | 30 parsed = urlparse.urlparse(url) |
31 page = parsed.path | 31 page = parsed.path |
32 if parsed.scheme != "" or page.startswith("/") or page.startswith("."): | 32 if parsed.scheme != "" or page.startswith("/") or page.startswith("."): |
33 # Not a page link | 33 # Not a page link |
34 return None, None | 34 return None, None |
35 | 35 |
36 if page == "" and url != "": | 36 if page == "" and url != "": |
37 # Page-relative link | 37 # Page-relative link |
38 return None, None | 38 return None, None |
39 | 39 |
40 def has_locale(locale, page): | |
41 try: | |
42 page = config.get("locale_overrides", page) | |
43 except ConfigParser.Error: | |
44 pass | |
45 return self.has_locale(locale, page) | |
46 | |
47 config = self.read_config() | 40 config = self.read_config() |
48 default_locale = config.get("general", "defaultlocale") | 41 default_locale = config.get("general", "defaultlocale") |
49 default_page = config.get("general", "defaultpage") | 42 default_page = config.get("general", "defaultpage") |
50 alternative_page = "/".join([page, default_page]).lstrip("/") | 43 alternative_page = "/".join([page, default_page]).lstrip("/") |
51 | 44 |
52 if self.has_localizable_file(default_locale, page): | 45 if self.has_localizable_file(default_locale, page): |
53 if not self.has_localizable_file(locale, page): | 46 if not self.has_localizable_file(locale, page): |
54 locale = default_locale | 47 locale = default_locale |
55 elif self.has_page(page): | 48 elif self.has_page(page): |
56 if not has_locale(locale, page): | 49 if not self.has_locale(locale, page): |
57 locale = default_locale | 50 locale = default_locale |
58 elif self.has_page(alternative_page): | 51 elif self.has_page(alternative_page): |
59 if not has_locale(locale, alternative_page): | 52 if not self.has_locale(locale, alternative_page): |
60 locale = default_locale | 53 locale = default_locale |
61 else: | 54 else: |
62 logging.warning("Link to %s cannot be resolved", page) | 55 logging.warning("Link to %s cannot be resolved", page) |
63 | 56 |
64 parts = page.split("/") | 57 parts = page.split("/") |
65 if parts[-1] == default_page: | 58 if parts[-1] == default_page: |
66 page = "/".join(parts[:-1]) | 59 page = "/".join(parts[:-1]) |
67 | 60 |
68 path = "/%s/%s" % (locale, page) | 61 path = "/%s/%s" % (locale, page) |
69 return locale, urlparse.urlunparse(parsed[0:2] + (path,) + parsed[3:]) | 62 return locale, urlparse.urlunparse(parsed[0:2] + (path,) + parsed[3:]) |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 def list_locales(self): | 143 def list_locales(self): |
151 result = set() | 144 result = set() |
152 for filename in self.list_files("locales"): | 145 for filename in self.list_files("locales"): |
153 if "/" in filename: | 146 if "/" in filename: |
154 locale, path = filename.split("/", 1) | 147 locale, path = filename.split("/", 1) |
155 result.add(locale) | 148 result.add(locale) |
156 return result | 149 return result |
157 | 150 |
158 def has_locale(self, locale, page): | 151 def has_locale(self, locale, page): |
159 config = self.read_config() | 152 config = self.read_config() |
160 if config.has_option("locale_overrides", page): | 153 try: |
161 page = config.get("locale_overrides", page) | 154 page = config.get("locale_overrides", page) |
| 155 except ConfigParser.Error: |
| 156 pass |
162 return self.has_file(self.locale_filename(locale, page)) | 157 return self.has_file(self.locale_filename(locale, page)) |
163 | 158 |
164 def read_locale(self, locale, page): | 159 def read_locale(self, locale, page): |
165 default_locale = self.read_config().get("general", "defaultlocale") | 160 default_locale = self.read_config().get("general", "defaultlocale") |
166 if locale == default_locale: | 161 if locale == default_locale: |
167 result = {} | 162 result = {} |
168 else: | 163 else: |
169 result = dict(self.read_locale(default_locale, page)) | 164 result = dict(self.read_locale(default_locale, page)) |
170 | 165 |
171 if self.has_locale(locale, page): | 166 if self.has_locale(locale, page): |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
282 path = os.path.join(dir, filename) | 277 path = os.path.join(dir, filename) |
283 if os.path.isfile(path): | 278 if os.path.isfile(path): |
284 result.append(relpath + filename) | 279 result.append(relpath + filename) |
285 elif os.path.isdir(path): | 280 elif os.path.isdir(path): |
286 do_list(path, relpath + filename + "/") | 281 do_list(path, relpath + filename + "/") |
287 do_list(self.get_path(subdir), "") | 282 do_list(self.get_path(subdir), "") |
288 return result | 283 return result |
289 | 284 |
290 def get_cache_dir(self): | 285 def get_cache_dir(self): |
291 return os.path.join(self._dir, "cache") | 286 return os.path.join(self._dir, "cache") |
OLD | NEW |