Skip to content

Commit

Permalink
Initial, working impl. of #128 (not hooked up)
Browse files Browse the repository at this point in the history
This is a technically-working implementation of #128, which creates
an object, exposed at runtime as lingua_franca.config, which descends
from dict. More robust get/set methods to follow.

The implementation is extensible to support full use of localization and
'full lang codes'.

Either that or hooking it up to the functions will be next.
  • Loading branch information
ChanceNCounter committed Apr 2, 2021
1 parent eee9570 commit f473b80
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 7 deletions.
17 changes: 13 additions & 4 deletions lingua_franca/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
### DO NOT CHANGE THIS IMPORT ORDER ###
from .internal import get_active_langs, get_supported_locs, \
get_full_lang_code

from .configuration import Config

### END OF IMPORT ORDER ###

from .internal import get_default_lang, set_default_lang, get_default_loc, \
get_active_langs, _set_active_langs, get_primary_lang_code, \
get_full_lang_code, resolve_resource_file, load_language, \
load_languages, unload_language, unload_languages, get_supported_langs
_set_active_langs, get_primary_lang_code, resolve_resource_file, \
load_language, load_languages, unload_language, unload_languages, \
get_supported_langs


from lingua_franca import config
config = Config()
1 change: 0 additions & 1 deletion lingua_franca/config.py

This file was deleted.

50 changes: 50 additions & 0 deletions lingua_franca/configuration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import json
from os import path

from lingua_franca import get_active_langs, get_supported_locs, \
get_full_lang_code
from lingua_franca.internal import UnsupportedLanguageError, resolve_resource_file

default_global_values = \
{
'load_langs_on_demand': False
}

class LangConfig(dict):
def __init__(self, lang_code):
if lang_code not in get_supported_locs():
# DO NOT catch UnsupportedLanguageError!
# If this fails, we want to crash. This can *only* result from
# someone trying to override sanity checks upstairs. There are no
# circumstances under which this should fail and allow the program
# to continue.
lang_code = get_full_lang_code(lang_code)


resource_file = resolve_resource_file(f'text/{lang_code}/config.json')
with open(resource_file, 'r', encoding='utf-8') as i_file:
default_values = json.load(i_file)
for k in default_values:
self[k] = default_values[k]

class Config(dict):
def __init__(self):
self['global'] = dict(default_global_values)
for lang in get_active_langs():
'''
TODO proper full loc support here will handle languages similarly to global:
self['en']['universal'] for 'default' English config
(all dialects if not overridden)
self['en']['en-us'] for overrides specific to en-US
self['en']['en-au'] for overrides specific to en-AU
and so forth.
'''
if all((lang not in self.keys(), lang not in get_supported_locs())):
self[lang] = {}
self[lang]['universal'] = LangConfig(lang)
# begin portion that will need to adapt for the todo above
full_loc = lang if lang in get_supported_locs() else \
get_full_lang_code(lang)
self[lang][full_loc] = LangConfig(lang)
11 changes: 9 additions & 2 deletions lingua_franca/internal.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
from sys import version
from warnings import warn

from lingua_franca import config

_SUPPORTED_LANGUAGES = ("ca", "cs", "da", "de", "en", "es", "fr", "hu",
"it", "nl", "pl", "pt", "sl", "sv")

Expand Down Expand Up @@ -88,6 +86,13 @@ def get_supported_langs():
"""
return _SUPPORTED_LANGUAGES

def get_supported_locs():
"""
Returns:
list(str)
"""
return _SUPPORTED_FULL_LOCALIZATIONS


def get_active_langs():
""" Get the list of currently-loaded language codes
Expand Down Expand Up @@ -448,6 +453,8 @@ def is_error_type(_type):
def localized_function_decorator(func):
# Wrapper's logic
def _call_localized_function(func, *args, **kwargs):
from lingua_franca import config

lang_code = None
load_langs_on_demand = config.load_langs_on_demand
unload_language_afterward = False
Expand Down
3 changes: 3 additions & 0 deletions lingua_franca/res/text/en-us/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"short_scale": true
}

0 comments on commit f473b80

Please sign in to comment.