LoveWithFood Tech

LWF

Five Steps to Organize Your Translations by Resources

| Comments

Recently, we noticed that our translations were getting disorganized as more categories and subcategories were added like below.

1
2
3
4
5
6
7
8
9
10
11
12
13
# config/locales/en.en.yml
en:
  coupons:

# config/locales/activerecord.en.yml
en:
  activerecord:
    errors:
      models:
        coupons:

# config/locales/simple_form.en.yml
en:

As more resources (models, service objects) were being added, it was getting time consuming to keep track of all our translations. Furthermore, we were running the inherent risk of duplicating translations with a large translation file. Our solution then was to organize the translations by splitting each resource to its own translation file. In the next five steps, we will show you how you can reorganize your translation file making sure no translation is missing.

Step 1 – Back up your translations

Duplicate config/locales as config/locales_new

Step 2 – Slave through the sorting of your translations

1
2
3
4
5
6
7
8
9
10
11
12
# config/locales_new/coupons.en.yml
en:
  coupons:

  activerecord:
    errors:
      models:
        coupons:

  simple_form:
    labels:
      coupon

Well… We can’t help you with this one. You may also want to sort them in alphabetical order.

1
2
3
4
5
6
en:
  activerecord:

  admin:

  simple_form:

Step 3 – Install HashDiff gem

In your Gemfile under group :test, add require 'hashdiff'. You can read more about the gem here.

Step 4 – Convert locale directories to hashes

Add a TranslationsDirectory class under spec/support to convert config/locales and config/locales_new to hashes:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# spec/support/translations_directory.rb
require 'hashdiff'

class TranslationsDirectory
  def initialize(path)
    @path = path
  end

  def to_hash
    result = {}

    Dir.glob("#{@path}/*.yml") do |file|
      if result.empty?
        result = YAML.load(File.read(file))
      else
        result = result.deep_merge(YAML.load(File.read(file)))
      end
    end

    result
  end
end

The method to_hash basically looks through the directory for all files ending with.yml and merges them into a single hash.

Step 5 – Write a test to make sure no translations are missing

Under spec/config:

1
2
3
4
5
6
7
8
9
10
11
# spec/config/locales_spec.rb
require 'spec_helper'

describe 'Difference between old locales and new locales' do
  it 'should be empty' do
    old_trans = TranslationsDirectory.new('config/locales')
    new_trans = TranslationsDirectory.new('config/locales_new')

    expect(HashDiff.diff(new_trans.to_hash, old_trans.to_hash)).to eq([])
  end
end

Once that’s done, run the test and watch it pass! If it doesn’t, HashDiff.diff(new_trans.to_hash, old_trans.to_hash) will generate an array showing duplicated, additional or missing translations.

That’s all from the tech geeks at Love With Food. Let us know what you think by leaving a comment below.

Comments