Is there a Gem that stores translations in one table like CakePHP does?
The polymorphic CakePHP i18n-table has the following structure:
id
locale
model
foreign_key
field
content
Globalize3 i.e. uses a new table for each model translation which I find too redundant.
You can add various backends to the i18n gem which generally provides internationalization for ruby. Globalize3 is one option. A more lightweight alternative could be i18n-active_record which uses a single table similar to your CakePHP example.
Related
I want to customize the controller views generated by haml-rails. According to the Rails guide I am supposed to put my customized templates (e.g. index.html.haml) into lib/templates/[subfolders].
In this case I tried several subfolders (e.g. lib/templates/haml/scaffold, lib/generators/haml/scaffold/templates) but I could not get my custom templates to be used.
I know that I could write another generator easily, but I am wondering if there is a more DRY way to do so. In theory it should be possible:
In Rails 3.0 and above, generators don't just look in the source root for templates, they also search for templates in other paths.
I am using Rails (4.2.5.2), haml (4.0.7) and haml-rails (0.9.0).
Holy moly. It worked after all. It is correct to put the templates into lib/templates/haml/scaffold. And now comes the catch: spring will cache the templates. Hence, you must either restart spring after changes or prepend DISABLE_SPRING to the generator command:
DISABLE_SPRING=true rails g scaffold ...
I want to implement friendly_id into existing model. Application uses russian gem, which handles new or hand-saved records well, but it doesn't seem to work when I update records from the command line.
User.find_each(&:save) (as friendly_id docs syggested) generate slugs like --<id>.
I used custom normalize method to provide transliterated slug:
def normalize_friendly_id(input)
Russian.transliterate input.to_s.mb_chars.downcase
end
but it definitely may miss some edge cases, and handles string differently from "normal" workflow. What I'm looking for is the way to reuse regular create/update flow and native behavior.
The best way to resolve this problem:
1) Add gem 'babosa' n your Gemfile
gem 'friendly_id'
gem 'babosa'
2) Owerride friendly_id's method in your model
def normalize_friendly_id(text)
text.to_slug.transliterate(:russian).normalize.to_s
end
I want to create a simple form that adds translations dynamically into config/locales/en.rb. There's a way to do this? I'll need something like a parser to solve this?
With using Globalize3 gem given here on stack-overflow answer Add translation to I18N dynamically
Rails cast link:
http://railscasts.com/episodes/338-globalize3?view=asciicast
I've played a bit with globalize and rails 3, but from what I'm able to tell, globalize only works for ActiveRecord instances. I'd also like to be able to have the translations for other static pages in the database ( say for example header,footer, company details etc. ). Is this possible to do with globalize? If not, please recommend a gem that I should use.
I managed to do this using i18n-active_record
If you want to translate static page parts - you may create a new model (with globalize translations) and use it for storing static content (or just put your translations into config/locales folder)
Hoping some learned Rails developers here can recommend an existing Ruby on Rails plugin or gem that allows you to continue using the Simple I18n backend whilst allowing you to optionally specify translations in the database.
Here's why:
I have one Rails app used for many websites. For the example I'll just use 2 websites:
Website 1: Leprechauns R Us
Website 2: Unicorns R Us
Most translations are the same for both websites, but occassionally I want to override a translation. For example, in my en-US.yml file I have the following translation:
view_all: View all
And for most websites this translation is fine, including for website 1 (Leprechauns) where I'm happy to use "View all".
However, for website 2, I'd like to use "View all Unicorns" as the view_all translation and I'd like to specify this in the database. For maintenance reasons I don't want to specify this override in a YAML file.
Many thanks,
Eliot
In the end I opted to take advantage of Rails' I18n::Backend::Simple's ability to process both .yml files and .rb files as locale dictionaries.
Artifacts created:
DB migration to create a translations table with columns: locale, key, text
Translation model to map to the translations table
Class method to_locale_hash on Translation model that returns a locale keyed hash as required by I18n::Backend::Simple.load_rb
A single-line file located at config/translations.rb with the line 'Translation.to_locale_hash'
For the source code see the Spree extension (sorry not in a Rails plugin structure, it will be easy to move over to a plugin should you require it) here:
http://github.com/eliotsykes/spree-i18n-db/tree/master