burek 0.5.0 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f15c99f8e22ba3d89f43a1cdc810a3662e2cf757
4
- data.tar.gz: 096c96cc33e8ef68e7e67586f6e1a5617099eedd
3
+ metadata.gz: 4653799d775ca0a0ae0945f48b5a282e526d5120
4
+ data.tar.gz: 5f7e5d7b68e8ca25f7157e510bec70e106d46252
5
5
  SHA512:
6
- metadata.gz: a42b817c06466e4a478d7fbb91d3982ad328079d6fed112d75711c0768b8b1d3a12cd0d497ffd1473e7e55ddeaa8a139e9d5ea0b996c2345617c6c17c4036cf2
7
- data.tar.gz: f5effbc7d4bf84306739b203b7326f914e7f9e98fedf4318e811e96c68f661af3fbdc22456b4982e3165def0876a991d29df8d81c27a64d93c5f8f542c051e98
6
+ metadata.gz: f471eb47e0b74b6080ff164604dfab4bd3a39931fa12911046d02dd0c2ec1c4bdbbd861af1944640ed87a0e6fa0652e81a5fc0f9cb8c6f836617cc85a73d4a45
7
+ data.tar.gz: 0112684fb1d79e5dbeb9028ab97db82dc2d25dca3fbe05df68451865e21ece05fc8d9ae613f511035f1d569226cf749e7f79d856f1933b093590d1df8eceb432
data/lib/config.rb ADDED
@@ -0,0 +1,20 @@
1
+ module Burek
2
+
3
+ @@config_hash = {
4
+ search_folders: ['./app/views/**/*'],
5
+ translations_path: './config/locales/burek/',
6
+ translation_placeholder: 'TODO',
7
+ ignore_folders_for_key: ['.','app'],
8
+ subfolder_depth: 2,
9
+ locales: ['en','fi']
10
+ }
11
+
12
+ def self.set_config(key, value)
13
+ @@config_hash[key] = value
14
+ end
15
+
16
+ def self.config(key)
17
+ raise 'Unknown config key!' unless @@config_hash.has_key? key
18
+ @@config_hash[key]
19
+ end
20
+ end
data/lib/core.rb ADDED
@@ -0,0 +1,122 @@
1
+ require 'config'
2
+ require 'yaml'
3
+
4
+ module Burek
5
+
6
+ module Core
7
+
8
+ def self.key_to_file_path(key, locale)
9
+ path_parts = key.split("/")
10
+ path_parts.pop
11
+
12
+ translation_file = Burek.config(:translations_path)
13
+ path_parts.each_with_index do |item, index|
14
+ if index == Burek.config(:subfolder_depth) || item == path_parts.last
15
+ translation_file += "#{item}.#{locale}.yml"
16
+ break
17
+ else
18
+ translation_file += "/#{item}/"
19
+ unless File.directory?(translation_file)
20
+ Dir.mkdir(translation_file)
21
+ end
22
+ end
23
+ end
24
+ return translation_file
25
+ end
26
+
27
+ def self.filter_path(file_name)
28
+ path = file_name.split('/')
29
+ path.delete_if do |item|
30
+ Burek.config(:ignore_folders_for_key).include? item
31
+ end
32
+ path.last.gsub!(/\.(.*?)$/,'').gsub!(/^_/,'') #strip extenison from file name
33
+ return path.join('/')
34
+ end
35
+
36
+ def self.run_burek
37
+ # Create translations folder if missing
38
+ unless File.directory?(Burek.config(:translations_path))
39
+ Dir.mkdir(Burek.config(:translations_path))
40
+ end
41
+
42
+ new_translations = {}
43
+ # Iterate all defined subfolders subfolders
44
+ for_each_file do |file_name|
45
+ File.open(file_name, "rb") do |file|
46
+ contents = file.read
47
+
48
+ filtered_path = Burek::Core.filter_path(file_name)
49
+ matches = Burek::Parser.find_burek_calls(contents)
50
+ matches.each do |value|
51
+ key = filtered_path + "/" + value.downcase.gsub(' ','_')
52
+ new_translations[key] = value
53
+ end
54
+ end
55
+ end
56
+
57
+ to_replace = {}
58
+ # Create files for each locale
59
+ Burek.config(:locales).each do |locale|
60
+ new_translations.each do |key,value|
61
+ path_parts = key.split("/")
62
+ item_name = path_parts.pop
63
+ path_parts_no_filename = path_parts[0..-2]
64
+
65
+ # Figure out file path
66
+ translation_file = Burek::Core.key_to_file_path(key, locale)
67
+
68
+ # Initialize translations hash
69
+ if File.exists?(translation_file)
70
+ translations_hash = YAML::load_file(translation_file) #Load
71
+ else
72
+ translations_hash = {}
73
+ translations_hash[locale.dup.force_encoding("UTF-8")] = {}
74
+ end
75
+
76
+ # Save info for replacing burek calls with translation calls
77
+ regular_translation_key = path_parts_no_filename.join('.') + ".#{item_name}"
78
+ to_replace[value] = regular_translation_key
79
+
80
+ # Nest in hashes
81
+ cur_hash = translations_hash[locale.dup.force_encoding("UTF-8")]
82
+ path_parts_no_filename.each do |item|
83
+ cur_hash[item] = {} unless cur_hash.has_key?(item)
84
+ cur_hash = cur_hash[item]
85
+ end
86
+ cur_hash[item_name] = ( locale == Burek.config(:locales).first ? value.dup.force_encoding("UTF-8") : Burek.config(:translation_placeholder) )
87
+
88
+ # Save to file
89
+ clean_yaml = Burek::Parser.yaml_to_i18n_file(translations_hash.to_yaml)
90
+ translation_file.gsub!("//","/")
91
+ File.write(translation_file, clean_yaml) #Store
92
+ end
93
+
94
+
95
+ end
96
+
97
+ # Replace all burek calls with regular translation calls
98
+ for_each_file do |file_name|
99
+ File.open(file_name, 'r') do |file|
100
+ content = file.read
101
+ processed_content = Burek::Parser.replace_burek_calls(content, to_replace)
102
+ unless processed_content.nil?
103
+ File.open(file_name, 'w') do |output_file|
104
+ output_file.print processed_content
105
+ end
106
+ end
107
+ end
108
+ end
109
+ end
110
+
111
+ def self.for_each_file
112
+ Burek.config(:search_folders).each do |folder|
113
+ Dir.glob(folder) do |file_name|
114
+ unless File.directory?(file_name)
115
+ yield file_name
116
+ end
117
+ end
118
+ end
119
+ end
120
+
121
+ end
122
+ end
data/lib/parser.rb ADDED
@@ -0,0 +1,33 @@
1
+ module Burek
2
+
3
+ module Parser
4
+
5
+ def self.find_burek_calls(string)
6
+ string.scan(burek_call_regex).flatten
7
+ end
8
+
9
+ def self.replace_burek_calls(contents, to_replace)
10
+ matches = find_burek_calls(contents)
11
+ changes_made = false
12
+ matches.each do |value|
13
+ if to_replace.has_key?(value)
14
+ contents.gsub!(burek_call_regex(value)," t('#{to_replace[value]}')")
15
+ changes_made = true
16
+ end
17
+ end
18
+
19
+ return contents if changes_made
20
+ end
21
+
22
+ def self.yaml_to_i18n_file(yaml)
23
+ yaml.lines.to_a[1..-1].join
24
+ end
25
+
26
+ private
27
+
28
+ def self.burek_call_regex(caption="(?<key>[^\\)]*)")
29
+ Regexp.new("[^a-zA-Z0-9_]burek[ \\t]*\\([ \\t]*('|\")#{caption}\('|\")[^\\)]*\\)")
30
+ end
31
+
32
+ end
33
+ end
@@ -0,0 +1,9 @@
1
+ module Burek
2
+
3
+ module ViewHelpers
4
+ def burek(key)
5
+ raw "<span class='translation_missing'>#{key}</span>"
6
+ end
7
+ end
8
+
9
+ end
data/tasks/tasks.rb ADDED
@@ -0,0 +1,14 @@
1
+ require 'yaml'
2
+ require 'parser'
3
+ require 'core'
4
+ require 'config'
5
+
6
+ namespace :burek do
7
+
8
+ desc "Task passes through all views and reports any missing translations"
9
+ task :fetch do
10
+ Burek::core.run_burek
11
+ end
12
+
13
+ end
14
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: burek
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Filip Defar
@@ -17,6 +17,11 @@ extensions: []
17
17
  extra_rdoc_files: []
18
18
  files:
19
19
  - lib/burek.rb
20
+ - lib/config.rb
21
+ - lib/core.rb
22
+ - lib/parser.rb
23
+ - lib/view_helpers.rb
24
+ - tasks/tasks.rb
20
25
  homepage: https://github.com/dabrorius/burek
21
26
  licenses:
22
27
  - MIT