Module: I18n::Tasks::Data::Adapter::YamlAdapter

Defined in:
lib/i18n/tasks/data/adapter/yaml_adapter.rb

Constant Summary collapse

EMOJI_REGEX =
/\\u[\da-f]{8}/i.freeze
TRAILING_SPACE_REGEX =
/ $/.freeze

Class Method Summary collapse

Class Method Details

.dump(tree, options) ⇒ String

Returns:

  • (String)


23
24
25
# File 'lib/i18n/tasks/data/adapter/yaml_adapter.rb', line 23

def dump(tree, options)
  strip_trailing_spaces(restore_emojis(tree.to_yaml(options || {})))
end

.parse(str, options) ⇒ Hash

Returns locale tree.

Returns:

  • (Hash)

    locale tree



13
14
15
16
17
18
19
20
# File 'lib/i18n/tasks/data/adapter/yaml_adapter.rb', line 13

def parse(str, options)
  if YAML.method(:load).arity.abs == 2
    YAML.safe_load(str, **(options || {}), permitted_classes: [Symbol], aliases: true)
  else
    # older jruby and rbx 2.2.7 do not accept options
    YAML.load(str)
  end
end

.restore_emojis(yaml) ⇒ String

Returns:

  • (String)


28
29
30
# File 'lib/i18n/tasks/data/adapter/yaml_adapter.rb', line 28

def restore_emojis(yaml)
  yaml.gsub(EMOJI_REGEX) { |m| [m[-8..].to_i(16)].pack('U') }
end

.strip_trailing_spaces(yaml) ⇒ String

Returns:

  • (String)


33
34
35
# File 'lib/i18n/tasks/data/adapter/yaml_adapter.rb', line 33

def strip_trailing_spaces(yaml)
  yaml.gsub(TRAILING_SPACE_REGEX, '')
end