Skip to content

williampollet/yaml_to_json_kata

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 

Repository files navigation

Yaml to JSON

Context

A Ruby on Rails based company decides to change its translations engine.

The actual engine works with translations stored in yaml files, one file per locale:

# en.yaml
en:
  admin:
    create:
      success: 'admin successfully created'
      failure: 'failure to create the admin'
  project:
    create:
    change_amount:
      success: 'the amount of your project has been modified'

# fr.yaml
fr:
  admin:
    create:
      success: 'administrateur créé avec succès'
      failure: 'administrateur n\'a pas pu être créé'
  project:
    create:
    change_amount:
      success: 'le montant de votre projet a été modifié'
  user:
    notify:
      account_changed: 'votre compte a bien été modifié'

It would like to switch to a JSON storage format, with concatenated keys:

{
  "admin.create.success": {
    "fr": "administrateur créé avec succès",
    "en": "admin successfully created"
  },
  "admin.create.failure": {
    "fr": "administrateur n'a pas pu être créé",
    "en": "failure to create the admin"
  },
  "project.create": {
    "fr": null,
    "en": null
  },
  "project.change_amount.success": {
    "fr": "le montant de votre projet a été modifié",
    "en": "the amount of your project has been modified"
  },
  "user.notify.account_changed": {
    "fr": "votre compte a bien été modifié",
    "en": null
  }
}

This new format repeats several times the locales, but it presents the following advantages:

  • it becomes possible to search for a translation from the unique key
  • the missing translations are quicker to spot
  • as the translations for one key are gathered in one place, it is not necessary to browse several files to translate a value in every languages

Exercise

When the translation file is consequent (several thousands lines), and the number of languages is high, it can be painful to migrate without automation.

Would you be able to write a script to automate the work ? A test driven approach is advised ! You will find a test set with large translations files in the test_set repository

Tips

This section is optional. It presents several technical functions that will help you trough the exercise.

  • you can use hash.dig('key1', 'key2', 'key3') to dig quickly into a deep hash. ref
  • the function hash1.deep_merge(hash2) will allow you to merge two deep hashes without overriding the former value ref. As this is a rails function, to have it work you must monkey-patch Hash with the code here
  • the iterator inject({}) will allow you to iterate over an array and inject the values you want in a resulting hash ref
  • the function YAML.load_file('path/to/file') will allow you to load the content of a yaml file and return it in a corresponding hash ref

Help

This section is optional. It will help you get through the exercise by giving you general advices and directions. ⚠️ It contains spoils regarding a technique to get trough. Unfurl at your own risks ! ⚠️

General advices on the approach:

If you don't know how to begin, consider doing the exercise step by step:
  • first create a small function capable of migrating a simple key, for a simple hash
  • then you can create a function capable of migrating several nested keys, for a more complex hash
  • then you can create a function capable of migrating a full file
  • then you can create a function capable of migrating several files (for several languages)

Algorithms tips:

You are stuck and you would like a tip on the algorithm to implement? A recursive strategy can help. Any other approach is welcome though

Credits

  • Thanks to LaLibertad for providing the translations test set
  • Thanks to sunny for the design of the flat translation format

About

migrating a translation file format from yaml to json

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published