forked from hpyhacking/peatio
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request hpyhacking#327 from peatio/client_i18n
Client i18n
- Loading branch information
Showing
13 changed files
with
888 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
//= require i18n | ||
<%= JsLocaleHelper.output_locale(:en) %> | ||
|
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,3 @@ | ||
//= require i18n | ||
<%= JsLocaleHelper.output_locale(:'zh-CN') %> | ||
|
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
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,3 @@ | ||
js: | ||
en: | ||
brand: Peatio |
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,3 @@ | ||
js: | ||
zh-CN: | ||
brand: 貔貅 |
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,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 |
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,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 |
Oops, something went wrong.