i18n rails get translations from two different yml files - ruby-on-rails

I am using tolk for translation, but tolk takes all my values from en.yml and dumpes them in es.yml overwriting the existing content .
There are some stuff i don't want to be overwritten , so when i am searching for the es translations, i want rails to look in both es.yml and es.defaults.yml
( and so, i can keep isolated what i generate with tolk, and what remais the same )
Is there a way i can do this?
Thanks

Rails loads every file in the config/locales/ directory, so it will probably already work like you're suggesting. You can even organize it further than that, according to the I18n Guide:
http://guides.rubyonrails.org/i18n.html#organization-of-locale-files
However, I think that with duplicate key structures, Rails will probably override the values of the earlier loaded (sorted by file name) locale file with the values of the later loaded file. So please try to avoid duplicate keys.

Related

I can't find where a string is getting defined -- any tricks to find its source?

I'm using:
Rails 3.2x
Spree 1.2
Ruby 1.9.3x
I'm trying to edit the title of one of my pages, and I cannot find where it is getting defined. It is showing up in my base ERB file as 'title', but that name is sufficiently generic to make it next to impossible to find where it is defined.
I have prodded everywhere I can think, I've tried searching for "title =", but nothing is working. I tried calling source_location on it, but that appears to only work on methods.
Any tricks for finding where a variable is defined?
I can't think of an elegant way. A dumb-but-probably-effective way would be to dump stack trace in your erb, then see what those locations are doing and if title is defined there. It has to enter somewhere between the start of program and invoking your erb.
When I can't find something, I use grep -ri some_string . at the command-line to recursively search all the content of the directory.
It's also a good tactic to let your editor search all the source code, since the ones worth using have the ability to search through all files in a directory.
it is created from a mixture of product names, a site config, and something else
An alternate trick is to add a HTML-comment section in your ERB file, and put the pertinent information for the components used to create the title into that section. Then, let the pages be generated and look inside the page's content to determine what table and row ID it is, the site_config filename, etc.
You really should be able to figure it out based on the parts that are concatenated to build the title and then search your database or files. That information isn't magically created out of thin air by Rails; Someone had to tell Rails how to define the title. But, people move on, or they don't document correctly, so try the embedded information trick.

Is there an easy way of keeping i18n message files in sync?

I am wondering if there is an easy way of keeping different i18n files in sync, so when adding a key/value in messsage_aa.properties would result in the same line in a message_bb.properties file? But this offcourse using a different value..
I would really like to have the same key on the same line number in each of my message_xx.properties file..
Any suggestion?
This feature depends on your IDE.
As an example, I use ResourceBundle plugin in Eclipse that partially do what you are asking.
"Partially", because it keeps the same order but not necessary the same line (i.e. it doesn't skip line to match one file with another).

Where should I put a list of static data in Ruby on Rails?

I have a static string array of Cities, States, Categories, etc that is accessed by my application in various places. Where should I put this? In a yml file someplace or a rb file in the lib directory?
Thanks!
See this so question
Basically, put constants in /config/initializers in a .rb file.
EDIT:
So this is not really constant data since "once a couple of months" the info will change. In that case you should put the info in the database and cache it. The caching will prevent round trips to the database and you can expire the cache when you need to update the info.
I advise you to put this in config/locales/*.yml files. It's good because you can specify different city names for different languages. See internationalization guide for more information.

Rails Generator: generate files based on already existing rails files

I wanted to make a generator that created files (and directories, etc...) based on already existing files in the app (for instance, the views or controllers). So if we had views set up like this
-app
-views
- layouts
- application.html.erb
- users
- index.html.erb
- show.html.erb
- etc ...
and I wanted to create files based on them I can do (with just ruby)
directories = Dir.entries("#{Rails.root}/app/views")
directories.each do |directory|
unless directory == "." or directory == ".."
files = Dir.entries("#{Rails.root}/app/views/#{directory}")
files.each do |file|
unless file == "." or file == ".."
text = File.read("#{Rails.root}/app/views/#{directory}/#{file}")
something #=> whatever else needs to go here to edit the file
something else #=> output_file.puts whatever
end
end
end
end
so this is basically what I would like to do with a generator so I can roll my code into a plugin and use it for other apps.
First question, how can I generate arbitrary files (with filenames based on existing filenames using the generator. Is it appropriate to cycle through the directories like I did above, grab the directory/file and generate files? Is there a way to do what I did using a simpler method (mine seems easily breakable).
Also, should I put all that read/format/write code inside the generator itself and just pass a string into the "initialize content" section of create_file or should I put it somewhere else. Or should I use the generator to create the bare files and populate it with an init script?
Is there a more rails type of way of populating generated files, or should I just shove all my formatting code inside the generator. If so, what is the appropriate way to approach this.
I am not sure if you want to know how generators are built in rails3 or not. The code you are showing is not very generator-like. In generators you can use all commands from Thor, which offers you a very powerful toolset of manipulating files, and injecting code (strings) into classes or files.
So I would most definitely fill your files inside a generator, because then it happens on user request, and the user can choose whether or not certain files need or can be overwritten or not.
Inside your gem, you will have a lib/generators folder, containing a templates folder, containing all files you might want to place inside the rails application.
From the Thor documentation, here is a nice example to construct files in a generator.
Hope this helps.
there's a simple API to use generators in Rails. here you can find a good guide:
http://guides.rubyonrails.org/generators.html
if you want to check some code:
https://github.com/coderloop/tamed_beast (I'm the author of its generators)
https://github.com/pilu/web-app-theme (another clean example)

Rails: Hash#to_json - split over multiple lines?

I'm having a slight annoyance with the javascript_I18n plugin, which generates js-friendly versions of the I18n translation tables, so that you can localise your javascript. It all works fine, but it works by calling to_json on each locale's translations hash and outputting the results into a file. When you call to_json on a hash the resulting string is all a single lne, which means you end up with files with an enormous line at the end.
This in turn is stopping git from being able to merge any changes, because the merge works on a line by line basis and can't deal with a single massive line with changes in the middle somewhere. It's not a massive problem because i can always just re-generate the js-friendly translation file with a rake task that comes with the plugin (which replaces the mid-merge file with a brand new one which i can just commit in), but it's just a bit annoying. It occurred to me that if the json was output on different lines, instead of all the same line, then it wouldn't be a problem, and it wouldn't even make the file that much bigger, just inserting two characters (\n) per line.
Before i try and hack the resulting string with a gsub to split it onto seperate lines, is there a nicer way to call to_json on a hash and output the results onto seperate lines? Or a nicer way to solve this problem generally? (i can't find much useful in the documentation for javascript_I18n).
Grateful for any advice - max
Not to answer you question, but to give a suggestion:
It would probably be easier to just ignore all the generated js translation files.
This is because even if you separate the translation into multiple lines, there are still chances of merge conflicts, which you probably already resolved once in your yaml file.
I would set it up this way:
1). gitignore all the js translation files.
2). In ActionController, add a before filter to generate the js files automatically on each load in development mode only.
3). Tweak your deploy.rb file to generate the js files after code update.
No more merge conflicts! (at least for js translation files) :D
Aaron Qian

Resources