{"action":"created","issue":{"url":"https://api.github.com/repos/shioyama/mobility/issues/176","repository_url":"https://api.github.com/repos/shioyama/mobility","labels_url":"https://api.github.com/repos/shioyama/mobility/issues/176/labels{/name}","comments_url":"https://api.github.com/repos/shioyama/mobility/issues/176/comments","events_url":"https://api.github.com/repos/shioyama/mobility/issues/176/events","html_url":"https://github.com/shioyama/mobility/issues/176","id":301981268,"number":176,"title":"Fallback for empty translations","user":{"login":"darkcode85","id":13897422,"avatar_url":"https://avatars0.githubusercontent.com/u/13897422?v=4","gravatar_id":"","url":"https://api.github.com/users/darkcode85","html_url":"https://github.com/darkcode85","followers_url":"https://api.github.com/users/darkcode85/followers","following_url":"https://api.github.com/users/darkcode85/following{/other_user}","gists_url":"https://api.github.com/users/darkcode85/gists{/gist_id}","starred_url":"https://api.github.com/users/darkcode85/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/darkcode85/subscriptions","organizations_url":"https://api.github.com/users/darkcode85/orgs","repos_url":"https://api.github.com/users/darkcode85/repos","events_url":"https://api.github.com/users/darkcode85/events{/privacy}","received_events_url":"https://api.github.com/users/darkcode85/received_events","type":"User","site_admin":false},"labels":[{"id":617584701,"url":"https://api.github.com/repos/shioyama/mobility/labels/fallbacks_plugin","name":"fallbacks_plugin","color":"d93f0b","default":false}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2018-03-03T08:43:32Z","updated_at":"2018-03-03T15:14:43Z","closed_at":null,"author_association":"NONE","body":"### Context\r\n\r\nRight now is impossible to deal with dynamic fallbacks as we have to set the fallback in the model and can not be done dynamically.\r\n\r\n### Possible Solution\r\n\r\nWould be good to have a new option `fallbacks_for_empty_translations` (not necessarily this name), this is an example of how it could work:\r\n\r\n```\r\nclass Blog < ActiveRecord::Base\r\n  extend Mobility\r\n  translates :title, type: :string, fallbacks_for_empty_translations: true\r\nend\r\n\r\nputs blog.string_translations.inspect\r\n# => [#<Mobility::ActiveRecord::StringTranslation id: 1, locale: \"ja\", key: \"title\", value: \"モビリティを使う!\", translatable_id: 1, translatable_type: \"Blog\">,\r\n#<Mobility::ActiveRecord::StringTranslation id: 1, locale: \"en\", key: \"title\", value: \"\", translatable_id: 1, translatable_type: \"Blog\">,\r\n#<Mobility::ActiveRecord::StringTranslation id: 1, locale: \"es\", key: \"title\", value: nil, translatable_id: 1, translatable_type: \"Blog\">]\r\n\r\nI18n.locale = :ja\r\nblog.title # => 'モビリティを使う!'\r\n\r\nI18n.locale = :en\r\nblog.title # => 'モビリティを使う!'\r\n\r\nI18n.locale = :es\r\nblog.title # => 'モビリティを使う!'\r\n\r\n# Locale without translation object\r\nI18n.locale = :it\r\nblog.title # => 'モビリティを使う!'\r\n```\r\n\r\nI think this will help a lot to deal with empty translations in some scenarios where the default fallback can not be hardcoded.\r\n"},"comment":{"url":"https://api.github.com/repos/shioyama/mobility/issues/comments/370154618","html_url":"https://github.com/shioyama/mobility/issues/176#issuecomment-370154618","issue_url":"https://api.github.com/repos/shioyama/mobility/issues/176","id":370154618,"user":{"login":"darkcode85","id":13897422,"avatar_url":"https://avatars0.githubusercontent.com/u/13897422?v=4","gravatar_id":"","url":"https://api.github.com/users/darkcode85","html_url":"https://github.com/darkcode85","followers_url":"https://api.github.com/users/darkcode85/followers","following_url":"https://api.github.com/users/darkcode85/following{/other_user}","gists_url":"https://api.github.com/users/darkcode85/gists{/gist_id}","starred_url":"https://api.github.com/users/darkcode85/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/darkcode85/subscriptions","organizations_url":"https://api.github.com/users/darkcode85/orgs","repos_url":"https://api.github.com/users/darkcode85/repos","events_url":"https://api.github.com/users/darkcode85/events{/privacy}","received_events_url":"https://api.github.com/users/darkcode85/received_events","type":"User","site_admin":false},"created_at":"2018-03-03T15:14:43Z","updated_at":"2018-03-03T15:14:43Z","author_association":"NONE","body":"Thanks @shioyama, I will explain my scenario in detail so you can understand my situation:\r\n\r\nI have a `Site` model that has a `has_many :languages` relationship with the `Language` model, the owner of the site can create has many languages and assign the default language to any of them. If they set up multiple languages then when they create a blog I have a multi language form where the title, content, etc.. can be fill out in the available languages, but what normally happen is that the owners only fill out the content for the default language, so if you visit the blog on a locale without translation then it show a blank content, the business logic here is to show the content for the default language if the translations are blank.\r\n\r\nSo for that reason I have tried to add dynamic fallbacks because for example for the site 1 the fallback hash could be `fallbacks: { es: :en, ... }` and for the site 2 the fallbacks could be different `fallbacks: { en: :ja, ... }` depending to the default language used. I tried to do that using Mobility and I didn't have success, I also tried the `fallbacks: true` but it doesn't work in this scenario.\r\n\r\nWhat I had in other app in a similar scenario (using globalize) was the `fallbacks_for_empty_translations: true` option which fixed this scenario. \r\n\r\nI also tried the default plugin but again looks like I can not pass dynamic content, ej: `default: lambda { |attribute, locale| self.title_\"#{default_language_locale}\" }`\r\n\r\nSo I'm not sure if fix this problem is possible in Mobility but if not I propuse to add the `fallbacks_for_empty_translations: true` option to just use the default content instead of pass dynamic hashes in the model context (will not be something good to add it on all the models).\r\n\r\nAnother possible solution would be to globally set the fallbacks in the controller context instead to have to add it in each model, so for example in the `application_controller.rb` we can have a `before_action` to set the fallback hash depending in the site configuration and that will work globally for all the models.\r\n\r\nHope this explanations make sense and you can now understand much better the problem and see if Mobility already has a solution for this scenario or maybe add one of the possible solution (or a new one).\r\n\r\n\r\n\r\n"}}
{"actor":{"display_login":"darkcode85"}}