|
| 1 | +"""Server-side i18n for observation dimension UI labels (listbox, table, KPI, JSON).""" |
| 2 | + |
| 3 | +from django.utils.translation import gettext |
| 4 | +from django.utils.translation import gettext_lazy as _ |
| 5 | + |
| 6 | +_COMMON_KPI = _("Documents") |
| 7 | +_COMMON_COL = _("Year") |
| 8 | +_COMMON_VALUE = _("Documents") |
| 9 | + |
| 10 | +OBSERVATION_SELECT_LABEL_SCIENTIFIC = _( |
| 11 | + "Select an observation table on the evolution of scientific production by:" |
| 12 | +) |
| 13 | +OBSERVATION_SELECT_LABEL_SOCIAL = _( |
| 14 | + "Select an observation table on social production in Brazil by:" |
| 15 | +) |
| 16 | +OBSERVATION_SELECT_LABEL_DEFAULT = _("Select an observation table") |
| 17 | + |
| 18 | +# Fallback configs when no ObservationDimension rows exist (msgids match seed / DB). |
| 19 | +FALLBACK_DIMENSION_BY_INDEX = { |
| 20 | + "scientific_production": { |
| 21 | + "slug": "documents-by-affiliation-country", |
| 22 | + "menu_label": "Affiliation \u2013 Country", |
| 23 | + "row_field_name": "country", |
| 24 | + "col_field_name": "publication_year", |
| 25 | + "row_bucket_size": 500, |
| 26 | + "col_bucket_size": 300, |
| 27 | + "table_title": ( |
| 28 | + "Evolution of scientific production - World - number of documents by " |
| 29 | + "Affiliation \u2013 Country" |
| 30 | + ), |
| 31 | + "kpi_label": "Documents", |
| 32 | + "row_label": "Country", |
| 33 | + "col_label": "Year", |
| 34 | + "value_label": "Documents", |
| 35 | + }, |
| 36 | + "social_production": { |
| 37 | + "slug": "documents-by-events", |
| 38 | + "menu_label": "Events", |
| 39 | + "row_field_name": "directory_type", |
| 40 | + "col_field_name": "publication_year", |
| 41 | + "row_bucket_size": 500, |
| 42 | + "col_bucket_size": 300, |
| 43 | + "table_title": "Brazil - Social Production - Country - Documents - Events", |
| 44 | + "kpi_label": "Documents", |
| 45 | + "row_label": "Types", |
| 46 | + "col_label": "Year", |
| 47 | + "value_label": "Documents", |
| 48 | + "is_default": True, |
| 49 | + }, |
| 50 | +} |
| 51 | + |
| 52 | +# Slug → lazy msgids (extracted by makemessages). DB seed values must match these msgids. |
| 53 | +DIMENSION_I18N = { |
| 54 | + "documents-by-affiliation-region-world": { |
| 55 | + "menu_label": _("Affiliation - Region of the World"), |
| 56 | + "row_label": _("Region of the World"), |
| 57 | + "table_title": _( |
| 58 | + "Evolution of scientific production - World - number of documents by " |
| 59 | + "Affiliation - Region of the World" |
| 60 | + ), |
| 61 | + }, |
| 62 | + "documents-by-affiliation-country": { |
| 63 | + "menu_label": _("Affiliation \u2013 Country"), |
| 64 | + "row_label": _("Country"), |
| 65 | + "table_title": _( |
| 66 | + "Evolution of scientific production - World - number of documents by " |
| 67 | + "Affiliation \u2013 Country" |
| 68 | + ), |
| 69 | + }, |
| 70 | + "documents-by-institution": { |
| 71 | + "menu_label": _("Affiliation \u2013 Institution"), |
| 72 | + "row_label": _("Institution"), |
| 73 | + "table_title": _( |
| 74 | + "Evolution of scientific production - World - number of documents by " |
| 75 | + "Affiliation \u2013 Institution" |
| 76 | + ), |
| 77 | + }, |
| 78 | + "documents-by-publisher": { |
| 79 | + "menu_label": _("Publisher"), |
| 80 | + "row_label": _("Publisher"), |
| 81 | + "table_title": _( |
| 82 | + "Evolution of scientific production - World - number of documents by Publisher" |
| 83 | + ), |
| 84 | + }, |
| 85 | + "documents-by-journal": { |
| 86 | + "menu_label": _("Journal"), |
| 87 | + "row_label": _("Journal"), |
| 88 | + "table_title": _( |
| 89 | + "Evolution of scientific production - World - number of documents by Journal" |
| 90 | + ), |
| 91 | + }, |
| 92 | + "documents-by-thematic-area": { |
| 93 | + "menu_label": _("Thematic \u00e1rea"), |
| 94 | + "row_label": _("Thematic \u00e1rea"), |
| 95 | + "table_title": _( |
| 96 | + "Evolution of scientific production - World - number of documents by " |
| 97 | + "Thematic \u00e1rea" |
| 98 | + ), |
| 99 | + }, |
| 100 | + "documents-by-domain": { |
| 101 | + "menu_label": _("Domain"), |
| 102 | + "row_label": _("Domain"), |
| 103 | + "table_title": _( |
| 104 | + "Evolution of scientific production - World - number of documents by Domain" |
| 105 | + ), |
| 106 | + }, |
| 107 | + "documents-by-subfield": { |
| 108 | + "menu_label": _("Subfield"), |
| 109 | + "row_label": _("Subfield"), |
| 110 | + "table_title": _( |
| 111 | + "Evolution of scientific production - World - number of documents by Subfield" |
| 112 | + ), |
| 113 | + }, |
| 114 | + "documents-by-document-type": { |
| 115 | + "menu_label": _("Document Type"), |
| 116 | + "row_label": _("Document Type"), |
| 117 | + "table_title": _( |
| 118 | + "Evolution of scientific production - World - number of documents by Document Type" |
| 119 | + ), |
| 120 | + }, |
| 121 | + "documents-by-document-language": { |
| 122 | + "menu_label": _("Document Language"), |
| 123 | + "row_label": _("Document Language"), |
| 124 | + "table_title": _( |
| 125 | + "Evolution of scientific production - World - number of documents by " |
| 126 | + "Document Language" |
| 127 | + ), |
| 128 | + }, |
| 129 | + "documents-by-open-access": { |
| 130 | + "menu_label": _("Open Access"), |
| 131 | + "row_label": _("Open Access"), |
| 132 | + "table_title": _( |
| 133 | + "Evolution of scientific production - World - number of documents by Open Access" |
| 134 | + ), |
| 135 | + }, |
| 136 | + "documents-by-access-type": { |
| 137 | + "menu_label": _("Access Type"), |
| 138 | + "row_label": _("Access Type"), |
| 139 | + "table_title": _( |
| 140 | + "Evolution of scientific production - World - number of documents by Access Type" |
| 141 | + ), |
| 142 | + }, |
| 143 | + "documents-by-source-type": { |
| 144 | + "menu_label": _("Source Type"), |
| 145 | + "row_label": _("Source Type"), |
| 146 | + "table_title": _( |
| 147 | + "Evolution of scientific production - World - number of documents by Source Type" |
| 148 | + ), |
| 149 | + }, |
| 150 | + "documents-by-source-country": { |
| 151 | + "menu_label": _("Source Country"), |
| 152 | + "row_label": _("Source Country"), |
| 153 | + "table_title": _( |
| 154 | + "Evolution of scientific production - World - number of documents by " |
| 155 | + "Source Country" |
| 156 | + ), |
| 157 | + }, |
| 158 | + "documents-by-funder": { |
| 159 | + "menu_label": _("Funder"), |
| 160 | + "row_label": _("Funder"), |
| 161 | + "table_title": _( |
| 162 | + "Evolution of scientific production - World - number of documents by Funder" |
| 163 | + ), |
| 164 | + }, |
| 165 | + "documents-by-events": { |
| 166 | + "menu_label": _("Events"), |
| 167 | + "row_label": _("Types"), |
| 168 | + "table_title": _("Brazil - Social Production - Country - Documents - Events"), |
| 169 | + }, |
| 170 | + "documents-by-institutions": { |
| 171 | + "menu_label": _("Institutions"), |
| 172 | + "row_label": _("Institutions"), |
| 173 | + "table_title": _( |
| 174 | + "Brazil - Social Production - Country - Documents - Institutions" |
| 175 | + ), |
| 176 | + }, |
| 177 | + "documents-by-states": { |
| 178 | + "menu_label": _("States"), |
| 179 | + "row_label": _("States"), |
| 180 | + "table_title": _("Brazil - Social Production - Country - Documents - States"), |
| 181 | + }, |
| 182 | + "documents-by-regions": { |
| 183 | + "menu_label": _("Regions"), |
| 184 | + "row_label": _("Regions"), |
| 185 | + "table_title": _("Brazil - Social Production - Country - Documents - Regions"), |
| 186 | + }, |
| 187 | + "documents-by-cities": { |
| 188 | + "menu_label": _("Cities"), |
| 189 | + "row_label": _("Cities"), |
| 190 | + "table_title": _("Brazil - Social Production - Country - Documents - Cities"), |
| 191 | + }, |
| 192 | + "documents-by-action": { |
| 193 | + "menu_label": _("Action"), |
| 194 | + "row_label": _("Action"), |
| 195 | + "table_title": _("Brazil - Social Production - Country - Documents - Action"), |
| 196 | + }, |
| 197 | + "documents-by-practice": { |
| 198 | + "menu_label": _("Practice"), |
| 199 | + "row_label": _("Practice"), |
| 200 | + "table_title": _("Brazil - Social Production - Country - Documents - Practice"), |
| 201 | + }, |
| 202 | + "documents-by-classification": { |
| 203 | + "menu_label": _("Classification"), |
| 204 | + "row_label": _("Classification"), |
| 205 | + "table_title": _( |
| 206 | + "Brazil - Social Production - Country - Documents - Classification" |
| 207 | + ), |
| 208 | + }, |
| 209 | + "documents-by-division": { |
| 210 | + "menu_label": _("Division"), |
| 211 | + "row_label": _("Division"), |
| 212 | + "table_title": _("Brazil - Social Production - Country - Documents - Division"), |
| 213 | + }, |
| 214 | + "documents-by-area": { |
| 215 | + "menu_label": _("Area"), |
| 216 | + "row_label": _("Area"), |
| 217 | + "table_title": _("Brazil - Social Production - Country - Documents - Area"), |
| 218 | + }, |
| 219 | + "documents-by-subarea": { |
| 220 | + "menu_label": _("Subarea"), |
| 221 | + "row_label": _("Subarea"), |
| 222 | + "table_title": _("Brazil - Social Production - Country - Documents - Subarea"), |
| 223 | + }, |
| 224 | +} |
| 225 | + |
| 226 | +_LABEL_FIELDS = ( |
| 227 | + "menu_label", |
| 228 | + "table_title", |
| 229 | + "row_label", |
| 230 | + "kpi_label", |
| 231 | + "col_label", |
| 232 | + "value_label", |
| 233 | +) |
| 234 | + |
| 235 | + |
| 236 | +def observation_dimension_select_label(index_name): |
| 237 | + if index_name == "scientific_production": |
| 238 | + return gettext(OBSERVATION_SELECT_LABEL_SCIENTIFIC) |
| 239 | + if index_name == "social_production": |
| 240 | + return gettext(OBSERVATION_SELECT_LABEL_SOCIAL) |
| 241 | + return gettext(OBSERVATION_SELECT_LABEL_DEFAULT) |
| 242 | + |
| 243 | + |
| 244 | +def fallback_dimension_for_index(index_name): |
| 245 | + spec = FALLBACK_DIMENSION_BY_INDEX.get(index_name) |
| 246 | + if spec is None: |
| 247 | + spec = FALLBACK_DIMENSION_BY_INDEX["scientific_production"] |
| 248 | + return localize_dimension(dict(spec)) |
| 249 | + |
| 250 | + |
| 251 | +def localize_dimension(dimension): |
| 252 | + """Apply gettext to display fields for the active request locale.""" |
| 253 | + if not dimension: |
| 254 | + return dimension |
| 255 | + |
| 256 | + localized = dict(dimension) |
| 257 | + slug = localized.get("slug") or "" |
| 258 | + spec = DIMENSION_I18N.get(slug) |
| 259 | + |
| 260 | + if spec: |
| 261 | + for field in _LABEL_FIELDS: |
| 262 | + if field in spec: |
| 263 | + localized[field] = gettext(spec[field]) |
| 264 | + localized.setdefault("kpi_label", gettext(_COMMON_KPI)) |
| 265 | + localized.setdefault("col_label", gettext(_COMMON_COL)) |
| 266 | + localized.setdefault("value_label", gettext(_COMMON_VALUE)) |
| 267 | + return localized |
| 268 | + |
| 269 | + for field in _LABEL_FIELDS: |
| 270 | + value = localized.get(field) |
| 271 | + if isinstance(value, str) and value.strip(): |
| 272 | + localized[field] = gettext(value) |
| 273 | + return localized |
0 commit comments