Where can I store non-localizable fixed strings in rails? - ruby-on-rails

I have some url templates that I use in my app - they are not localizable strings, so config/locales/en.yml does not seem like the best place for them - where do I store these? For example, "http://www.example.com/apps?id=" is one such string.
I know there are several approaches possible (a constants file in the lib folder, or using rails configuration in rails4, or some config gem like configatron), but what is the'right' way?

I don't know that there is "a right way" - the new 'x' configuration option system in Rails 4 seems to be the most "Railsy" solution out there.
I personally think it is totally okay to have a GlobalConstants module defined in an initializer or a file in the lib directory. Put all your global constants in there and be diligent about using them throughout your app where needed.
For projects where there's a mix and match of i18n strings I like having a "base" i18n file and then using the override system to create a sort of inheritance chain between the i18n files. That's not always the appropriate solution but I've had it work out quite well a few times.
Hope that helps you.

As you said there are many solutions and it dependence on usage.
You can store them in environment variables.
dotenv gem help you handle it easier:
https://github.com/bkeepers/dotenv

Related

i18n language files. How to manage missing and unused keys

I have a Rails/Angular webapp. We use two different methods to translate our app: Ruby Globalize i18n with the corresponding yml language files, and angular-translate with the corresponding json language files. Managing language files can be a pain. I just found the i18n-tasks gem on github to help find missing and unused keys in my yml language files. It works great! Does anyone know of a similar static analysis tool to help manage json language files? Is there a better way to do this?
As I write this I am wondering if there is some sort of i18n transpilation that I could do. Keep a single language file and generate the yml and json from that.
Features wanted:
sort keys alphabetically
delete unused keys in all language files
add missing keys in all language files
i18n-tasks already supports JSON, but you would need to write a custom scanner to detect usages of angular-translate. See the custom scanner example.

rails i18n, the best way to manage localization

I'm using http://guides.rubyonrails.org/i18n.html for localization. Looks like that the only solution for updating localization is to update yml files. May be there are some solutions or gems which adds ability to update localization via application. For example we can include some gem and update localization files from our app. Is there some solutions for that?
Shameless plug for my own product here, but check out http://www.localeapp.com for a solution that works with YAML files out of the box.
Take a look at Globalize 3. It stores the translation/localization data in the database. There's also a gem to hook it up with ActiveAdmin.
Also, while you're at it, don't miss the rails-i18n gem mentioned in the guide. It translates Rails itself into many languages, so you don't have to do it yourself.
There is a great railscast to manage i18n through Redis.

Rails tool to search for untranslated strings using yaml locale files

Is there a way to look up all keys used in the rails application and add the untranslated keys to the different yaml locale files?
The tool should scan all the helpers, controllers, views, ... to look for keys being used. Next it should check whether the key is already added to the different locale yaml files.
I'm using the I18n that ships default with Rails.
Update: I'm kind of searching for a rake task that will just search my app for translations keys followed by a look up in the yaml locale files.
Do you mean something like i18n-tasks?
https://github.com/glebm/i18n-tasks
This gem analyses code statically for key usages, such as
I18n.t('some.key'), in order to:
Report keys that are missing or unused.
Pre-fill missing keys, optionally from Google Translate or DeepL Pro.
Remove unused keys.
For finding "untranslated" entries, I believe that any sufficiently advanced diff tool will do (you actually want to just filter the similarities, hiding differences.)
However, please keep in mind that this way you will only find the strings that are exactly the same as English. In other words it doesn't mean that they will be untranslated - some of them might sound exactly the same as English in a target language.
To actually make sure that something is or is not translated, I would recommend using Translation Memory. That will put you in control. However, for small projects it might be an overkill.

Should I put constants for my Rails project in environment.rb?

I want to store a path for a special directory used by my Rails application. Should I store that in environment.rb, or is there another place this is meant to go?
THE_DIRECTORY_PATH = '/path/to/directory'
Let's assume my controllers + models or libraries in /lib need access as well.
How about storing it in a YAML configuration file that gets loaded by an initializer? This Railscast has the details.
Use a robust YAML-file approach that allows per-environment settings. Try app_config, which has loads of great features, including referring syntax like AppConfig.the_directory_path.
If controllers need access to it, then a better place would be the ApplicationController.

How do I get TextMate to use Ruby on Rails by default?

How do I make TextMate use the Rails bundle by default? On some files it's enabled, and on others (including models) it's not, and I wish it would just assume that every .rb file is a Ruby on Rails file unless I tell it otherwise.
Is there a project-level or global setting for this?
It's possible to do it at a global level, but you have to edit and view some plist files to make it work. Check out this site for more information.
As far as I know you have to change the scope selector for each bundle. Maybe TM2 will be different.

Resources