@@ -67,9 +67,25 @@ class Preprocessors:
67
67
"""
68
68
69
69
@staticmethod
70
- def process_translations (context : dict ) -> dict :
70
+ def current_year (context : dict ) -> dict :
71
71
"""
72
- Download the translations from the GitHub repository and extract them.
72
+ Add the current year to the context, so it can be used for the copyright
73
+ note, or other places where it is needed.
74
+ """
75
+ context ["current_year" ] = datetime .datetime .now ().year
76
+ return context
77
+
78
+ @staticmethod
79
+ def download_translated_content (context : dict ) -> dict :
80
+ """
81
+ Download the translations from the mirror translations repository.
82
+ https://github.com/Scientific-Python-Translations/pandas-translations
83
+
84
+ All translated languages are downloaded, extracted and place inside the
85
+ ``pandas`` folder in a separate folder for each language (e.g. ``pandas/es``).
86
+
87
+ The extracted folder and the translations folders are deleted before
88
+ downloading the information, so the translations are always up to date.
73
89
"""
74
90
base_folder = os .path .dirname (__file__ )
75
91
extract_path = os .path .join (base_folder , context ["translations" ]["source_path" ])
@@ -90,50 +106,70 @@ def process_translations(context: dict) -> dict:
90
106
return context
91
107
92
108
@staticmethod
93
- def current_year (context : dict ) -> dict :
109
+ def add_navbar_content (context : dict ) -> dict :
94
110
"""
95
- Add the current year to the context, so it can be used for the copyright
96
- note, or other places where it is needed.
111
+ Add the navbar content to the context.
112
+
113
+ The navbar content is loaded for all available languages.
97
114
"""
98
- context ["current_year" ] = datetime .datetime .now ().year
115
+ context ["navbar" ] = {}
116
+ for lang in context ["translations" ]["languages" ]:
117
+ path = os .path .join (
118
+ context ["source_path" ],
119
+ "" if lang == "en" else f"{ lang } " ,
120
+ context ["main" ]["navbar_fname" ],
121
+ )
122
+ if os .path .exists (path ):
123
+ with open (
124
+ path ,
125
+ encoding = "utf-8" ,
126
+ ) as f :
127
+ navbar_lang = yaml .safe_load (f )
128
+ context ["navbar" ][lang ] = navbar_lang ["navbar" ]
129
+
99
130
return context
100
131
101
132
@staticmethod
102
133
def navbar_add_info (context : dict ) -> dict :
103
134
"""
135
+ Items in the main navigation bar can be direct links, or dropdowns with
136
+ subitems. This context preprocessor adds a boolean field
137
+ ``has_subitems`` that tells which one of them every element is. It
138
+ also adds a ``slug`` field to be used as a CSS id.
139
+ """
140
+ for i , item in enumerate (context ["navbar" ]["en" ]):
141
+ context ["navbar" ]["en" ][i ] = dict (
142
+ item ,
143
+ has_subitems = isinstance (item ["target" ], list ),
144
+ slug = (item ["name" ].replace (" " , "-" ).lower ()),
145
+ )
146
+ return context
147
+
148
+ @staticmethod
149
+ def navbar_add_translated_info (context : dict ) -> dict :
150
+ """
151
+ Prepare the translated navbar information for the template.
152
+
104
153
Items in the main navigation bar can be direct links, or dropdowns with
105
154
subitems. This context preprocessor adds a boolean field
106
155
``has_subitems`` that tells which one of them every element is. It
107
156
also adds a ``slug`` field to be used as a CSS id.
108
157
"""
109
158
110
- def update_target (item : dict , url_prefix : str ) -> None :
159
+ def update_target (item : dict , prefix : str ) -> None :
111
160
if item .get ("translated" , True ):
112
- item ["target" ] = f"{ url_prefix } { item ['target' ]} "
161
+ item ["target" ] = f"{ prefix } / { item ['target' ]} "
113
162
else :
114
163
item ["target" ] = f"../{ item ['target' ]} "
115
164
116
- context ["navbar" ] = {}
117
- for lang in context ["translations" ]["languages" ]:
118
- prefix = "" if lang == "en" else lang
119
- url_prefix = "" if lang == "en" else lang + "/"
120
- with open (
121
- os .path .join (
122
- context ["source_path" ], prefix , context ["main" ]["navbar_fname" ]
123
- ),
124
- encoding = "utf-8" ,
125
- ) as f :
126
- navbar_lang = yaml .safe_load (f )
127
-
128
- context ["navbar" ][lang ] = navbar_lang ["navbar" ]
129
- for i , item in enumerate (navbar_lang ["navbar" ]):
165
+ for lang in list (context ["translations" ]["languages" ].keys ())[1 :]:
166
+ for i , item in enumerate (context ["navbar" ][lang ]):
130
167
has_subitems = isinstance (item ["target" ], list )
131
- if lang != "en" :
132
- if has_subitems :
133
- for sub_item in item ["target" ]:
134
- update_target (sub_item , url_prefix )
135
- else :
136
- update_target (item , url_prefix )
168
+ if has_subitems :
169
+ for sub_item in item ["target" ]:
170
+ update_target (sub_item , lang )
171
+ else :
172
+ update_target (item , lang )
137
173
138
174
context ["navbar" ][lang ][i ] = dict (
139
175
item ,
0 commit comments