tôi cố gắng để cho phép thẻ trans
và tôi đã thực hiện một thử nghiệm mẫu i18n.html:Làm cách nào để bật thẻ {% trans%} cho các mẫu jinja?
{% trans %}For sale{% endtrans %}--{{message}}--{{decimal_format}}
Dưới đây là mã python tôi theo manpages:
from webapp2_extras import i18n as multilingua
import jinja2
from webapp2_extras.i18n import lazy_gettext as gettext
from webapp2_extras.i18n import lazy_gettext as _
from jinja2 import Environment, FileSystemLoader
jinja_environment = jinja2.Environment(loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
extensions=['jinja2.ext.i18n'])
# The code below seems wrong since it is django but it was the only way I could make the page load
jinja_environment.install_gettext_translations(django.utils.translation)
class HelloWorldHandler(webapp2.RequestHandler):
def get(self):
# Set the requested locale.
locale = self.request.GET.get('locale', 'pt')
multilingua.get_i18n().set_locale(locale)
message = multilingua.gettext('For sale')
#django.utils.translation.activate('pt')
template = jinja_environment.get_template('templates/i18n.html')
decimal_format = multilingua.I18n(self.request).format_decimal(1000)
self.response.out.write(template.render(message=message, decimal_format=decimal_format))
tôi không thể làm cho nó hoạt mà không có django và do đó tôi hỏi làm thế nào để mất bản dịch django và ở lại với webapp2.i18n + jinja thay thế.
Cũng có một cuộc thảo luận trong số a thread nơi tôi không phải là người duy nhất nói rằng tài liệu đó có phần không đầy đủ hoặc khó tìm. Bạn có thể vui lòng trả lời hoặc nhận xét đó là cách được khuyến nghị để làm thẻ chuyển đổi hoạt động và lý do tôi phải bao gồm jinja_environment.install_gettext_translations(django.utils.translation)
?
Khi tôi cố gắng loại bỏ việc sử dụng django của tôi, tôi cũng mất các chức năng của webapp2.i18n. Các tệp miền địa phương của tôi là cả ngôn ngữ/... và conf/locale .. vì đầu tiên là mặc định cho webapp2 và thứ hai là mặc định cho bản dịch django, vì vậy tôi thực sự có thể sử dụng một số nguyên tắc để thực hành tốt nhất ở đây để loại bỏ phụ thuộc django và sử dụng webapp2 và jinja để hiển thị bản địa hóa của tôi.
Nếu bất kỳ sự giúp đỡ, tôi đã nhận được một thông báo lỗi khi cố gắng để loại bỏ django:
self.response.out.write(template.render(message=message, decimal_format=decimal_format))
File "/media/Lexar/montao/montaoproject/jinja2/environment.py", line 894, in render
return self.environment.handle_exception(exc_info, True)
File "/media/Lexar/montao/montaoproject/templates/i18n.html", line 2, in top-level template code
{{ _('For sale') }}--{{message}}--{{decimal_format}}
UndefinedError: 'gettext' is undefined
Cảm ơn bạn
Giải pháp/giải pháp không có giấy tờ khiến thẻ jinja của tôi bị gián đoạn wor k: 'từ webapp2_extras nhập i18n jinja_environment.install_gettext_translations (i18n)' –
Sẽ rất thú vị khi biết cách bạn tìm ra giải pháp/giải pháp không có giấy tờ này. – eyquem
cho bất cứ ai đọc sách này, đây là hướng dẫn cập nhật về cách thực hiện điều này http://mikeshilkov.wordpress.com/2012/07/26/enable-jinja2-and-i18n-translations-on-google-appengine/ –