|
@@ -8,8 +8,9 @@ from functools import lru_cache
|
|
|
|
|
|
LANGUAGES = 'languages'
|
|
LANGUAGES = 'languages'
|
|
|
|
|
|
|
|
+#TODO: load from config file
|
|
DEFAULT_LANG = 'en'
|
|
DEFAULT_LANG = 'en'
|
|
-REGISTERED_LANG = set()
|
|
|
|
|
|
+OTHER_LANGS = set(['es'])
|
|
|
|
|
|
|
|
|
|
@lru_cache(None)
|
|
@lru_cache(None)
|
|
@@ -36,6 +37,16 @@ def save_dic(lang, dic):
|
|
f.write(f"{key}:{value}\n")
|
|
f.write(f"{key}:{value}\n")
|
|
|
|
|
|
|
|
|
|
|
|
+def add_lang_prefix(lang, path):
|
|
|
|
+ if lang == DEFAULT_LANG:
|
|
|
|
+ return path
|
|
|
|
+
|
|
|
|
+ if lang not in OTHER_LANGS:
|
|
|
|
+ print(f"Not registered language: `{lang}`")
|
|
|
|
+
|
|
|
|
+ return f"/{lang}{path}"
|
|
|
|
+
|
|
|
|
+
|
|
@contextfilter
|
|
@contextfilter
|
|
def lang(ctx, value):
|
|
def lang(ctx, value):
|
|
lang = ctx.environment.globals['lang']
|
|
lang = ctx.environment.globals['lang']
|
|
@@ -56,7 +67,7 @@ def lang(ctx, value):
|
|
|
|
|
|
|
|
|
|
@contextfilter
|
|
@contextfilter
|
|
-def lang_url(ctx, value):
|
|
|
|
|
|
+def lang_url(ctx, lang):
|
|
name, _ = splitext(ctx.name)
|
|
name, _ = splitext(ctx.name)
|
|
if name.startswith('./'):
|
|
if name.startswith('./'):
|
|
name = name[2:]
|
|
name = name[2:]
|
|
@@ -64,11 +75,13 @@ def lang_url(ctx, value):
|
|
if name == 'index':
|
|
if name == 'index':
|
|
name = ''
|
|
name = ''
|
|
|
|
|
|
- if value == DEFAULT_LANG:
|
|
|
|
- return "/" + name
|
|
|
|
- else:
|
|
|
|
- REGISTERED_LANG.add(value)
|
|
|
|
- return "/" + value + '/' + name
|
|
|
|
|
|
+ return add_lang_prefix(lang, f"/{name}")
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+@contextfilter
|
|
|
|
+def cur_lang(ctx, path):
|
|
|
|
+ lang = ctx.environment.globals['lang']
|
|
|
|
+ return add_lang_prefix(lang, path)
|
|
|
|
|
|
|
|
|
|
def static(value):
|
|
def static(value):
|
|
@@ -119,6 +132,7 @@ def run(args):
|
|
# Add filters
|
|
# Add filters
|
|
env.filters['lang'] = lang
|
|
env.filters['lang'] = lang
|
|
env.filters['lang_url'] = lang_url
|
|
env.filters['lang_url'] = lang_url
|
|
|
|
+ env.filters['cur_lang'] = cur_lang
|
|
env.filters['static'] = static
|
|
env.filters['static'] = static
|
|
|
|
|
|
path = Path(args.source)
|
|
path = Path(args.source)
|
|
@@ -135,7 +149,7 @@ def run(args):
|
|
|
|
|
|
copytree('static', join(args.target, 'static'))
|
|
copytree('static', join(args.target, 'static'))
|
|
|
|
|
|
- for other_lang in REGISTERED_LANG:
|
|
|
|
|
|
+ for other_lang in OTHER_LANGS:
|
|
env.globals['lang'] = other_lang
|
|
env.globals['lang'] = other_lang
|
|
compile(env, path, args.target)
|
|
compile(env, path, args.target)
|
|
|
|
|