burek 0.5.5 → 0.5.9
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +77 -3
- data/lib/burek.rb +2 -0
- data/lib/config.rb +35 -9
- data/lib/core/burek_finder.rb +6 -2
- data/lib/core/core.rb +15 -1
- data/lib/core/locales_creator.rb +9 -5
- data/lib/file_helpers.rb +1 -1
- data/tasks/tasks.rb +6 -1
- metadata +1 -1
data/README.md
CHANGED
@@ -1,4 +1,78 @@
|
|
1
|
-
burek
|
2
|
-
|
1
|
+
# burek
|
2
|
+
|
3
|
+
Burek is here to help you with your RoR translations. It's not here to replace rails translation, but to help you manage them. In the end *all* burek calls are replaced with regular translation calls.
|
4
|
+
|
5
|
+
## How it works?
|
6
|
+
|
7
|
+
### 1. Call burek in your view files
|
8
|
+
|
9
|
+
```html
|
10
|
+
# views/users/index.html
|
11
|
+
<h1>
|
12
|
+
<%= burek("All users") %>
|
13
|
+
</h1>
|
14
|
+
```
|
15
|
+
|
16
|
+
### 2. Fetch translations with burek rake task
|
17
|
+
|
18
|
+
```bash
|
19
|
+
rake burek:fetch
|
20
|
+
```
|
21
|
+
|
22
|
+
### 3. Burek generates translation files for you.
|
23
|
+
If you defined, for example, you want to use English and Finnish locales (and English is your main locale).
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
# config/locales/burek/views/users/index.en.yml
|
27
|
+
en:
|
28
|
+
views:
|
29
|
+
users:
|
30
|
+
all_users: All users
|
31
|
+
```
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
# config/locales/burek/views/users/index.fi.yml
|
35
|
+
en:
|
36
|
+
views:
|
37
|
+
users:
|
38
|
+
all_users: TODO
|
39
|
+
```
|
40
|
+
|
41
|
+
It also replaces all burek calls with regular translation calls
|
42
|
+
|
43
|
+
```html
|
44
|
+
# views/users/index.html
|
45
|
+
<h1>
|
46
|
+
<%= t("views.users.all_users") %>
|
47
|
+
</h1>
|
48
|
+
```
|
49
|
+
|
50
|
+
That's it!
|
51
|
+
|
52
|
+
## How to install it?
|
53
|
+
|
54
|
+
Just add following line to your Gemfile:
|
55
|
+
```ruby
|
56
|
+
gem "burek"
|
57
|
+
```
|
58
|
+
## How to configure it?
|
59
|
+
You can use default configuration, if you want to override it create following file. You don't have to override all configuration variables, but I listed them all here with brief descriptions.
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
# config/initializers/burek.rb
|
63
|
+
Burek.setup do |config|
|
64
|
+
config.search_folders = ['./app/views/**/*'] # Where should I look for burek calls?
|
65
|
+
config.translations_path = './config/locales/burek/' # Where should I generate translation files?
|
66
|
+
config.translation_placeholder = 'TODO' # What should I set as default translation for non-main languages
|
67
|
+
config.locales = ['en'] # What locales do you want to use? (NOTE: First locale is considered main)
|
68
|
+
|
69
|
+
# NOTE: Burek generates your translation keys depending on file path where burek call was found.
|
70
|
+
config.ignore_folders_for_key = ['.','app'] # What folders should be ignored when generating translation key
|
71
|
+
|
72
|
+
# NOTE: When generating locale files they are nested in subfolders that are generated from translation key
|
73
|
+
config.subfolder_depth = 2 # How deep should I nest translation files in subfolders?
|
74
|
+
end
|
75
|
+
```
|
76
|
+
|
77
|
+
|
3
78
|
|
4
|
-
A ruby/rails gem for managing translations easy way.
|
data/lib/burek.rb
CHANGED
data/lib/config.rb
CHANGED
@@ -1,21 +1,47 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
|
1
3
|
module Burek
|
2
4
|
|
3
|
-
|
5
|
+
class BurekConfiguration
|
6
|
+
def initialize(hash)
|
7
|
+
@hash = hash
|
8
|
+
end
|
9
|
+
|
10
|
+
def hash
|
11
|
+
@hash
|
12
|
+
end
|
13
|
+
|
14
|
+
def get(key)
|
15
|
+
hash[key]
|
16
|
+
end
|
17
|
+
|
18
|
+
def method_missing(m, *args, &block)
|
19
|
+
key = m.to_s.gsub('=','').to_sym
|
20
|
+
raise 'Unknown config key!' unless @hash.has_key? key
|
21
|
+
@hash[key] = args.first
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
@@configuration = BurekConfiguration.new({
|
4
26
|
search_folders: ['./app/views/**/*'],
|
5
27
|
translations_path: './config/locales/burek/',
|
6
28
|
translation_placeholder: 'TODO',
|
7
29
|
ignore_folders_for_key: ['.','app'],
|
8
30
|
subfolder_depth: 2,
|
9
|
-
locales: ['en'
|
10
|
-
}
|
31
|
+
locales: ['en']
|
32
|
+
})
|
11
33
|
|
12
|
-
def self.
|
13
|
-
@@
|
34
|
+
def self.setup
|
35
|
+
yield @@configuration
|
14
36
|
end
|
15
|
-
|
16
|
-
def self.
|
17
|
-
|
18
|
-
|
37
|
+
|
38
|
+
def self.configuration
|
39
|
+
@@configuration
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.config
|
43
|
+
configuration
|
19
44
|
end
|
20
45
|
|
21
46
|
end
|
47
|
+
|
data/lib/core/burek_finder.rb
CHANGED
@@ -15,13 +15,17 @@ module Burek
|
|
15
15
|
filtered_path = filter_path(file_name)
|
16
16
|
matches = find_burek_calls(contents)
|
17
17
|
matches.each do |caption|
|
18
|
-
key = filtered_path + "/" + caption
|
18
|
+
key = filtered_path + "/" + caption_to_key_part(caption)
|
19
19
|
new_translations[key] = caption
|
20
20
|
end
|
21
21
|
end
|
22
22
|
return new_translations
|
23
23
|
end
|
24
24
|
|
25
|
+
# Sanitizes caption so that it can be used as key part
|
26
|
+
def self.caption_to_key_part(caption)
|
27
|
+
caption.strip.gsub(/ +/,' ').downcase.gsub(/[^0-9a-z_ ]/i, '').split(' ')[0..3].join('_')
|
28
|
+
end
|
25
29
|
|
26
30
|
# Finds burek calls in a string
|
27
31
|
#
|
@@ -46,7 +50,7 @@ module Burek
|
|
46
50
|
def self.filter_path(file_name)
|
47
51
|
path = file_name.split('/')
|
48
52
|
path.delete_if do |item|
|
49
|
-
Burek.config(:ignore_folders_for_key).include? item
|
53
|
+
Burek.config.get(:ignore_folders_for_key).include? item
|
50
54
|
end
|
51
55
|
path.last.gsub!(/\.(.*?)$/,'').gsub!(/^_/,'') #strip extenison from file name
|
52
56
|
return path.join('/')
|
data/lib/core/core.rb
CHANGED
@@ -16,11 +16,25 @@ module Burek
|
|
16
16
|
# 3. replace all burek calls with regular translation calls
|
17
17
|
#
|
18
18
|
def self.run_burek
|
19
|
-
Burek::FileHelpers.create_folder_if_missing Burek.config(:translations_path)
|
19
|
+
Burek::FileHelpers.create_folder_if_missing Burek.config.get(:translations_path)
|
20
20
|
|
21
|
+
puts "Searching for burek calls..."
|
21
22
|
new_translations = Burek::Finder.find_burek_calls_in_files
|
23
|
+
if new_translations.any?
|
24
|
+
new_translations.each do |file, caption|
|
25
|
+
puts "\t-> Found '#{caption}' in '#{file}'"
|
26
|
+
end
|
27
|
+
else
|
28
|
+
puts "No burek calls found!"
|
29
|
+
end
|
30
|
+
|
31
|
+
puts "Adding translations to locale filess..."
|
22
32
|
to_replace = Burek::LocalesCreator.create_locales(new_translations)
|
33
|
+
|
34
|
+
puts "Repalcing burek calls with translation calls..."
|
23
35
|
Burek::Replacer.replace_burek_calls_in_files(to_replace)
|
36
|
+
|
37
|
+
puts "DONE!"
|
24
38
|
end
|
25
39
|
|
26
40
|
# A regex for finiding burek calls
|
data/lib/core/locales_creator.rb
CHANGED
@@ -14,7 +14,7 @@ module Burek
|
|
14
14
|
def self.create_locales(new_translations)
|
15
15
|
to_replace = {}
|
16
16
|
# Create files for each locale
|
17
|
-
Burek.config(:locales).each do |locale|
|
17
|
+
Burek.config.get(:locales).each do |locale|
|
18
18
|
new_translations.each do |key,value|
|
19
19
|
path_parts = key.split("/")
|
20
20
|
item_name = path_parts.pop
|
@@ -33,7 +33,7 @@ module Burek
|
|
33
33
|
cur_hash[item] = {} unless cur_hash.has_key?(item)
|
34
34
|
cur_hash = cur_hash[item]
|
35
35
|
end
|
36
|
-
cur_hash[item_name] = ( locale == Burek.config(:locales).first ? value : Burek.config(:translation_placeholder) )
|
36
|
+
cur_hash[string_to_key(item_name)] = ( locale == Burek.config.get(:locales).first ? value : Burek.config.get(:translation_placeholder) )
|
37
37
|
|
38
38
|
# Save to file
|
39
39
|
Burek::LocalesCreator.translations_hash_to_file(translations_hash, translation_file)
|
@@ -72,12 +72,16 @@ module Burek
|
|
72
72
|
return translations_hash
|
73
73
|
end
|
74
74
|
|
75
|
+
def self.string_to_key(string)
|
76
|
+
string.split(' ')[0..3].join('_').downcase.gsub(/[^0-9a-z_]/i, '')
|
77
|
+
end
|
78
|
+
|
75
79
|
# Creates a translation call key from path where burek call was found and burek call caption
|
76
80
|
#
|
77
81
|
def self.path_parts_to_key(path_parts, item_name)
|
78
82
|
regular_translation_key = path_parts.join('.')
|
79
83
|
regular_translation_key += "." unless regular_translation_key.nil? || regular_translation_key.empty?
|
80
|
-
regular_translation_key += "#{item_name}"
|
84
|
+
regular_translation_key += "#{string_to_key(item_name)}"
|
81
85
|
end
|
82
86
|
|
83
87
|
# Generates a file path to locales file from a file path where burek call was found.
|
@@ -97,9 +101,9 @@ module Burek
|
|
97
101
|
path_parts = key.split("/")
|
98
102
|
path_parts.pop
|
99
103
|
|
100
|
-
translation_file = Burek.config(:translations_path)
|
104
|
+
translation_file = Burek.config.get(:translations_path)
|
101
105
|
path_parts.each_with_index do |item, index|
|
102
|
-
if index == Burek.config(:subfolder_depth) || item == path_parts.last
|
106
|
+
if index == Burek.config.get(:subfolder_depth) || item == path_parts.last
|
103
107
|
translation_file += "#{item}.#{locale}.yml"
|
104
108
|
break
|
105
109
|
else
|
data/lib/file_helpers.rb
CHANGED
data/tasks/tasks.rb
CHANGED
@@ -5,9 +5,14 @@ require 'config'
|
|
5
5
|
namespace :burek do
|
6
6
|
|
7
7
|
desc "Task passes through all views and reports any missing translations"
|
8
|
-
task :fetch do
|
8
|
+
task :fetch => :environment do
|
9
9
|
Burek::Core.run_burek
|
10
10
|
end
|
11
|
+
|
12
|
+
desc "Show loaded configuration"
|
13
|
+
task :show_config => :environment do
|
14
|
+
puts Burek.configuration.hash
|
15
|
+
end
|
11
16
|
|
12
17
|
end
|
13
18
|
|