Skip to content

Instantly share code, notes, and snippets.

@ZempTime
Last active July 19, 2017 22:54
Show Gist options
  • Save ZempTime/b1195179965f5ce929fb08302e2e0b13 to your computer and use it in GitHub Desktop.
Save ZempTime/b1195179965f5ce929fb08302e2e0b13 to your computer and use it in GitHub Desktop.
Translation Model
# pseudocode
# assuming message is set by the teacher, and isn't stored on a 1-1 message/parent basis
# but a 1 message - many parents basis
class Message < ActiveRecord::Base
has_many :translations
def translated_message(language_code='en')
translation = translations.where(language_code: language_code).first
if translation
return translation
else
GoogleTranslate.conver(message)
end
end
end
class Translation < ActiveRecord::Base
belongs_to :message
validates :language_code, in: LANGUAGE_CODES
end
# So in the controller, we can just ask the message for the right text to send:
@message.translated_message(parent.language_code)
# I think we would also benefit from breaking out language codes into its own domain table,
# since there at max only a couple hundred of em. Then we can just point all the other models at these,
# and ask questions like, "give me all the messages in spanish" or whatever table points to it
class Language < ActiveRecord::Base
# code
# name
end
# might be a more standard setup for above domain table
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment