Skip to content

Commit

Permalink
Enable I18N for reveals, contexts and texts #72
Browse files Browse the repository at this point in the history
  • Loading branch information
4lm committed Aug 15, 2019
1 parent 48d43e0 commit d2b05a3
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 34 deletions.
19 changes: 13 additions & 6 deletions app_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def get_language_or_fallback():
'config',
'esys_areas.cfg'))


def labels():
language = get_language_or_fallback()
return ConfigObj(os.path.join(settings.BASE_DIR,
Expand All @@ -59,19 +60,25 @@ def labels():
language,
'labels.cfg'))

TEXT_FILES_DIR = os.path.join(settings.BASE_DIR,
'stemp_abw',
'locale',
DEFAULT_LANGUAGE,
'reveals')

TEXT_FILES = {name: {'file': os.path.join(TEXT_FILES_DIR, f'{name}.md'),
def text_files_dir():
language = get_language_or_fallback()
return os.path.join(settings.BASE_DIR,
'stemp_abw',
'locale',
language,
'reveals')


def text_files():
return {name: {'file': os.path.join(text_files_dir(), f'{name}.md'),
'icon': icon}
for name, icon in
{'welcome': 'ion-help-buoy',
'outlook': 'ion-navigate'}.items()
}


MAP_DATA_CACHE_TIMEOUT = 60 * 60

# Mapping between UI control id and data in scenario data dict.
Expand Down
16 changes: 7 additions & 9 deletions config/prepare_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
ScenarioDropdownForm
from stemp_abw.app_settings import LAYER_AREAS_METADATA, LAYER_REGION_METADATA,\
LAYER_RESULT_METADATA, LAYER_DEFAULT_STYLES, ESYS_COMPONENTS_METADATA,\
ESYS_AREAS_METADATA, labels, TEXT_FILES_DIR
ESYS_AREAS_METADATA, labels, text_files_dir


def prepare_layer_data():
Expand Down Expand Up @@ -83,8 +83,8 @@ def _config2layer(cat_name, layer_cfg_metadata):
return layer_data


def prepare_component_data():
component_data = {}
def component_data():
component_data_store = {}

# update component/area and group labels using labels config
comp_metadata = OrderedDict()
Expand Down Expand Up @@ -146,15 +146,15 @@ def prepare_component_data():
comp_groups = comp_metadata.copy()
for grp, comps in comp_groups.items():
comp_groups[grp]['comps'] = ComponentGroupForm(components=comps['comps'])
component_data['comp_groups'] = comp_groups
component_data_store['comp_groups'] = comp_groups

# create area groups for areas panel (variable areas) using components config
area_groups = area_metadata.copy()
for grp, comps in area_groups.items():
area_groups[grp]['comps'] = AreaGroupForm(components=comps['comps'])
component_data['area_groups'] = area_groups
component_data_store['area_groups'] = area_groups

return component_data
return component_data_store


def prepare_scenario_data():
Expand All @@ -166,7 +166,7 @@ def create_panel_reveal_info_button(reveal_id, reveal_icon):
"""Creates reveal window with trigger button with content from markdown file
(panel info button, e.g. in wind slider)
"""
f = open(os.path.join(TEXT_FILES_DIR, f'{reveal_id}.md'), 'r', encoding='utf-8')
f = open(os.path.join(text_files_dir(), f'{reveal_id}.md'), 'r', encoding='utf-8')
popup = InfoButton(text=f.read(),
tooltip='Bitte klicken!',
is_markdown=True,
Expand All @@ -177,6 +177,4 @@ def create_panel_reveal_info_button(reveal_id, reveal_icon):
f.close()
return popup


COMPONENT_DATA = prepare_component_data()
SCENARIO_DATA = prepare_scenario_data()
18 changes: 7 additions & 11 deletions config/prepare_texts.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,25 @@
from utils.widgets import InfoButton
from stemp_abw.app_settings import labels, TEXT_FILES
from stemp_abw.app_settings import labels, text_files

def prepare_label_data():
def label_data():
return {'panels': labels()['panels'],
'tooltips': labels()['tooltips'],
'charts': labels()['charts']}


def create_reveal_info_button():
def text_data():
"""Create reveal window with trigger button with content from markdown file
(general app info buttons, e.g. in top navigation bar)
"""
text_data = {}
for name, data in TEXT_FILES.items():
text_data_store = {}
for name, data in text_files().items():
f = open(data['file'], 'r', encoding='utf-8')
text = f.read()
text_data[name] = InfoButton(text=text,
text_data_store[name] = InfoButton(text=text,
tooltip=text.split("\n")[0][2:],
is_markdown=True,
ionicon_type=data['icon'],
ionicon_size='medium',
info_id=name)
f.close()
return {'texts': text_data}


LABEL_DATA = prepare_label_data()
TEXT_DATA = create_reveal_info_button()
return {'texts': text_data_store}
2 changes: 1 addition & 1 deletion locale/en/reveals/welcome.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Willkommen!
# EN: Willkommen!

<div style="background-color: #F2994A; color: #fff; width: 100%; padding: 0.05rem 0.75rem 0.05rem 0.75rem; vertical-align: middle; margin-bottom: 0.75rem;">
<h3>Neue Funktionen!</h3>
Expand Down
4 changes: 2 additions & 2 deletions results/results.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from stemp_abw.models import Scenario, RegMun, MunData
from stemp_abw.results.io import oemof_json_to_results
from stemp_abw.app_settings import NODE_LABELS, SIMULATION_CFG as SIM_CFG
from stemp_abw.config.prepare_texts import LABEL_DATA
from stemp_abw.config.prepare_texts import label_data

from oemof.outputlib import views

Expand Down Expand Up @@ -496,7 +496,7 @@ def visualize(self, **kwargs):
# load tooltip text from labels using container id
container_id = kwargs.get('renderTo', None)
if container_id is not None:
tooltip_section = LABEL_DATA['charts'].get(container_id, None)
tooltip_section = label_data()['charts'].get(container_id, None)
if tooltip_section is not None:
tooltip_text = tooltip_section.get('text', '')
else:
Expand Down
10 changes: 5 additions & 5 deletions views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
from django.shortcuts import HttpResponse, render
import json

from stemp_abw.config.prepare_context import COMPONENT_DATA, SCENARIO_DATA,\
from stemp_abw.config.prepare_context import component_data, SCENARIO_DATA,\
prepare_layer_data
from stemp_abw.config.prepare_texts import LABEL_DATA, TEXT_DATA
from stemp_abw.config.prepare_texts import label_data, text_data
from stemp_abw.config.leaflet import LEAFLET_CONFIG
from stemp_abw.models import Scenario
from stemp_abw.views.detail_views import *
Expand Down Expand Up @@ -86,10 +86,10 @@ def get_context_data(self, **kwargs):
if data['cat'] == 'results'}
context.update(layer_data)

context.update(COMPONENT_DATA)
context.update(component_data())
context.update(SCENARIO_DATA)
context.update(LABEL_DATA)
context.update(TEXT_DATA)
context.update(label_data())
context.update(text_data())
context['re_pot_layer_id_list'] = RE_POT_LAYER_ID_LIST

context['results_charts_tab1_viz'] = results_charts_tab1_viz
Expand Down

0 comments on commit d2b05a3

Please sign in to comment.