Skip to content

Commit

Permalink
Support JSON as a translation file format.
Browse files Browse the repository at this point in the history
  • Loading branch information
BoboFraggins committed Sep 7, 2018
1 parent 85b1796 commit ebcd95a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ pkg
.bundle
.rvmrc
.ruby-version
.ruby-gemset
Gemfile.lock
gemfiles/*.lock
13 changes: 12 additions & 1 deletion lib/i18n/backend/base.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require 'yaml'
require 'json'
require 'i18n/core_ext/hash'
require 'i18n/core_ext/kernel/suppress_warnings'

Expand All @@ -10,7 +11,7 @@ module Base
include I18n::Backend::Transliterator

# Accepts a list of paths to translation files. Loads translations from
# plain Ruby (*.rb) or YAML files (*.yml). See #load_rb and #load_yml
# plain Ruby (*.rb), YAML files (*.yml), or JSON files (*.json). See #load_rb, #load_yml, and #load_json
# for details.
def load_translations(*filenames)
filenames = I18n.load_path if filenames.empty?
Expand Down Expand Up @@ -236,6 +237,16 @@ def load_yml(filename)
end
alias_method :load_yaml, :load_yml

# Loads a JSON translations file. The data must have locales as
# toplevel keys.
def load_json(filename)
begin
JSON.parse(File.read(filename))
rescue TypeError, StandardError => e
raise InvalidLocaleData.new(filename, e.inspect)
end
end

def translate_localization_format(locale, object, format, options)
format.to_s.gsub(/%[aAbBpP]/) do |match|
case match
Expand Down
9 changes: 9 additions & 0 deletions test/backend/simple_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ def setup
assert_nothing_raised { I18n.backend.load_translations("#{locales_dir}/en.yaml") }
end

test "simple load_translations: given a JSON file name with yaml extension does not raise anything" do
assert_nothing_raised { I18n.backend.load_translations("#{locales_dir}/en.json") }
end

test "simple load_translations: given a Ruby file name it does not raise anything" do
assert_nothing_raised { I18n.backend.load_translations("#{locales_dir}/en.rb") }
end
Expand All @@ -40,6 +44,11 @@ def setup
assert_equal({ 'en' => { 'foo' => { 'bar' => 'baz' } } }, data)
end

test "simple load_json: loads data from a JSON file" do
data = I18n.backend.send(:load_yml, "#{locales_dir}/en.json")
assert_equal({ 'en' => { 'foo' => { 'bar' => 'baz' } } }, data)
end

test "simple load_translations: loads data from known file formats" do
I18n.backend = I18n::Backend::Simple.new
I18n.backend.load_translations("#{locales_dir}/en.rb", "#{locales_dir}/en.yml")
Expand Down
7 changes: 7 additions & 0 deletions test/test_data/locales/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"en": {
"foo": {
"bar": "baz"
}
}
}

0 comments on commit ebcd95a

Please sign in to comment.