Skip to content

Preserve quote style while changing a YAML file #671

@marselmustafin

Description

@marselmustafin

I have Rails I18n en.yml file which contains values with or without qoutes.

I want to change this file programmatically, but if I use Psych#load and Psych#dump approach it removes all the quotes from values. I don't want to change styling of existing values, how can I do that?

As I understand parsing the ast is the way, but I want to preserve simplicity of working with YAML as the hash and then simply dump it back as the original file but with a few new key/values.

Input:

en:
  account:
    invoices:
      empty: 'It''s empty here'
      tabs:
        donations: Donations
        giftcards: 'Gift Cards'
        membership: Membership
        purchases: Purchases
        subscription: Subscription
      title: 'Invoices History'

Expected result (all styling preserved, new key added):

en:
  account:
    invoices:
      empty: 'It''s empty here'
      tabs:
        donations: Donations
        giftcards: 'Gift Cards'
        membership: Membership
        purchases: Purchases
        subscription: Subscription
        my_new_key: New Value
      title: 'Invoices History'

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions