Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add suffix option #561

Merged
merged 2 commits into from
Feb 14, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
6 changes: 5 additions & 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 << footer
EddieOne marked this conversation as resolved.
Show resolved Hide resolved
end

protected
Expand All @@ -19,6 +19,10 @@ def header
text + %(#{@namespace}.translations || (#{@namespace}.translations = {});\n)
end

def footer
@suffix || ''
end

def line(locale, translations)
if @js_extend
%(#{@namespace}.translations["#{locale}"] = I18n.extend((#{@namespace}.translations["#{locale}"] || {}), #{translations});\n)
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
192 changes: 192 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"i18n"
],
"devDependencies": {
"jasmine-node": "^1.14.5"
"jasmine-node": "^1.16.2"
EddieOne marked this conversation as resolved.
Show resolved Hide resolved
},
"main": "app/assets/javascripts/i18n.js",
"scripts": {
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