Can you implement custom fallbacks for Globalize3?
-
24-06-2021 - |
سؤال
I'd like to have some custome fallbacks for Globalize3 on Rails.
I know you can set
config.i18n.fallbacks = true
and have untranslated text fallback to its default locale, but I would rather have something that would indicate it untranslated, something like a #not tnranslated# before or after the falled back text.
For example if the default locale text is
Foo bar
I would like the fallback text shown on an un translated text be
Foo bar #not translated!#
The only way I can figure to do this is have an if else around the text and check if the current locale is translated, and that doesn't seem to elegant, something like so.
<% if article.is_translated?(I18n.locale) %>
<%= article.title %>
<% else %>
<%= article.title %>#not translated#
<% end %>
Any advice would be greatly appreciated.
المحلول
How about adding a patch like this:
Globalize::ActiveRecord::Adapter.class_eval do
def fetch(locale, name)
record.globalize_fallbacks(locale).each do |fallback|
value = stash.contains?(fallback, name) ? fetch_stash(fallback, name) : fetch_attribute(fallback, name)
unless fallbacks_for?(value)
set_metadata(value, :locale => fallback, :requested_locale => locale)
return value if (fallback == locale)
return value + " #not translated#"
end
end
return nil
end
end
In case it's not clear, the two lines that I changed are these ones:
return value if (fallback == locale)
return value + " #not translated#"
لا تنتمي إلى StackOverflow