Ver código fonte

Translate url for current lang

Danilo Gómez Gómez 5 anos atrás
pai
commit
4c1228758a
2 arquivos alterados com 32 adições e 18 exclusões
  1. 10 10
      layout/_base.html
  2. 22 8
      sitegen.py

+ 10 - 10
layout/_base.html

@@ -33,20 +33,20 @@
 					<div class="vbox">
 						<div class="hbox">
 							<h1 class="header-title">
-								<div><a href="/work" {% if work_selected %}class="selected"{% endif %}>{{ 'work' | lang }}</a></div> /
-								<div><a href="/news" {% if news_selected %}class="selected"{% endif %}>{{ 'news' | lang }}</a></div> /
-								<div><a href="/about"{% if about_selected %}class="selected"{% endif %}>{{ 'about' | lang }}</a></div> /
-								<div><a href="/contact" {% if contact_selected %}class="selected"{% endif %}>{{ 'contact' | lang }}</a></div> /
+								<div><a href="{{ '/work' | cur_lang }}" {% if work_selected %}class="selected"{% endif %}>{{ 'work' | lang }}</a></div> /
+								<div><a href="{{ '/news' | cur_lang }}" {% if news_selected %}class="selected"{% endif %}>{{ 'news' | lang }}</a></div> /
+								<div><a href="{{ '/about' | cur_lang }}" {% if about_selected %}class="selected"{% endif %}>{{ 'about' | lang }}</a></div> /
+								<div><a href="{{ '/contact' | cur_lang }}" {% if contact_selected %}class="selected"{% endif %}>{{ 'contact' | lang }}</a></div> /
 							</h1>
 						</div>
 						<div class="hbox">
 							<div class="header-subtitle">
-								<div><a href="/work/all" {% if all_selected %}class="selected"{% endif %}>{{ 'all' | lang }}</a></div> /
-								<div><a href="/work/poster" {% if poster_selected %}class="selected"{% endif %}>{{ 'poster' | lang }}</a></div> /
-								<div><a href="/work/illustration" {% if illustration_selected %}class="selected"{% endif %}>{{ 'illustration' | lang }}</a></div> /
-								<div><a href="/work/editorial" {% if editorial_selected %}class="selected"{% endif %}>{{ 'editorial' | lang }}</a></div> /
-								<div><a href="/work/branding" {% if branding_selected %}class="selected"{% endif %}>{{ 'branding' | lang }}</a></div> /
-								<div><a href="/work/other" {% if other_selected %}class="selected"{% endif %}>{{ 'other' | lang }}</a></div>
+								<div><a href="{{ '/work/all' | cur_lang }}" {% if all_selected %}class="selected"{% endif %}>{{ 'all' | lang }}</a></div> /
+								<div><a href="{{ '/work/poster' | cur_lang }}" {% if poster_selected %}class="selected"{% endif %}>{{ 'poster' | lang }}</a></div> /
+								<div><a href="{{ '/work/illustration' | cur_lang }}" {% if illustration_selected %}class="selected"{% endif %}>{{ 'illustration' | lang }}</a></div> /
+								<div><a href="{{ '/work/editorial' | cur_lang }}" {% if editorial_selected %}class="selected"{% endif %}>{{ 'editorial' | lang }}</a></div> /
+								<div><a href="{{ '/work/branding' | cur_lang }}" {% if branding_selected %}class="selected"{% endif %}>{{ 'branding' | lang }}</a></div> /
+								<div><a href="{{ '/work/other' | cur_lang }}" {% if other_selected %}class="selected"{% endif %}>{{ 'other' | lang }}</a></div>
 							</div>
 						</div>
 					</div>

+ 22 - 8
sitegen.py

@@ -8,8 +8,9 @@ from functools import lru_cache
 
 LANGUAGES = 'languages'
 
+#TODO: load from config file
 DEFAULT_LANG = 'en'
-REGISTERED_LANG = set()
+OTHER_LANGS = set(['es'])
 
 
 @lru_cache(None)
@@ -36,6 +37,16 @@ def save_dic(lang, dic):
             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
 def lang(ctx, value):
     lang = ctx.environment.globals['lang']
@@ -56,7 +67,7 @@ def lang(ctx, value):
 
 
 @contextfilter
-def lang_url(ctx, value):
+def lang_url(ctx, lang):
     name, _ = splitext(ctx.name)
     if name.startswith('./'):
         name = name[2:]
@@ -64,11 +75,13 @@ def lang_url(ctx, value):
     if name == 'index':
         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):
@@ -119,6 +132,7 @@ def run(args):
     # Add filters
     env.filters['lang'] = lang
     env.filters['lang_url'] = lang_url
+    env.filters['cur_lang'] = cur_lang
     env.filters['static'] = static
 
     path = Path(args.source)
@@ -135,7 +149,7 @@ def run(args):
 
     copytree('static', join(args.target, 'static'))
 
-    for other_lang in REGISTERED_LANG:
+    for other_lang in OTHER_LANGS:
         env.globals['lang'] = other_lang
         compile(env, path, args.target)