Skip to content

Commit

Permalink
Merge pull request hpyhacking#327 from peatio/client_i18n
Browse files Browse the repository at this point in the history
Client i18n
  • Loading branch information
poshboytl committed Oct 27, 2014
2 parents 2689be4 + 421166b commit 2ce1fc5
Show file tree
Hide file tree
Showing 13 changed files with 888 additions and 18 deletions.
36 changes: 18 additions & 18 deletions app/assets/javascripts/component_ui/candlestick.js.coffee
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
if gon.local is "zh-CN"
DATETIME_LABEL_FORMAT_FOR_TOOLTIP =
DATETIME_LABEL_FORMAT_FOR_TOOLTIP =
millisecond: ['%m月%e日, %H:%M:%S.%L', '%m月%e日, %H:%M:%S.%L', '-%H:%M:%S.%L']
second: ['%m月%e日, %H:%M:%S', '%m月%e日, %H:%M:%S', '-%H:%M:%S']
minute: ['%m月%e日, %H:%M', '%m月%e日, %H:%M', '-%H:%M']
Expand All @@ -9,7 +9,7 @@ if gon.local is "zh-CN"
month: ['%Y年%m月', '%Y年%m月', '-%m']
year: ['%Y', '%Y', '-%Y']

DATETIME_LABEL_FORMAT =
DATETIME_LABEL_FORMAT =
second: '%H:%M:%S'
minute: '%H:%M'
hour: '%H:%M'
Expand All @@ -18,39 +18,39 @@ DATETIME_LABEL_FORMAT =
month: '%Y-%m'
year: '%Y'

DATE_RANGE =
min1:
DATE_RANGE =
min1:
default_range: 1000 * 3600 * 2 # 2h
dataGrouping_units: [['minute', [1]]]
min5:
min5:
default_range: 1000 * 3600 * 10 # 10h
dataGrouping_units: [['minute', [5]]]
min15:
min15:
default_range: 1000 * 3600 * 24 * 1 # 1d
dataGrouping_units: [['minute', [15]]]
min30:
min30:
default_range: 1000 * 3600 * 24 * 2 # 2d
dataGrouping_units: [['minute', [30]]]
min60:
min60:
default_range: 1000 * 3600 * 24 * 5 # 5d
dataGrouping_units: [['hour', [1]]]
min120:
min120:
default_range: 1000 * 3600 * 24 * 10 # 10d
dataGrouping_units: [['hour', [2]]]
min360:
default_range: 1000 * 3600 * 24 * 30 * 1 # 1m
min360:
default_range: 1000 * 3600 * 24 * 30 * 1 # 1m
dataGrouping_units: [['hour', [6]]]
min1440:
min1440:
default_range: 1000 * 3600 * 24 * 30 * 3 # 3m
dataGrouping_units: [['day', [1]]]
min4320:
min4320:
default_range: 1000 * 3600 * 24 * 30 * 9 # 9m
dataGrouping_units: [['day', [3]]]
min10080:
min10080:
default_range: 1000 * 3600 * 24 * 30 * 12 # 12m
dataGrouping_units: [['day', [7]]]

RANGE_DEFAULT =
RANGE_DEFAULT =
fill: 'none',
stroke: 'none',
'stroke-width': 0,
Expand Down Expand Up @@ -101,7 +101,7 @@ INDICATOR = {MA: false, EMA: false}
for i in [0..1]
if chart.series[i].points.length > 0
tooltips.push chart.series[i].points[chart.series[i].points.length - 1]
chart.tooltip.refresh tooltips
chart.tooltip.refresh tooltips if tooltips.length

@initHighStock = (data) ->
range = DATE_RANGE["min#{data['minutes']}"]['default_range']
Expand Down Expand Up @@ -181,7 +181,7 @@ INDICATOR = {MA: false, EMA: false}
"""
<li><span style='color: {series.color};'>{series.name}: <b>{point.y}</b></span></li>
"""

scrollbar:
buttonArrowColor: '#333'
barBackgroundColor: '#202020'
Expand All @@ -191,7 +191,7 @@ INDICATOR = {MA: false, EMA: false}
buttonBorderColor: '#2a2a2a'
trackBorderColor: '#2a2a2a'

rangeSelector:
rangeSelector:
enabled: false

xAxis:
Expand Down
3 changes: 3 additions & 0 deletions app/assets/javascripts/locales/en.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//= require i18n
<%= JsLocaleHelper.output_locale(:en) %>

3 changes: 3 additions & 0 deletions app/assets/javascripts/locales/zh-CN.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//= require i18n
<%= JsLocaleHelper.output_locale(:'zh-CN') %>

1 change: 1 addition & 0 deletions app/views/layouts/app.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
</script>
<%= stylesheet_link_tag stylesheet, media: "all" %>
<%= javascript_include_tag "emberapp" %>
<%= javascript_include_tag "locales/#{I18n.locale}" %>
<%= yield(:scripts) if content_for?(:scripts) %>
<%= render 'shared/html5' %>
<%= render 'shared/meta' %>
Expand Down
1 change: 1 addition & 0 deletions app/views/layouts/application.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ html lang="en"
- stylesheet = content_for?(:stylesheet) ? yield(:stylesheet) : "application"
= stylesheet_link_tag stylesheet, media: "all"
= javascript_include_tag "application"
= javascript_include_tag "locales/#{I18n.locale}"
= yield(:scripts) if content_for?(:scripts)
= render 'shared/html5'
= render 'shared/meta'
Expand Down
1 change: 1 addition & 0 deletions app/views/layouts/landing.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ html lang="en"
= include_gon
= stylesheet_link_tag "application", :media => "all"
= javascript_include_tag "application"
= javascript_include_tag "locales/#{I18n.locale}"
= render 'shared/html5'
= render 'shared/meta'
= csrf_meta_tags
Expand Down
5 changes: 5 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ class Application < Rails::Application
#config.assets.precompile += ['bootstrap-datetimepicker.css']
config.assets.initialize_on_precompile = true

# Precompile all available locales
Dir.glob("#{config.root}/app/assets/javascripts/locales/*.js.erb").each do |file|
config.assets.precompile << "locales/#{file.match(/([a-z\-A-Z]+\.js)\.erb$/)[1]}"
end

config.generators do |g|
g.orm :active_record
g.template_engine :erb
Expand Down
2 changes: 2 additions & 0 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,7 @@

config.active_record.default_timezone = :local

require 'middleware/i18n_js'
config.middleware.insert_before ActionDispatch::Static, Middleware::I18nJs
config.middleware.insert_before Rack::Runtime, ::SecurityMiddleware
end
3 changes: 3 additions & 0 deletions config/locales/client.en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
js:
en:
brand: Peatio
3 changes: 3 additions & 0 deletions config/locales/client.zh-CN.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
js:
zh-CN:
brand: 貔貅
16 changes: 16 additions & 0 deletions lib/js_locale_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module JsLocaleHelper

def self.load_yaml(locale)
YAML::load(File.open("#{Rails.root}/config/locales/client.#{locale}.yml"))['js']
rescue
{}
end

def self.output_locale(locale=:en)
result = ""
result << "I18n.translations = #{load_yaml(locale).to_json};\n"
result << "I18n.locale = '#{locale}';\n"
result
end

end
32 changes: 32 additions & 0 deletions lib/middleware/i18n_js.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module Middleware
class I18nJs
def initialize(app)
@app = app
end

def call(env)
update_cache(env['REQUEST_PATH']) if matching?(env['REQUEST_PATH'])
@app.call(env)
end

private

def matching?(path)
path =~ /^\/assets\/locales\/[a-z\-A-Z]*\.js/
end

def cache_dir
@cache_dir ||= Rails.root.join("public/assets/locales")
end

def update_cache(path)
locale = path.scan(/[a-z\-A-Z]*\.js/).first.gsub('.js', '')
file_path = "#{cache_dir}/#{locale}.js"

FileUtils.mkdir_p(cache_dir)
File.open(file_path, "w+") do |file|
file << JsLocaleHelper.output_locale(locale)
end
end
end
end
Loading

0 comments on commit 2ce1fc5

Please sign in to comment.