Skip to content

Commit

Permalink
Merge pull request #561 from webgroup-limited/add-suffix-option
Browse files Browse the repository at this point in the history
Add suffix option
  • Loading branch information
PikachuEXE committed Feb 14, 2020
2 parents 5505008 + 7bca360 commit 0d0bc5e
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ MyNamespace.translations["en"] = { ... }

Setting the `prefix: "import I18n from 'i18n-js';\n"` option will add the line at the beggining of the resultant translation file.
This can be useful to use this gem with the [i18n-js](https://www.npmjs.com/package/i18n-js) npm package, which is quite useful to use it with webpack.
The user should provide the semi-colon and the newline character if needed.
The user should provide the semi-colon and the newline character if needed. Note, the suffix option may also be used to wrap translations files.

For example:

Expand Down
3 changes: 2 additions & 1 deletion lib/i18n/js/formatters/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ module I18n
module JS
module Formatters
class Base
def initialize(js_extend: false, namespace: nil, pretty_print: false, prefix: nil)
def initialize(js_extend: false, namespace: nil, pretty_print: false, prefix: nil, suffix: nil)
@js_extend = js_extend
@namespace = namespace
@pretty_print = pretty_print
@prefix = prefix
@suffix = suffix
end

protected
Expand Down
2 changes: 1 addition & 1 deletion lib/i18n/js/formatters/js.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def format(translations)
translations.each do |locale, translations_for_locale|
contents << line(locale, format_json(translations_for_locale))
end
contents
contents << (@suffix || '')
end

protected
Expand Down
7 changes: 5 additions & 2 deletions lib/i18n/js/segment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module JS

# Class which enscapulates a translations hash and outputs a single JSON translation file
class Segment
OPTIONS = [:namespace, :pretty_print, :js_extend, :prefix, :sort_translation_keys, :json_only].freeze
OPTIONS = [:namespace, :pretty_print, :js_extend, :prefix, :suffix, :sort_translation_keys, :json_only].freeze
LOCALE_INTERPOLATOR = /%\{locale\}/

attr_reader *([:file, :translations] | OPTIONS)
Expand All @@ -25,6 +25,7 @@ def initialize(file, translations, options = {})
@pretty_print = !!options[:pretty_print]
@js_extend = options.key?(:js_extend) ? !!options[:js_extend] : true
@prefix = options.key?(:prefix) ? options[:prefix] : nil
@suffix = options.key?(:suffix) ? options[:suffix] : nil
@sort_translation_keys = options.key?(:sort_translation_keys) ? !!options[:sort_translation_keys] : true
@json_only = options.key?(:json_only) ? !!options[:json_only] : false
end
Expand Down Expand Up @@ -70,7 +71,9 @@ def formatter_options
{ js_extend: @js_extend,
namespace: @namespace,
pretty_print: @pretty_print,
prefix: @prefix }
prefix: @prefix,
suffix: @suffix
}
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ translations:
namespace: "Foo"
pretty_print: true
prefix: "import random from 'random-library';\n"
suffix: "//test"
3 changes: 2 additions & 1 deletion spec/ruby/i18n/js_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@
end
end

context "namespace, prefix and pretty_print options" do
context "namespace, prefix, suffix, and pretty_print options" do

before do
stub_const('I18n::JS::DEFAULT_EXPORT_DIR_PATH', temp_path)
Expand Down Expand Up @@ -379,6 +379,7 @@
"foo": "Foo",
"fallback_test": "Success"
};
//test
EOS
}$/)
end
Expand Down

0 comments on commit 0d0bc5e

Please sign in to comment.