-
-
Notifications
You must be signed in to change notification settings - Fork 631
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Automatically generate javascript files for react-intl when the serve…
…r starts up.
- Loading branch information
1 parent
6370447
commit fdff007
Showing
5 changed files
with
133 additions
and
0 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
require "erb" | ||
|
||
module ReactOnRails | ||
class LocalesToJs | ||
def initialize | ||
@locale_files = set_locale_files | ||
@default_locale = set_default_locale | ||
@translations, @defaults = generate_translations | ||
end | ||
|
||
def convert | ||
i18n_js_files.each do |f| | ||
template = send("template_#{f}") | ||
path = send("path_#{f}") | ||
create_js_file(template, path) | ||
end | ||
end | ||
|
||
private | ||
|
||
def i18n_js_files | ||
%w("translations_js", "default_js") | ||
end | ||
|
||
def create_js_file(template, path) | ||
result = ERB.new(template).result() | ||
File.open(path, "w") do |f| | ||
f.write(result) | ||
end | ||
end | ||
|
||
def generate_translations | ||
translations = {} | ||
defaults = {} | ||
@locale_files.each do |f| | ||
translation = YAML.load(File.open(f)) | ||
key = translation.keys[0] | ||
val = flatten(translation[key]) | ||
translations = translations.deep_merge(key => val) | ||
defaults = defaults.deep_merge(flatten_defaults(val)) if key == @default_locale | ||
end | ||
translations.to_json, defaults.to_json | ||
end | ||
|
||
def format(input) | ||
input.to_s.tr(".", "_").camelize(:lower).to_sym | ||
end | ||
|
||
def flatten_defaults(val) | ||
flatten(val).each_with_object({}) do |(k, v), h| | ||
key = format(k) | ||
h[key] = { id: k, defaultMessage: v } | ||
end | ||
end | ||
|
||
def flatten(translations) | ||
translations.each_with_object({}) do |(k, v), h| | ||
if v.is_a? Hash | ||
flatten(v).map { |hk, hv| h["#{k}.#{hk}".to_sym] = hv } | ||
else | ||
h[k] = v | ||
end | ||
end | ||
end | ||
|
||
def i18n_dir | ||
ReactOnRails.configuration.i18n_dir | ||
end | ||
|
||
def set_locale_files | ||
ReactOnRails.configuration.rails_locales_path | ||
end | ||
|
||
def set_default_locale | ||
ReactOnRails.configuration.default_locale || "en" | ||
end | ||
|
||
def path_translations_js | ||
i18n_dir + "translations.js" | ||
end | ||
|
||
def path_default_js | ||
i18n_dir + "default.js" | ||
end | ||
|
||
def template_translations_js | ||
<<-JS | ||
export const translations = #{@translations}; | ||
JS | ||
end | ||
|
||
def template_default_js | ||
<<-JS | ||
import { defineMessages } from 'react-intl'; | ||
const defaultLocale = \'#{@default_locale}\'; | ||
const defaultMessages = defineMessages(#{@defaults}); | ||
export { defaultMessages, defaultLocale }; | ||
JS | ||
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