I'm trying to use I18n locales for a multisite application and wondering whether it's at all possible to load some extra translation locale files after the rails app has initialized?
I'm already using the excellent Globalize gem for ActiveRecord model translations, but I essentially want to be able to load the locales for a given site from it's own folder. I'm therefore trying to use something along the following lines in my site loader code:
I18n.load_path = File.join(site.path, 'locales')
Where the site.path variable relates to the current site's configuration & asset directory.
I don't want to rely on adding an i18n initializer to glob through the sites directory for locales for 2 reasons:
I can't guarantee a different site won't use the same translatable string, and I don't want them overwriting each other.
I don't want to have to restart the application each time a new site has been added.
If it helps, my logic to doing it this way allows for some sites to be multi language, and some not, and allows me to be lazy about whether a site is multi-language or not because if I can do the above successfully, I can use I18n.available_locales to determine whether or not to show the lanuage switcher in the admin app etc.
Related
I have a Rails application that is using multiple gems. Each gem provides assets, that are added to the Rails asset path:
main app
admin engine with customised forms
wysiwyg engine
I want to override some of the partials provided by the wysiwyg engine inside the admin engine. I know that I need to affect the order that each engine adds its asset paths to the ActionView lookup context used by render for partial resolution, but I'm not sure how to do this.
This is actually documented by Rails but it took me a long time to find the relevant documentation. I assumed I needed to affect the bundler gem file load order, but I actually needed to define the order that engines (railties) are initialised:
https://api.rubyonrails.org/classes/Rails/Engine.html#class-Rails::Engine-label-Loading+priority
In my config/application.rb I added:
config.railties_order = [:main_app, Admin::Engine, :all]
I would prefer to define the dependency between the admin engine and the wysiwyg engine, but this does address my issue.
Where do I find activerecord and date translations for whatever language I'm trying to add to my site?
Rails comes equipped with an I18n framework / API, which like most such frameworks involves defining a message catalog for each supported language. In the case of Rails, catalogs are YAML files under config/locales.
Rails defines standard keys for general ActiveRecord validation messages, but only comes with English translations by default. An alternative to providing translations from scratch yourself is to install the rails-i18n gem (svenfuchs/rails-i18n), which comes bundled with translations for many supported languages. These include both default ActiveRecord messages and assorted other things such as date and currency formats, days of the week, etc. You can mix and match the translations in rails-i18n with your own custom translations of course by adding your own locale files.
Note: The contributors to rails-i18n have done a great job, but not all locales have the full set of ActiveRecord messages, so in some cases you will have to provide your own (or consider contributing a pull request to the project).
EDIT:
The pre-defined catalogs that come with rails-i18n are installed with the gem. Running bundle info rails-i18n will show you the local install path; the locale files are in rails/locale if you want. Rails should find these automatically with further effort. So, in rails console:
foobar> I18n.locale = :tr
=> :tr
foobar> I18n.t 'time.pm'
=> "öğleden sonra"
You can extend or override the defaults by creating a custom locale file in your app's config/locales directory. The I18n search path will check config/locales before moving onto the default, so you only need to supply new or changed translations in your custom locale file.
I want to know if there is a way (or a gem) that can compile me a Rails application into a static web site; I have some files that need to only be compiled once (i.e. they have no dynamic content but they need to be parsed at least once). I can't seem to find any way to do that so I have a feeling that it might not even be possible.
I don't believe there's a way to do that with an entire Rails app. That's more the territory of https://github.com/mojombo/jekyll or https://github.com/imathis/octopress. If it's only a few pages you can use caches_page :page1, :page2, ... in your controllers. That will write the fully-rendered page to public/ so that it can be served directly by Nginx/Apache on subsequent requests.
Edit In Rails 4 you'll need to use the actionpack-page_caching gem.
I need to localize an app, so I created a file for Spanish translations, but the config/locales/es.yml file doesn't seem to be properly loading, or maybe it loads after my templates are evaluated.
First a little context, I'm using the handlebars_assets and haml_assets gems. The latter allows to use the ActionView helpers within the templates in the asset pipeline and it also has access to the application classes, so one could write templates like:
.some_div
= SomeModel.model_name.human
And it works, except that it's returning the fallback name of the model. If I make these kinds of translations in a view though (instead of .hbs.haml template) I get the proper translated string. This is also true if I execute the translation methods in the rails console.
Following the Rails Guides I created the following initializer:
I18n.default_local = :es
Which is working properly if I consider that translating in views renders the expected output. My application is mostly living on the client side, so I really don't use Rails' views, it's a single page application managed with the help of Backbone. All the UI lives in templates that get rendered through the asset pipeline. So my question is, has anyone managed to make I18n work with this setup?
Since there are so many options there available for internationalization of a rails app, which gems or plugins are the best (today) for adding i18n support to a rails app.
Im using I18n bundled with rails for the application messages, button labels, and model attribue names.
But I also need to let the users to input content and the translated version of the content, but there are so many options for this right now that I don't really know which one to use.
We at Mynewsdesk (the guys behind the translate plugin) has moved on, now we're using Web Translate It to manage our locales. We've bloged about our translation workflow.
I like this one:
https://www.github.com/mynewsdesk/translate
because it has a nice webGUI for translating new/changed strings