Skip to content

Commit 9949d75

Browse files
committed
Move translation files into database
1 parent 8bc15e0 commit 9949d75

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

app/controllers/pages_controller.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,10 @@ def javascript_i18n
7777
# filepath = Rails.root.join('public', 'i18n', 'javascript', "#{locale.to_sym}.js")
7878
# File.read(filepath)
7979

80+
data = Localization::Translation.where(locale: :en).joins(:original).
81+
where('localization_originals.type': :website_client_side).
82+
pluck(:key, :value).to_h
8083
# TOOD: Pivot on the params[:locale]
81-
render json: {
82-
'diggingDeeper.editViaGitHub': 'Edit via GitHub',
83-
'diggingDeeper.linkOpensInNewTab': 'The link opens in a new window or tab'
84-
}
84+
render json: data
8585
end
8686
end

scripts/load_js_i18n_into_db.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# frozen_string_literal: true
2+
3+
require "json"
4+
5+
# Directory containing your TS files
6+
DIR = Rails.root.join("app/javascript/i18n/en")
7+
8+
PAIR_REGEX = /
9+
['"](?<key>[^'"]+)['"] # key
10+
\s*:\s*
11+
(?<val>
12+
"(?:[^"\\]|\\.)*" # double-quoted string
13+
|
14+
'(?:[^'\\]|\\.)*' # single-quoted string
15+
)
16+
/x
17+
18+
19+
def import_file(file_path)
20+
puts "📂 Importing #{file_path}"
21+
content = File.read(file_path)
22+
23+
content.scan(PAIR_REGEX) do |key, raw_val|
24+
value = raw_val[1..-2] # strip surrounding quotes
25+
value = value.gsub("\\'", "'").gsub('\\"', '"')
26+
27+
Localization::Original::Create.(:website_client_side, key, value, nil)
28+
# puts " → #{key} = #{value}"
29+
rescue ActiveRecord::RecordNotUnique
30+
# Skip
31+
rescue ActiveRecord::RecordInvalid => e
32+
puts "⚠️ Failed to import #{key}: #{e.message}"
33+
end
34+
end
35+
36+
Dir.glob(DIR.join("*.ts")).each do |file|
37+
import_file(file)
38+
end

0 commit comments

Comments
 (0)