-
-
Notifications
You must be signed in to change notification settings - Fork 521
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
json_only option should allow multiple locales in one file (#531)
* - `json_only` option should allow multiple locales. - Refactor segment writer code to use formatter classes. This can be extended in the future if needed to support other formats easily. * RSpecs: should --> expect
- Loading branch information
1 parent
c1b1165
commit 5786e88
Showing
8 changed files
with
130 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
module I18n | ||
module JS | ||
module Formatters | ||
class Base | ||
def initialize(js_extend: false, namespace: nil, pretty_print: false) | ||
@js_extend = js_extend | ||
@namespace = namespace | ||
@pretty_print = pretty_print | ||
end | ||
|
||
protected | ||
|
||
def format_json(translations) | ||
if @pretty_print | ||
::JSON.pretty_generate(translations) | ||
else | ||
translations.to_json | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
require "i18n/js/formatters/base" | ||
|
||
module I18n | ||
module JS | ||
module Formatters | ||
class JS < Base | ||
def format(translations) | ||
contents = header | ||
translations.each do |locale, translations_for_locale| | ||
contents << line(locale, format_json(translations_for_locale)) | ||
end | ||
contents | ||
end | ||
|
||
protected | ||
|
||
def header | ||
%(#{@namespace}.translations || (#{@namespace}.translations = {});\n) | ||
end | ||
|
||
def line(locale, translations) | ||
if @js_extend | ||
%(#{@namespace}.translations["#{locale}"] = I18n.extend((#{@namespace}.translations["#{locale}"] || {}), #{translations});\n) | ||
else | ||
%(#{@namespace}.translations["#{locale}"] = #{translations};\n) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
require "i18n/js/formatters/base" | ||
|
||
module I18n | ||
module JS | ||
module Formatters | ||
class JSON < Base | ||
def format(translations) | ||
format_json(translations) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters