Last active
July 19, 2017 22:54
-
-
Save ZempTime/b1195179965f5ce929fb08302e2e0b13 to your computer and use it in GitHub Desktop.
Translation Model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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