NUBIC / surveyor

A Rails gem that lets you code surveys, questionnaires, quizzes, etc... and add them to your app.
http://nubic.github.com/surveyor
MIT License
750 stars 273 forks source link

Translations seemingly ignored #500

Open archonic opened 9 years ago

archonic commented 9 years ago

The app is translating just fine but survey pages are set to the default.

SurveyorController extends ApplicationController where the local is populated, so I don't see why setting the locale in SurveyorController would change anything, but it did. Setting the locale in SurveyorController has the survey pages translate, but the survey content is set to default. The database translation requests (I know there's a PR to remove these) are set to "fr" but are still rendering "en" content:

SurveyTranslation Load (1.7ms)  SELECT  "survey_translations".* FROM "survey_translations" WHERE "survey_translations"."survey_id" = ? AND "survey_translations"."locale" = ?  ORDER BY "survey_translations"."id" ASC LIMIT 1  [["survey_id", 15], ["locale", "fr"]]
  CACHE (0.1ms)  SELECT  "survey_translations".* FROM "survey_translations" WHERE "survey_translations"."survey_id" = ? AND "survey_translations"."locale" = ?  ORDER BY "survey_translations"."id" ASC LIMIT 1  [["survey_id", 15], ["locale", "fr"]]
  CACHE (0.1ms)  SELECT  "survey_translations".* FROM "survey_translations" WHERE "survey_translations"."survey_id" = ? AND "survey_translations"."locale" = ?  ORDER BY "survey_translations"."id" ASC LIMIT 1  [["survey_id", 15], ["locale", "fr"]]
  CACHE (0.1ms)  SELECT  "survey_translations".* FROM "survey_translations" WHERE "survey_translations"."survey_id" = ? AND "survey_translations"."locale" = ?  ORDER BY "survey_translations"."id" ASC LIMIT 1  [["survey_id", 15], ["locale", "fr"]]
  Rendered partials/_answer.html.haml (83.7ms)
  CACHE (0.1ms)  SELECT  "survey_translations".* FROM "survey_translations" WHERE "survey_translations"."survey_id" = ? AND "survey_translations"."locale" = ?  ORDER BY "survey_translations"."id" ASC LIMIT 1  [["survey_id", 15], ["locale", "fr"]]
  CACHE (0.2ms)  SELECT  "survey_translations".* FROM "survey_translations" WHERE "survey_translations"."survey_id" = ? AND "survey_translations"."locale" = ?  ORDER BY "survey_translations"."id" ASC LIMIT 1  [["survey_id", 15], ["locale", "fr"]]
  Rendered partials/_answer.html.haml (46.4ms)
  CACHE (0.1ms)  SELECT  "survey_translations".* FROM "survey_translations" WHERE "survey_translations"."survey_id" = ? AND "survey_translations"."locale" = ?  ORDER BY "survey_translations"."id" ASC LIMIT 1  [["survey_id", 15], ["locale", "fr"]]
  CACHE (0.1ms)  SELECT  "survey_translations".* FROM "survey_translations" WHERE "survey_translations"."survey_id" = ? AND "survey_translations"."locale" = ?  ORDER BY "survey_translations"."id" ASC LIMIT 1  [["survey_id", 15], ["locale", "fr"]]
  Rendered partials/_answer.html.haml (43.6ms)
  CACHE (0.1ms)  SELECT  "survey_translations".* FROM "survey_translations" WHERE "survey_translations"."survey_id" = ? AND "survey_translations"."locale" = ?  ORDER BY "survey_translations"."id" ASC LIMIT 1  [["survey_id", 15], ["locale", "fr"]]
  CACHE (0.1ms)  SELECT  "survey_translations".* FROM "survey_translations" WHERE "survey_translations"."survey_id" = ? AND "survey_translations"."locale" = ?  ORDER BY "survey_translations"."id" ASC LIMIT 1  [["survey_id", 15], ["locale", "fr"]]
  Rendered partials/_answer.html.haml (43.3ms)
  CACHE (0.1ms)  SELECT  "survey_translations".* FROM "survey_translations" WHERE "survey_translations"."survey_id" = ? AND "survey_translations"."locale" = ?  ORDER BY "survey_translations"."id" ASC LIMIT 1  [["survey_id", 15], ["locale", "fr"]]
  CACHE (0.1ms)  SELECT  "survey_translations".* FROM "survey_translations" WHERE "survey_translations"."survey_id" = ? AND "survey_translations"."locale" = ?  ORDER BY "survey_translations"."id" ASC LIMIT 1  [["survey_id", 15], ["locale", "fr"]]
  Rendered partials/_answer.html.haml (39.9ms)
  Rendered partials/_question.html.haml (537.9ms)
  Rendered partials/_section.html.haml (563.5ms)

I checked the value of I18n.locale in _answer.html.haml and it was also fr. Pretty odd.

Any ideas what I could be doing wrong?

prusswan commented 9 years ago

It does not look like the translations were ever implemented in full. "survey_translations" only references survey_id and not any of the translatable models like Question or Answer with their own text fields, so it would not be very useful.

yoon commented 9 years ago

@archonic the UI (prompts, buttons, etc.) is localized separately from the text of the questions/answers themselves

@prusswan translations were implemented in full - they are simply linked through the survey, not via individual models

prusswan commented 9 years ago

@yoon Okay, I saw the yaml files, but initially I assumed they are just part of DSL, and not also used for retrieving the translations.

Edit: It seems to be working on my end, the yaml content for the entire survey is stored in the SurveyTranslation model. One caveat however is that the questions and answers (or any other fields to be translated) must have reference identifiers, which cannot be just numbers, since they will just end up as numeric keys in hash returned from parsing the yaml content, and translates finds the translation by reference_identifier (which is a string key). This can be fixed by something like deep_stringify_keys, which is available in Rails 4.