Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Internationalization #22

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
87 changes: 83 additions & 4 deletions prosopopee/prosopopee.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import sys
import yaml
import shutil
import collections

from jinja2 import Environment, FileSystemLoader

Expand All @@ -20,6 +21,78 @@
}
}

class ListTranslateViewIterator(object):
def __init__(self, lang, l):
self.lang = lang
self.it = iter(l)

def next(self):
return _(self.lang, self.it.next())

def __iter__(self):
return self

class ListTranslateView(collections.Sequence):
def __init__(self, lang, l):
self.lang=lang
self.proxified_list = l

def __len__(self):
return len(self.proxified_list)

def __getitem__(self, i):
return _(self.lang, self.proxified_list[i])

def __iter__(self):
return ListTranslateViewIterator(self.lang, self.proxified_list)

def __str__(self):
# TODO: fails with unicode input, but we don't use it...
return "[%s]" % (", ".join(["%s" % x for x in self]))

class DictTranslateViewIterator(object):
def __init__(self, lang, d):
self.lang = lang
self.it = iter(d)

def next(self):
return _(self.lang, self.it.next())

def __iter__(self):
return self

class DictTranslateView(collections.Mapping):
def __init__(self, lang, d):
self.lang=lang
self.proxified_dict = d

def __len__(self):
return len(self.proxified_dict)

def __getitem__(self, key):
if isinstance(self.proxified_dict[key], dict) and self.proxified_dict[key].has_key(self.lang):
return self.proxified_dict[key][self.lang]
else:
return _(self.lang, self.proxified_dict[key])

def __iter__(self):
return DictTranslateViewIterator(self.lang, self.proxified_dict)

def __str__(self):
# TODO: fails with unicode input, but we don't use it...
return "{%s}" % (", ".join(["'%s': '%s'" % (k,v) for (k,v) in self.iteritems()]))

def _(lang, o):
# TODO: should support iterators too
# TODO: testing for isinstance(o, collections.Mapping) creates an infinite recursion loop
t = type(o)
if isinstance(o, dict):
return DictTranslateView(lang, o)
elif isinstance(o, list):
return ListTranslateView(lang, o)
else:
return o

class Image(object):
base_dir = ""
target_dir = ""
Expand Down Expand Up @@ -101,6 +174,9 @@ def main():
if settings.get("settings", {}).get("gm"):
SETTINGS["gm"].update(settings["settings"]["gm"])

langs = settings.get("multilingual", [None])
main_lang = langs[0]

front_page_galleries_cover = []

dirs = filter(lambda x: x not in (".", "..") and os.path.isdir(x) and os.path.exists(os.path.join(os.getcwd(), x, "settings.yaml")), os.listdir(os.getcwd()))
Expand Down Expand Up @@ -157,16 +233,19 @@ def main():
Image.target_dir = os.path.join(os.getcwd(), "build", gallery)

template_to_render = page_template if gallery_settings.get("static") else gallery_index_template
open(os.path.join("build", gallery, "index.html"), "w").write(template_to_render.render(settings=settings, gallery=gallery_settings, Image=Image).encode("Utf-8"))
for lang in langs:
filename = "index.%s.html" % lang if lang and lang != main_lang else "index.html"
open(os.path.join("build", gallery, filename), "w").write(template_to_render.render(settings=_(lang,settings), gallery=_(lang,gallery_settings), Image=Image).encode("Utf-8"))

front_page_galleries_cover = reversed(sorted(front_page_galleries_cover, key=lambda x: x["date"]))
front_page_galleries_cover = list(reversed(sorted(front_page_galleries_cover, key=lambda x: x["date"])))

# this should probably be a factory
Image.base_dir = os.getcwd()
Image.target_dir = os.path.join(os.getcwd(), "build")

open(os.path.join("build", "index.html"), "w").write(index_template.render(settings=settings, galleries=front_page_galleries_cover, Image=Image).encode("Utf-8"))

for lang in langs:
filename = "index.%s.html" % lang if lang and lang != main_lang else "index.html"
open(os.path.join("build", filename), "w").write(index_template.render(settings=_(lang,settings), galleries=_(lang,front_page_galleries_cover), Image=Image).encode("Utf-8"))

if __name__ == '__main__':
main()
25 changes: 25 additions & 0 deletions prosopopee/templates/locales.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{% if settings.multilingual -%}
<script type="text/javascript" charset="utf-8">
function contains(a, obj) {
for (var i = 0; i < a.length; i++) {
if (a[i] === obj) {
return true;
}
}
return false;
}

if (window.navigator && window.navigator.language) {
var locale = window.navigator.language.substr(0, 2);
var availableLocales = [ "{{ settings.multilingual | join('", "') }}" ];
var pagename = location.href.split("/").slice(-1);
var targetpagename = "index.html";
if (locale != availableLocales[0] && contains(availableLocales, locale)) {
targetpagename = "index." + locale + ".html";
}
if (pagename != targetpagename) {
document.location.href = targetpagename;
}
}
</script>
{% endif -%}
1 change: 1 addition & 0 deletions prosopopee/themes/exposure/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<!--Let browser know website is optimized for mobile-->
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>{{ settings.title }}</title>
{% include "locales.html" %}
</head>

<body>
Expand Down
1 change: 1 addition & 0 deletions prosopopee/themes/exposure/templates/gallery-index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<!--Let browser know website is optimized for mobile-->
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>{{ gallery.title }} · {{ settings.title }}</title>
{% include "locales.html" %}
</head>

<body>
Expand Down