How to Update Rails Locale Yaml File Without Loosing Comments and Variables

How do I update Rails locale YAML file without loosing comments and variables?

I don't think you can.

YAML ignores comments in a data file, but it doesn't parse them, so they are thrown away as the file is loaded. Once the file is loaded they're gone.

The only way to do what you want that I can think of, is to open the file outside of YAML, then write the comments, then write the YAML content created using to_yaml. Something like:

require 'yaml'

data = {
'foo' => 'bar',
}

File.open('data.yaml', 'w') do |fo|
fo.puts "# Don't mess with this."
fo.puts data.to_yaml
end

Which creates:

# Don't mess with this.
---
foo: bar

Is there a clever way to pass a yml locale file to my client for translation?

This is exactly what Locale was created for : you upload your YAML files, your client/translator edits the content and you sync YAML back down. You don't email files and you don't have to deal with crappy file formats - check it out!

Full disclosure : I co-founded and develop Locale.

Erb params in yml file

Template:

t('application.titleserv', search: params[:search] )

Yml file

titleserv: "blablabla %{search} blabla"

that's what you're looking for ?

How do you save values into a YAML file?

Using ruby-1.9.3 (Approach may not work in older versions).

I'm assuming the file looks like this (adjust code accordingly):

---
content:
session: 0

and is called /tmp/test.yml

Then the code is just:

require 'yaml' # Built in, no gem required
d = YAML::load_file('/tmp/test.yml') #Load
d['content']['session'] = 2 #Modify
File.open('/tmp/test.yml', 'w') {|f| f.write d.to_yaml } #Store

Load an ActiveRecord models from a YAML file

With the YAML structure you added to your question the below should work, assuming file.yml is the name of you YAML file

require 'yaml' #I think rails does this already for you so it might not be necessary

def load_data_from_yaml
YAML.load_file("file.yml").each { |building| self.class.create!(building) }
end

if the size of your YAML file is huge and you want some speed you could have a look at: https://github.com/bjhaid/active_record_bulk_insert

and you method would look like this:

def load_data_from_yaml
self.class.bulk_insert(YAML.load_file("file.yml"))
end

Refactoring Ruby on Rails i18n YAML files using dictionaries

TLDNR; Don't hack your file format, improve the rails helpers and help to establish a standardized key structure!

TLDR;

Don't want to rain on your parade, but I have a few issues with this technique. The dilemma of where to use the dot shortcut and how the rails helpers' key structure differs can be a bit puzzling.

As I understand it, the question is basically about DRYing up your locale files and using a feature of the YAML language to achieve this.

Firstly, anchors are only really guaranteed to work for YAML so this solution can't be applied generically to I18n. This technique is probably not feasible if you use a different backend. Be it SQL, Redis or Json, I'm not aware of any of them having any kind symlinking functionality. And that's without going too much into the fact that under the hood, the translations are in fact duplicated.

The second and bigger problem that I have is about linguistics. Your example makes the case that all of these terms are exactly equal in context and in meaning. Unfortunately this is only ever the case in extremely simple examples.

Undoubtedly, as your app grows or as you add additional languages, you'll find that a Person's "name" attribute has to be distinct from say a Book's "name" attribute which in English we'll call a "title" - OK, this example is really convoluted ;) but as you mix in more and more languages this situation does occur frequently and ideally, we want a generic way of dealing with it.

I think in large part, the complexity comes from the rails helpers that have evolved with different defaults without there being a convention for key structures.

Going back to your example you mention 2 things that I think are really distinct : activerecord attribute translations which use the rails helpers and view translations which use the dot shortcut.

Let me give you an example of a workflow that is super frequent :

  1. You create a form with the User's "name" field in this situation, you want to use the generic "name" attribute translations (label_tag should use something like :'attributes.name'). This is the simplest, DRYest case to get you up and running quickly, bulk translating simple attributes.
  2. A while later you decide that User's "name" needs to be translated as "full name" for this model only so you create a new translation that has a higher priority in label_tag's lookup call (say :'activerecord.attributes.users.name'))
  3. Later still, the marketing guy has the brilliant idea of displaying this field's label as "enter your funky fresh name" on this page (and only on this page). We're not describing the name attribute anymore, we're describing a particular view of this form; this is where the dot shortcut comes in converting :'.form.name' to something like ':users.new.form.name'.

There is no way we could handle this situation with a shared "dictionary". Sure our locale file would be DRY, but our linguistic/translation concerns are vastly different from our developer concerns here (sadly).

On the plus side, we can get clearer about what kind of content we're describing and reflect that in our key structures and in our tools - that for me is the way forward! :)



Related Topics



Leave a reply



Submit