I''d be grateful for feedback regarding the following: /* Iterate over elements and lookup translation for language in JSON dictionary */ function translateChildren(parent, language) { var phrases = parent.getElementsByClassName(''lang''); phrases.each(function(p) { var phrase = p.readAttribute(''for''); p.innerHTML = translations[phrase][language]; }); } /* Get language from Cookie */ function getLanguage() { document.cookie.scan(/language=(..)/, function(match){ lang = match[1] }) return lang; } /* JSON dictionary */ var translations = { ''search'': {''en'': "Search", ''de'': "Suche"}, ''logout'': {''en'': "Logout", ''de'': "Abmelden"}, ''account'': {''en'': "Account", ''de'': "Konto"} } ... <div id="menu"> <a href = "" class = "lang" for = "search">Search</a> <a href = "" class = "lang" for = "account">Account</a> <a href = "" class = "lang" for = "logout">logout</a> </div> <script>translateChildren($(''menu''), getLanguage())</script> ... The inline script tag is a bit messy, but means the page can be translated before the onload event would trigger it. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Is this a bad idea then? On Jun 1, 12:44 pm, Adam Groves <adam.gro...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''d be grateful for feedback regarding the following: > > /* Iterate over elements and lookup translation for language in JSON > dictionary */ > function translateChildren(parent, language) { > > var phrases = parent.getElementsByClassName(''lang''); > > phrases.each(function(p) { > var phrase = p.readAttribute(''for''); > p.innerHTML = translations[phrase][language]; > }); > > } > > /* Get language from Cookie */ > function getLanguage() { > document.cookie.scan(/language=(..)/, > function(match){ > lang = match[1] > }) > return lang; > > } > > /* JSON dictionary */ > var translations = { > ''search'': {''en'': "Search", ''de'': "Suche"}, > ''logout'': {''en'': "Logout", ''de'': "Abmelden"}, > ''account'': {''en'': "Account", ''de'': "Konto"} > > } > > ... > <div id="menu"> > <a href = "" class = "lang" for = "search">Search</a> > <a href = "" class = "lang" for = "account">Account</a> > <a href = "" class = "lang" for = "logout">logout</a> > </div> > <script>translateChildren($(''menu''), getLanguage())</script> > ... > > The inline script tag is a bit messy, but means the page can be > translated before the onload event would trigger it.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Is there a specific reason that you need to load the language dynamically? Wouldn''t using a server side script like PHP do? Doing it this way only adds to maintenance. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
I was thinking of the benefits of using it with page caching: it would mean that you wouldn''t need to cache for every language. On Jun 4, 1:28 pm, Kroc Camen <KrocCa...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Is there a specific reason that you need to load the language > dynamically? Wouldn''t using a server side script like PHP do? > Doing it this way only adds to maintenance.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Adam Groves a écrit :> I was thinking of the benefits of using it with page caching: it would > mean that you wouldn''t need to cache for every language.I see very little point in that. Cache is expandable on the server side. OTOH, translating client-side is putting way too much processing there, and will result in detectable latencies both for loading (you''re trying to load all the languages there) and then applying the translation (the user is likely to see the untranslated version first). -- Christophe Porteneuve a.k.a. TDD "[They] did not know it was impossible, so they did it." --Mark Twain Email: tdd-x+CfDp/qHev2eFz/2MeuCQ@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
"... the user is likely to see the untranslated version first" Yup - that''s exactly what''s happening in ie. On Jun 5, 8:06 am, Christophe Porteneuve <t...-x+CfDp/qHev2eFz/2MeuCQ@public.gmane.org> wrote:> Adam Groves a écrit : > > > I was thinking of the benefits of using it with page caching: it would > > mean that you wouldn''t need to cache for every language. > > I see very little point in that. Cache is expandable on the server > side. OTOH, translating client-side is putting way too much processing > there, and will result in detectable latencies both for loading (you''re > trying to load all the languages there) and then applying the > translation (the user is likely to see the untranslated version first). > > -- > Christophe Porteneuve a.k.a. TDD > "[They] did not know it was impossible, so they did it." --Mark Twain > Email: t...-x+CfDp/qHev2eFz/2MeuCQ@public.gmane.org--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---