Skip to content

Commit

Permalink
Add I18N for labels #72
Browse files Browse the repository at this point in the history
  • Loading branch information
4lm committed Aug 15, 2019
1 parent 22a9faf commit 48d43e0
Show file tree
Hide file tree
Showing 7 changed files with 266 additions and 255 deletions.
31 changes: 22 additions & 9 deletions app_settings.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
import os
from django.utils import translation
from configobj import ConfigObj

from wam import settings

# TODO: Explain vars!

# tool language
# available languages: German ('de') and English ('en')
LANGUAGE = 'de'
# Set default language for app
DEFAULT_LANGUAGE = 'de'

# Store which defines all available languages for an app
LANGUAGE_STORE = ['de', 'en']

# Get the language of current app thread or fallback to default language.
def get_language_or_fallback():
current_thread_language = translation.get_language()
if current_thread_language in LANGUAGE_STORE:
return current_thread_language
else:
return DEFAULT_LANGUAGE

# import configs
LAYER_AREAS_METADATA = ConfigObj(os.path.join(settings.BASE_DIR,
Expand Down Expand Up @@ -40,16 +51,18 @@
'config',
'esys_areas.cfg'))

LABELS = ConfigObj(os.path.join(settings.BASE_DIR,
'stemp_abw',
'locale',
LANGUAGE,
'labels.cfg'))
def labels():
language = get_language_or_fallback()
return ConfigObj(os.path.join(settings.BASE_DIR,
'stemp_abw',
'locale',
language,
'labels.cfg'))

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

TEXT_FILES = {name: {'file': os.path.join(TEXT_FILES_DIR, f'{name}.md'),
Expand Down
48 changes: 24 additions & 24 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 All @@ -23,7 +23,7 @@ def _config2layer(cat_name, layer_cfg_metadata):
for ls in layer_cfg_metadata.values()
for l, d in ls.items()}
for l, v in layer_list.items():
layer_list[l]['title'] = LABELS['layers'][l]['title']
layer_list[l]['title'] = labels()['layers'][l]['title']

# create JSON for layer styles
layer_style = {l: a['style']
Expand All @@ -40,11 +40,11 @@ def _config2layer(cat_name, layer_cfg_metadata):
layer_metadata = OrderedDict()
for (grp, layers) in layer_cfg_metadata.items():
layer_metadata.update({grp: {'layers': layers}})
layer_metadata[grp]['title'] = LABELS['layer_groups'][grp]['title']
layer_metadata[grp]['text'] = LABELS['layer_groups'][grp]['text']
layer_metadata[grp]['title'] = labels()['layer_groups'][grp]['title']
layer_metadata[grp]['text'] = labels()['layer_groups'][grp]['text']
for l, v in layers.items():
layer_metadata[grp]['layers'][l]['title'] = LABELS['layers'][l]['title']
layer_metadata[grp]['layers'][l]['text'] = LABELS['layers'][l]['text']
layer_metadata[grp]['layers'][l]['title'] = labels()['layers'][l]['title']
layer_metadata[grp]['layers'][l]['text'] = labels()['layers'][l]['text']

# create layer groups for layer menu using layers config
layer_groups = layer_metadata.copy()
Expand Down Expand Up @@ -91,54 +91,54 @@ def prepare_component_data():
area_metadata = OrderedDict()
for (grp, comps) in ESYS_COMPONENTS_METADATA.items():
comp_metadata.update({grp: {'comps': comps}})
comp_metadata[grp]['title'] = LABELS['component_groups'][grp]['title']
comp_metadata[grp]['text'] = LABELS['component_groups'][grp]['text']
comp_metadata[grp]['title'] = labels()['component_groups'][grp]['title']
comp_metadata[grp]['text'] = labels()['component_groups'][grp]['text']
for l, v in comps.items():
comp_metadata[grp]['comps'][l]['title'] = LABELS['components'][l]['title']
comp_metadata[grp]['comps'][l]['text'] = LABELS['components'][l]['text']
comp_metadata[grp]['comps'][l]['title'] = labels()['components'][l]['title']
comp_metadata[grp]['comps'][l]['text'] = labels()['components'][l]['text']

# additional text
if LABELS['components'][l].get('text2') is not None:
if labels()['components'][l].get('text2') is not None:
comp_metadata[grp]['comps'][l]['text2'] =\
LABELS['components'][l]['text2']
labels()['components'][l]['text2']

# reveal window
reveal_id = LABELS['components'][l].get('reveal_id')
reveal_icon = LABELS['components'][l].get('reveal_icon')
reveal_id = labels()['components'][l].get('reveal_id')
reveal_icon = labels()['components'][l].get('reveal_icon')
if (reveal_id is not None) and (reveal_icon is not None):
comp_metadata[grp]['comps'][l]['popup'] =\
create_panel_reveal_info_button(reveal_id, reveal_icon)
else:
comp_metadata[grp]['comps'][l]['popup'] = None

# icon
if LABELS['components'][l].get('icon') is not None:
if labels()['components'][l].get('icon') is not None:
comp_metadata[grp]['comps'][l]['icon'] =\
LABELS['components'][l]['icon']
labels()['components'][l]['icon']
else:
comp_metadata[grp]['comps'][l]['icon'] = None

for (grp, comps) in ESYS_AREAS_METADATA.items():
area_metadata.update({grp: {'comps': comps}})
area_metadata[grp]['title'] = LABELS['component_groups'][grp]['title']
area_metadata[grp]['text'] = LABELS['component_groups'][grp]['text']
area_metadata[grp]['title'] = labels()['component_groups'][grp]['title']
area_metadata[grp]['text'] = labels()['component_groups'][grp]['text']
for l, v in comps.items():
area_metadata[grp]['comps'][l]['title'] = LABELS['components'][l]['title']
area_metadata[grp]['comps'][l]['text'] = LABELS['components'][l]['text']
area_metadata[grp]['comps'][l]['title'] = labels()['components'][l]['title']
area_metadata[grp]['comps'][l]['text'] = labels()['components'][l]['text']

# reveal window
reveal_id = LABELS['components'][l].get('reveal_id')
reveal_icon = LABELS['components'][l].get('reveal_icon')
reveal_id = labels()['components'][l].get('reveal_id')
reveal_icon = labels()['components'][l].get('reveal_icon')
if (reveal_id is not None) and (reveal_icon is not None):
area_metadata[grp]['comps'][l]['popup'] =\
create_panel_reveal_info_button(reveal_id, reveal_icon)
else:
area_metadata[grp]['comps'][l]['popup'] = None

# icon
if LABELS['components'][l].get('icon') is not None:
if labels()['components'][l].get('icon') is not None:
area_metadata[grp]['comps'][l]['icon'] =\
LABELS['components'][l]['icon']
labels()['components'][l]['icon']
else:
area_metadata[grp]['comps'][l]['icon'] = None

Expand Down
8 changes: 4 additions & 4 deletions config/prepare_texts.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
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():
return {'panels': LABELS['panels'],
'tooltips': LABELS['tooltips'],
'charts': LABELS['charts']}
return {'panels': labels()['panels'],
'tooltips': labels()['tooltips'],
'charts': labels()['charts']}


def create_reveal_info_button():
Expand Down
Loading

0 comments on commit 48d43e0

Please sign in to comment.