-
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.
Implement index, show action, use hashes
- Loading branch information
1 parent
3a79ee8
commit 12f4e56
Showing
3 changed files
with
58 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
require 'cgi' | ||
require 'digest' | ||
|
||
module Moirai | ||
class TranslationFilesController < ApplicationController | ||
def index | ||
i18n_file_paths = I18n.load_path | ||
@file_paths = i18n_file_paths.select { |path| (path.start_with? Rails.root.to_s) && path.end_with?(".yml", ".yaml") } | ||
@file_hashes = @file_paths.map { |path| [Digest::SHA256.hexdigest(path), path] }.to_h | ||
end | ||
|
||
def show | ||
i18n_file_paths = I18n.load_path | ||
@file_paths = i18n_file_paths.select { |path| (path.start_with? Rails.root.to_s) && path.end_with?(".yml", ".yaml") } | ||
@file_hashes = @file_paths.map { |path| [Digest::SHA256.hexdigest(path), path] }.to_h | ||
file_path = @file_hashes[params[:id]] | ||
decoded_path = CGI.unescape(file_path) | ||
@translations = parse_file(decoded_path) | ||
render json: @translations | ||
end | ||
|
||
private | ||
|
||
def parse_file(path) | ||
yaml_content = YAML.load_file(path) | ||
root_key = yaml_content.keys.first | ||
flatten_hash(yaml_content[root_key]) | ||
end | ||
|
||
def flatten_hash(hash, parent_key = "", result = {}) | ||
hash.each do |key, value| | ||
new_key = parent_key.empty? ? key.to_s : "#{parent_key}.#{key}" | ||
case value | ||
when Hash | ||
flatten_hash(value, new_key, result) | ||
when Array | ||
value.each_with_index do |item, index| | ||
array_key = "#{new_key}.#{index}" | ||
if item.is_a?(Hash) | ||
flatten_hash(item, array_key, result) | ||
else | ||
result[array_key] = item | ||
end | ||
end | ||
else | ||
result[new_key] = value | ||
end | ||
end | ||
result | ||
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,4 @@ | ||
<% @file_paths.each do |file| %> | ||
<% file_hash = Digest::SHA256.hexdigest(file) %> | ||
<%= link_to File.basename(file), translation_file_path(file_hash) %> | ||
<% 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 |
---|---|---|
|
@@ -2,4 +2,6 @@ | |
|
||
Moirai::Engine.routes.draw do | ||
root to: "pages#index" | ||
|
||
resources :translation_files, only: %i[index show] | ||
end |