@@ -67,7 +67,16 @@ class Preprocessors:
67
67
"""
68
68
69
69
@staticmethod
70
- def process_translations (context : dict ) -> dict :
70
+ def current_year (context : dict ) -> dict :
71
+ """
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 :
71
80
"""
72
81
Download the translations from the GitHub repository and extract them.
73
82
"""
@@ -90,12 +99,25 @@ def process_translations(context: dict) -> dict:
90
99
return context
91
100
92
101
@staticmethod
93
- def current_year (context : dict ) -> dict :
102
+ def add_navbar_content (context : dict ) -> dict :
94
103
"""
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 .
104
+ Add the original and translated navbar content to the context, so it can
105
+ be used in the templates .
97
106
"""
98
- context ["current_year" ] = datetime .datetime .now ().year
107
+ context ["navbar" ] = {}
108
+ context ["navbar_translations" ] = {}
109
+ for lang in context ["translations" ]["languages" ]:
110
+ with open (
111
+ os .path .join (
112
+ context ["source_path" ],
113
+ "" if lang == "en" else f"{ lang } " ,
114
+ context ["main" ]["navbar_fname" ],
115
+ ),
116
+ encoding = "utf-8" ,
117
+ ) as f :
118
+ navbar_lang = yaml .safe_load (f )
119
+ context ["navbar_translations" ][lang ] = navbar_lang ["navbar" ]
120
+ context ["navbar" ] = context ["navbar_translations" ]["en" ]
99
121
return context
100
122
101
123
@staticmethod
@@ -106,36 +128,37 @@ def navbar_add_info(context: dict) -> dict:
106
128
``has_subitems`` that tells which one of them every element is. It
107
129
also adds a ``slug`` field to be used as a CSS id.
108
130
"""
131
+ for i , item in enumerate (context ["navbar" ]):
132
+ context ["navbar" ][i ] = dict (
133
+ item ,
134
+ has_subitems = isinstance (item ["target" ], list ),
135
+ slug = (item ["name" ].replace (" " , "-" ).lower ()),
136
+ )
137
+ return context
138
+
139
+ @staticmethod
140
+ def navbar_add_translated_info (context : dict ) -> dict :
141
+ """
142
+ Add the translated navbar content to the context, so it can be used in
143
+ the templates. The translated navbar content is stored in the
144
+ """
109
145
110
- def update_target (item : dict , url_prefix : str ) -> None :
146
+ def update_target (item : dict , prefix : str ) -> None :
111
147
if item .get ("translated" , True ):
112
- item ["target" ] = f"{ url_prefix } { item ['target' ]} "
148
+ item ["target" ] = f"{ prefix } / { item ['target' ]} "
113
149
else :
114
150
item ["target" ] = f"../{ item ['target' ]} "
115
151
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" ]):
152
+ for lang in list (context ["translations" ]["languages" ].keys ())[1 :]:
153
+ for i , item in enumerate (context ["navbar_translations" ][lang ]):
130
154
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 )
137
-
138
- context ["navbar" ][lang ][i ] = dict (
155
+ if has_subitems :
156
+ for sub_item in item ["target" ]:
157
+ update_target (sub_item , lang )
158
+ else :
159
+ update_target (item , lang )
160
+
161
+ context ["navbar_translations" ][lang ][i ] = dict (
139
162
item ,
140
163
has_subitems = has_subitems ,
141
164
slug = (item ["name" ].replace (" " , "-" ).lower ()),
0 commit comments