README_I18N 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. Generate gettext infrastructure
  2. -------------------------------
  3. Erlydtl allows templates to use i18n features based on gettext. Standard po
  4. files can be used to generate i18ized templates. A template parser/po generator
  5. is also provided.
  6. Translation done by `{%trans%}`, `{%blocktrans%}` tags and by wrapping variables
  7. in `_(...)` construction.
  8. Translation may be applied in compile time (translated strings are embedded in
  9. compiled template code) or in runtime (template will query gettext server during
  10. template rendering). 1'st is faster in terms of template rendering time, 2'nd is
  11. more flexible (you may update locales without any template recompilation).
  12. In order to apply compile-time translation, you must pass `blocktrans_fun` plus
  13. `blocktrans_locales` (this will translate `{%blocktrans%}`) and/or `locale` (this
  14. translates `{%trans%}` and `_("string literal")` values) to `erlydtl:compile/3`.
  15. Next, you should pass `locale` option to `my_compiled_template:render/2`.
  16. If you prefer runtime translation, just don't pass `blocktrans_fun` and/or `locale`
  17. compilation options and add `translation_fun` option to `my_compiled_template:render/2`.
  18. 1. In order to enable i18n you first, you'll need gettext library to be
  19. available on your lib_path.
  20. Library can be downloaded from http://github.com/etnt/gettext
  21. 2. Then you'll need to add a parse target on your makefile (or the script
  22. used to trigger template reparsing) trans:
  23. erl -pa ./ebin ./deps/*/ebin -noshell -s reloader -run i18n_manager \
  24. generate_pos "en,es" "./views/*/*.html,./views/*.html"
  25. rm -rf $(GETTEXT_DIR)/lang/default-old
  26. mv $(GETTEXT_DIR)/lang/default $(GETTEXT_DIR)/lang/default-old
  27. cp -rf $(GETTEXT_DIR)/lang/$(GETTEXT_TMP_NAME) $(GETTEXT_DIR)/lang/default
  28. rm -rf $(GETTEXT_DIR)/lang/$(GETTEXT_TMP_NAME)/*
  29. Mind that GETTEXT_DIR and GETTEXT_TMP_NAME must be bound to existing
  30. directories. Args passed to i18n_manager:generate_pos are locales that
  31. will be supported (generating dir structure and po files) and
  32. directories where generator will search for template files including
  33. trans tags.
  34. 3. If you wish to translate templates at compile-time, gettext server must be
  35. running before template parsing and it must be
  36. populated with the content of the po files. Consider adding this
  37. snipplet to the code before template parsing
  38. gettext_server:start(),
  39. LoadPo =
  40. fun(Lang)->
  41. {_, Bin} = file:read_file("./lang/default/"++ Lang ++"/gettext.po"),
  42. gettext:store_pofile(Lang, Bin)
  43. end,
  44. lists:map(LoadPo, ["es","en"]).
  45. Here locales are the codes are provided to gettext. Those codes must be
  46. a subset of the locales provided to po generation process.
  47. 4. Update strings. Edit po files on $(GETTEXT_DIR)/lang/default/$(LOCALE)/gettext.po
  48. translating msgstr to the translated version of their corresponding msgstr.
  49. 5. Generate localized templates providing `locale` compile option or use runtime
  50. translation via `translation_fun` rendering option.