Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

Ruby gemを使ってYAMLをソートする

More than 1 year has passed since last update.

YAMLをソートするには

以下のgemを使う。
redealumni/i18n_yaml_sorter

使い方

ここではテキトーな作業フォルダを作ってそこでソートしてみる。

Install

bundlerを使う方法(単にgem installでも可)

$ bundle init
---
Gemfileに以下を追加
  gem "i18n_yaml_sorter"
---
$ bundle install --path vendor/bundle

実行

bundle install出来たら、sort_yamlというコマンドが使えるようになっているので、
以下のコマンドでYAMLファイルをソートする。

$ bundle exec sort_yaml < #{入力YAMLファイル} > #{出力先ファイル} 
example:
$ bundle exec sort_yaml < ja.yml > ja_out.yml

実行結果

before

en-US:
  # Note that our comments are important:
  # Don't let your yaml sorter delete them!
  bananas: |
    Bananas are "nice":
      - They are <b> sweet </b>.
      this: not a key  
    That is why everyone like bananas!
  grapes: We eat them.
  apples: >
    Apples are fine,
    just don't eat your 
    iPods!
  grapes: We dont' eat them.

after

en-US:
  # Note that our comments are important:
  # Don't let your yaml sorter delete them!
  apples: >
    Apples are fine,
    just don't eat your
    iPods!
  bananas: |
    Bananas are "nice":
      - They are <b> sweet </b>.
      this: not a key
    That is why everyone like bananas!
  grapes: We dont' eat them.
  grapes: We eat them.
  • コメントはそのままの場所に残るっぽい。
  • キーが重複していると、値でソートするっぽい。

環境情報

意図せずこのバージョンだった。これじゃないと動かないとかではない。

$ rbenv -v
rbenv 1.1.1

$ ruby -v
ruby 2.3.3p222 (2016-11-21 revision 56859) [x86_64-darwin18]

$ bundle -v
Bundler version 1.17.1
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away