Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement YAML parsing, create CRUD show and index pages #3

Merged
merged 17 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions app/controllers/moirai/translation_files_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
require "cgi"
require "digest"

module Moirai
class TranslationFilesController < ApplicationController
before_action :load_file_paths, only: [:index, :show]
before_action :generate_file_hashes, only: [:index, :show]

def index
@files = @file_paths.map do |path|
{
id: Digest::SHA256.hexdigest(path),
name: File.basename(path),
path: path
}
end
end

def show
@file_path = @file_hashes[params[:id]]
decoded_path = CGI.unescape(@file_path)
@translation_keys = parse_file(decoded_path)
end

private

def load_file_paths
i18n_file_paths = I18n.load_path
@file_paths = i18n_file_paths.select { |path| path.end_with?(".yml", ".yaml") }
end

def generate_file_hashes
@file_hashes = @file_paths.map { |path| [Digest::SHA256.hexdigest(path), path] }.to_h
end

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
9 changes: 9 additions & 0 deletions app/views/moirai/translation_files/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<h1>Translation files</h1>

<ul>
CuddlyBunion341 marked this conversation as resolved.
Show resolved Hide resolved
<% @files.each do |file| %>
<li>
<%= link_to (file[:path]), translation_file_path(file[:id]) %>
</li>
<% end %>
</ul>
19 changes: 19 additions & 0 deletions app/views/moirai/translation_files/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<h1><%= @file_path %></h1>

<table>
<thead>
<tr>
<td>Key</td>
<td>Value</td>
<td></td>
</tr>
</thead>
<tbody>
<% @translation_keys.each do |key, value| %>
<tr>
<td><%= key %><td>
<td><%= value %><td>
</tr>
<% end %>
</tbody>
</table>
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@

Moirai::Engine.routes.draw do
root to: "pages#index"

resources :translation_files, only: %i[index show]
end
Loading