Hi there I''m trying to rewrite a webgen site with multilingual support. For now, the langbar tag and the index.xx.page idiom work great to translate the content. I''m now looking to translate the template. Easiest solution was to copy default.template as default.fr.template (for example) and translate the file. However, I would prefer to have a unique template file (save the duplication and maintenance). I tried two solutions: - defining metadata in the header of each .page files, it works, but of course you have to duplicate even more the information in each .page file>> index.page--- title: About sort_info: 1 in_menu: true routed_title: Presentation description: User Help --->> index.fr.page--- title: ? propos sort_info: 1 in_menu: true routed_title: Pr?sentation description: Documentation --- - following a friend advice, defining some default metadata in config.yaml, using a hash table: default_meta_info: Page: description: en: User Help fr: Documentation but then I didn''t succeed to use the hashtable in the template and I would also need the language key used by webgen to choose between the fr or en option. default.template <h1> {description:} </h1> <h1> {description[en]:} </h1> <h1> {description:[en]} </h1> So how to build an efficient multilingual support with template? -- Simon
On 2010-10-20 13:54 +0200 Simon Denier wrote:> Hi there > > I''m trying to rewrite a webgen site with multilingual support. > > For now, the langbar tag and the index.xx.page idiom work great to > translate the content. > > I''m now looking to translate the template. > > Easiest solution was to copy default.template as default.fr.template > (for example) and translate the file. However, I would prefer to have > a unique template file (save the duplication and maintenance).You can look through the mailing list archives, there have been some threads with exactly this issue. One solution was to use the gettext standard to localize strings. This works fine, especially when you have many things to translate. However, this introduces another dependency. I would suggest using conditionals in ERB fragments if you don''t have many things to translate between the two templates. Taking your example: default.template: <% desc = case context.node.lang when ''en'' then ''User Help'' when ''fr'' then ''Documentation'' end %> ... <h1><%= desc %></h1> -- Thomas
On 20 oct. 2010, at 15:51, Thomas Leitner wrote:> On 2010-10-20 13:54 +0200 Simon Denier wrote: >> Hi there >> >> I''m trying to rewrite a webgen site with multilingual support. >> >> For now, the langbar tag and the index.xx.page idiom work great to >> translate the content. >> >> I''m now looking to translate the template. >> >> Easiest solution was to copy default.template as default.fr.template >> (for example) and translate the file. However, I would prefer to have >> a unique template file (save the duplication and maintenance). > > You can look through the mailing list archives, there have been some > threads with exactly this issue. One solution was to use the gettext > standard to localize strings. This works fine, especially when you have > many things to translate. However, this introduces another dependency.Sorry, are you referring to http://groups.google.com/group/webgen-users/? I was not able to find the info. Maybe it could be added to the faq?> > I would suggest using conditionals in ERB fragments if you don''t have > many things to translate between the two templates. Taking your example: > > default.template: > > <% > desc = case context.node.lang > when ''en'' then ''User Help'' > when ''fr'' then ''Documentation'' > end > %> > ... > <h1><%= desc %></h1>Thanks for the hint. I prefer the syntax of hash tables but your post gave me the tip: <% desc = { "en" => "Geco User Help", "fr" => "Documentation Geco" } %> <h1><%= desc[context.node.lang] %></h1> Now it works! What could be even better is to push the translation data into another file and to import this file in the template. No luck for now (I''m not much of ruby hacker) File default.languages <% desc = { "en" => "Geco User Help", "fr" => "Documentation Geco" } %> File default.template <% erb = File.open(''default.languages'') { |fp| ERb.new(fp.read) } erb.run %> in general, it crashes saying it can''t find file ''default.languages'' (though I tried to copy it in different folders)
On 2010-10-21 02:36 +0200 Simon Denier wrote:> Sorry, are you referring to > http://groups.google.com/group/webgen-users/? I was not able to find > the info. Maybe it could be added to the faq?The webgen-users [google group][1] is just a mirror of the webgen-users [mailing list][2]. This information can be found directly on http://webgen.rubyforge.org. [1]: http://groups.google.com/group/webgen-users/ [2]: http://rubyforge.org/mail/?group_id=296> What could be even better is to push the translation data into > another file and to import this file in the template. No luck for now > (I''m not much of ruby hacker) > > File default.languages > <% > desc = { > "en" => "Geco User Help", > "fr" => "Documentation Geco" > } > %> > > File default.template > <% > erb = File.open(''default.languages'') { |fp| ERb.new(fp.read) } > erb.run > %> > > in general, it crashes saying it can''t find file > ''default.languages'' (though I tried to copy it in different folders)If you don''t specify a directory, `File.open` tries to find the file in the current working directory, ie. where you executed webgen. However, webgen internally stores a reference to the webgen website directory which is processed. Try this: File default.template <% erb = File.open(File.join(context.website.directory, ''default.languages'')) { |fp| ERb.new(fp.read) } erb.run %> Then you can store the file `default.languages` in the website directory and it will work every time. You could also use YAML for storing the hash in the external file: File default.languages desc: en: Geco User Help fr: Documentation Geco File default.template <% translations = YAML::load(File.read(File.join(context.website.directory, ''default.languages''))) %> <h1><%= translations[''desc''][''en''] %></h1> -- Thomas
On 21 oct. 2010, at 20:20, Thomas Leitner wrote:> On 2010-10-21 02:36 +0200 Simon Denier wrote: > > Then you can store the file `default.languages` in the website > directory and it will work every time. > > You could also use YAML for storing the hash in the external file: > > File default.languages > > desc: > en: Geco User Help > fr: Documentation Geco > > File default.template > > <% translations = YAML::load(File.read(File.join(context.website.directory, ''default.languages''))) %> > <h1><%= translations[''desc''][''en''] %></h1>Thanks Thomas! I''m not much of a Ruby hacker, so that was very helpful. This last solution is simple and elegant. -- Simon -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/webgen-users/attachments/20101022/b31e907e/attachment.html>