GithubHelp home page GithubHelp logo

yaml_to_json_kata's Introduction

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

yaml_to_json_kata's People

Contributors

williampollet avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

nicozf fabienneb

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.