burek 0.5.9 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9cff03970830167bb22fb7c2abc148f0de082ebf
4
+ data.tar.gz: f972fc14ae5e046b12395a64ba7762d98cd5d5a1
5
+ SHA512:
6
+ metadata.gz: 1eaccdc6a2d8aff70ad57a7ba9728445692e0ebf251e79afec6aaa4eb4cf3ede1311f047871fff2c3cc8c107cfaf93c4e4a2332f2e0e36daf8c4814c4a859217
7
+ data.tar.gz: 553961b8f66205529f3c7c381a9db769711a3deb5cb1d7c7a1ca99de9fe9411dc17233516afbc4f75f623bf4c61d61da39551960458e3f1804415b8a320f825d
data/README.md CHANGED
@@ -25,17 +25,13 @@ If you defined, for example, you want to use English and Finnish locales (and En
25
25
  ```ruby
26
26
  # config/locales/burek/views/users/index.en.yml
27
27
  en:
28
- views:
29
- users:
30
- all_users: All users
28
+ all_users: All users
31
29
  ```
32
30
 
33
31
  ```ruby
34
32
  # config/locales/burek/views/users/index.fi.yml
35
33
  en:
36
- views:
37
- users:
38
- all_users: TODO
34
+ all_users: TODO
39
35
  ```
40
36
 
41
37
  It also replaces all burek calls with regular translation calls
@@ -43,12 +39,40 @@ It also replaces all burek calls with regular translation calls
43
39
  ```html
44
40
  # views/users/index.html
45
41
  <h1>
46
- <%= t("views.users.all_users") %>
42
+ <%= t("all_users") %>
47
43
  </h1>
48
44
  ```
49
45
 
50
46
  That's it!
51
47
 
48
+ ## Additional options
49
+
50
+ You can also pass a hash as second argument to burek call.
51
+
52
+ ```html
53
+ # views/users/index.html
54
+ <h1>
55
+ <%= burek("All users", {key: 'users', parent_key: 'levelone.leveltwo'}) %>
56
+ </h1>
57
+ ```
58
+
59
+ This call will generate following translation file:
60
+
61
+ ```ruby
62
+ # config/locales/burek/views/users/index.fi.yml
63
+ en:
64
+ levelone:
65
+ leveltwo:
66
+ users: "All users"
67
+ ```
68
+
69
+ ```html
70
+ # views/users/index.html
71
+ <h1>
72
+ <%= t("levelone.leveltwo.users") %>
73
+ </h1>
74
+ ```
75
+
52
76
  ## How to install it?
53
77
 
54
78
  Just add following line to your Gemfile:
data/lib/config.rb CHANGED
@@ -24,7 +24,7 @@ module Burek
24
24
 
25
25
  @@configuration = BurekConfiguration.new({
26
26
  search_folders: ['./app/views/**/*'],
27
- translations_path: './config/locales/burek/',
27
+ translations_path: './config/locales/',
28
28
  translation_placeholder: 'TODO',
29
29
  ignore_folders_for_key: ['.','app'],
30
30
  subfolder_depth: 2,
@@ -0,0 +1,55 @@
1
+ module Burek
2
+ class BurekCall
3
+
4
+ def initialize(translation, options={})
5
+ if translation.is_a?(Hash)
6
+ @translation = translation
7
+ else
8
+ @translation = {default_locale => translation }
9
+ end
10
+ @key = options[:key] || key_from_translation
11
+ @parent_key = options[:parent_key] || ''
12
+ end
13
+
14
+ def translation(locale=nil)
15
+ locale ||= default_locale
16
+ locale = locale.to_s
17
+ if @translation.has_key?(locale)
18
+ return @translation[locale]
19
+ else
20
+ return Burek.config.get(:translation_placeholder)
21
+ end
22
+ end
23
+
24
+ def key
25
+ @key
26
+ end
27
+
28
+ def parent_key
29
+ @parent_key
30
+ end
31
+
32
+ def full_key
33
+ if parent_key.nil? || parent_key.length == 0
34
+ key
35
+ else
36
+ parent_key + "." + key
37
+ end
38
+ end
39
+
40
+ def parent_key_array
41
+ @parent_key.split('.')
42
+ end
43
+
44
+ private
45
+
46
+ def key_from_translation
47
+ translation.downcase.gsub(' ','_')
48
+ end
49
+
50
+ def default_locale
51
+ Burek.config.get(:locales).first
52
+ end
53
+
54
+ end
55
+ end
data/lib/core/core.rb CHANGED
@@ -1,8 +1,8 @@
1
1
  require 'config'
2
2
  require 'yaml'
3
- require 'core/burek_replacer'
4
- require 'core/burek_finder'
5
- require 'core/locales_creator'
3
+ require 'core/burek_call'
4
+ require 'core/translations_store'
5
+
6
6
  require 'file_helpers'
7
7
 
8
8
  module Burek
@@ -19,38 +19,58 @@ module Burek
19
19
  Burek::FileHelpers.create_folder_if_missing Burek.config.get(:translations_path)
20
20
 
21
21
  puts "Searching for burek calls..."
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}'"
22
+
23
+ # Initializing translations store
24
+ translations = TranslationsStore.new
25
+ Burek.config.get(:locales).each do |locale|
26
+ filename = "burek.#{locale}.yml"
27
+ file_path = Burek.config.get(:translations_path) + filename
28
+ translations.load_locale locale, file_path
29
+ end
30
+
31
+ # Replacing calls and updating translations hash
32
+ Burek::FileHelpers.open_each_file do |contents, file_name|
33
+ matches = fetch_params_from_string(contents)
34
+ matches.each do |call_params|
35
+ call = burek_call_from_params_string call_params
36
+ translations.push_call call
37
+ contents.gsub!(burek_call_params_regex(call_params),"t('#{call.full_key}')")
38
+ end
39
+ File.open(file_name, "w:UTF-8") do |f|
40
+ f.write contents
26
41
  end
27
- else
28
- puts "No burek calls found!"
29
42
  end
30
-
31
- puts "Adding translations to locale filess..."
32
- to_replace = Burek::LocalesCreator.create_locales(new_translations)
33
-
34
- puts "Repalcing burek calls with translation calls..."
35
- Burek::Replacer.replace_burek_calls_in_files(to_replace)
43
+
44
+ # Saving new translations
45
+ Burek.config.get(:locales).each do |locale|
46
+ filename = "burek.#{locale}.yml"
47
+ file_path = Burek.config.get(:translations_path) + filename
48
+ translations.save_locale locale, file_path
49
+ end
36
50
 
37
51
  puts "DONE!"
38
52
  end
39
53
 
54
+ def self.store_burek_call_to_locale_file(call)
55
+ Burek.config.get(:locales).each do |locale|
56
+ translations_hash_to_file translation_hash, file_path
57
+ end
58
+ end
59
+
60
+ def self.fetch_params_from_string(string)
61
+ string.scan(self.burek_call_params_regex).flatten
62
+ end
63
+
64
+ def self.burek_call_from_params_string(params)
65
+ call = "BurekCall.new(#{params})"
66
+ eval call
67
+ end
68
+
69
+ private
70
+
40
71
  # A regex for finiding burek calls
41
- #
42
- # It is used by Burek::Finder and Burek::Replacer.
43
- # Example calls it can find
44
- # * burek('Hello world')
45
- # * burek("Hello world")
46
- # * burek ( "Hello world" )
47
- #
48
- # ==== Attributes
49
- # * +caption+ - Use it to find burek call with specific caption.
50
- # By default it will accept any string and capture it in group named 'key'.
51
- #
52
- def self.burek_call_regex(caption="(?<key>[^\\)]*)")
53
- Regexp.new("[^a-zA-Z0-9_]burek[ \\t]*\\([ \\t]*('|\")#{caption}\('|\")[^\\)]*\\)")
72
+ def self.burek_call_params_regex(caption='([^\)]*)')
73
+ /burek *\(#{caption}\)/
54
74
  end
55
75
 
56
76
  end
@@ -0,0 +1,59 @@
1
+ module Burek
2
+
3
+ class TranslationsStore
4
+
5
+ def initialize(translations_hash={})
6
+ @translations_hash = translations_hash
7
+ end
8
+
9
+ def load_locale(locale, file_path)
10
+ locale = locale.to_s
11
+ if File.exists?(file_path)
12
+ @translations_hash = YAML::load_file(file_path) #Load
13
+ else
14
+ @translations_hash[locale] = {}
15
+ end
16
+ end
17
+
18
+ def save_locale(locale, file_path)
19
+ locale = locale.to_s
20
+ File.open(file_path, "w:UTF-8") do |f|
21
+ f.write locale_yaml(locale)
22
+ end
23
+ end
24
+
25
+ def push_call(call)
26
+
27
+ Burek.config.get(:locales).each do |locale|
28
+ # In case it's a symbole convert it to string
29
+ locale = locale.to_s
30
+
31
+ # Initialize hash for current locale
32
+ @translations_hash[locale] = {} unless @translations_hash.has_key?(locale)
33
+
34
+ # Nest hash
35
+ cur_hash = @translations_hash[locale]
36
+ call.parent_key_array.each do |item|
37
+ cur_hash[item] = {} unless cur_hash.has_key?(item)
38
+ cur_hash = cur_hash[item]
39
+ end
40
+ cur_hash[call.key] = call.translation(locale)
41
+ end
42
+
43
+ end
44
+
45
+ def get_hash
46
+ @translations_hash
47
+ end
48
+
49
+ private
50
+
51
+ def locale_yaml(locale)
52
+ yaml = {locale => @translations_hash[locale] }.to_yaml # Pluck only one top level key
53
+ clean_yaml = yaml.lines.to_a[1..-1].join # Clean up first line
54
+ return clean_yaml
55
+ end
56
+
57
+ end
58
+
59
+ end
@@ -2,7 +2,7 @@ module Burek
2
2
 
3
3
  module ViewHelpers
4
4
  def burek(key)
5
- raw "<span class='translation_missing'>#{key}</span>"
5
+ raw "<span style='background-color:red;'>#{key}</span>"
6
6
  end
7
7
  end
8
8
 
metadata CHANGED
@@ -1,15 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: burek
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.9
5
- prerelease:
4
+ version: 0.6.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Filip Defar
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-09-25 00:00:00.000000000 Z
11
+ date: 2014-09-20 00:00:00.000000000 Z
13
12
  dependencies: []
14
13
  description: Manage your translations the easy way.
15
14
  email: dabrorius@gmail.com
@@ -20,36 +19,34 @@ files:
20
19
  - tasks/tasks.rb
21
20
  - lib/burek.rb
22
21
  - lib/config.rb
23
- - lib/core/burek_finder.rb
24
- - lib/core/burek_replacer.rb
22
+ - lib/core/burek_call.rb
25
23
  - lib/core/core.rb
26
- - lib/core/locales_creator.rb
24
+ - lib/core/translations_store.rb
27
25
  - lib/file_helpers.rb
28
26
  - lib/rails/view_helpers.rb
29
27
  - README.md
30
28
  homepage: https://github.com/dabrorius/burek
31
29
  licenses:
32
30
  - MIT
31
+ metadata: {}
33
32
  post_install_message:
34
33
  rdoc_options: []
35
34
  require_paths:
36
35
  - lib
37
36
  required_ruby_version: !ruby/object:Gem::Requirement
38
- none: false
39
37
  requirements:
40
- - - ! '>='
38
+ - - '>='
41
39
  - !ruby/object:Gem::Version
42
40
  version: '0'
43
41
  required_rubygems_version: !ruby/object:Gem::Requirement
44
- none: false
45
42
  requirements:
46
- - - ! '>='
43
+ - - '>='
47
44
  - !ruby/object:Gem::Version
48
45
  version: '0'
49
46
  requirements: []
50
47
  rubyforge_project:
51
- rubygems_version: 1.8.23
48
+ rubygems_version: 2.0.3
52
49
  signing_key:
53
- specification_version: 3
50
+ specification_version: 4
54
51
  summary: Manage your translations the easy way.
55
52
  test_files: []
@@ -1,60 +0,0 @@
1
- require 'file_helpers'
2
-
3
- module Burek
4
- module Finder
5
-
6
- # Searches all files within search folders for burek calls.
7
- #
8
- # ==== Returns
9
- # Hash in whichs values represent burek call captions and keys represent locales file paths in which
10
- # those captions were found
11
- #
12
- def self.find_burek_calls_in_files
13
- new_translations = {}
14
- Burek::FileHelpers.open_each_file do |contents, file_name|
15
- filtered_path = filter_path(file_name)
16
- matches = find_burek_calls(contents)
17
- matches.each do |caption|
18
- key = filtered_path + "/" + caption_to_key_part(caption)
19
- new_translations[key] = caption
20
- end
21
- end
22
- return new_translations
23
- end
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
29
-
30
- # Finds burek calls in a string
31
- #
32
- # ==== Attributes
33
- # * +string+ - A string that (possibly) contains burek calls
34
- #
35
- # ==== Returns
36
- # Array of burek call captions
37
- #
38
- def self.find_burek_calls(string)
39
- string.scan(Burek::Core.burek_call_regex).flatten
40
- end
41
-
42
- # Removes file extension from a path as well as any folder that is specified as ignored folder in config
43
- #
44
- # ==== Attributes
45
- # * +file_name+ - path in string format that needs to be filtered
46
- #
47
- # ==== Returns
48
- # Filtered path as a string
49
- #
50
- def self.filter_path(file_name)
51
- path = file_name.split('/')
52
- path.delete_if do |item|
53
- Burek.config.get(:ignore_folders_for_key).include? item
54
- end
55
- path.last.gsub!(/\.(.*?)$/,'').gsub!(/^_/,'') #strip extenison from file name
56
- return path.join('/')
57
- end
58
-
59
- end
60
- end
@@ -1,32 +0,0 @@
1
- require 'file_helpers'
2
- require 'core/burek_finder'
3
-
4
- module Burek
5
- module Replacer
6
-
7
- def self.replace_burek_calls_in_files(to_replace)
8
- Burek::FileHelpers.open_each_file do |content,file_name|
9
- processed_content = replace_burek_calls(content, to_replace)
10
- unless processed_content.nil?
11
- File.open(file_name, 'w') do |output_file|
12
- output_file.print processed_content
13
- end
14
- end
15
- end
16
- end
17
-
18
- def self.replace_burek_calls(contents, to_replace)
19
- matches = Burek::Finder.find_burek_calls(contents)
20
- changes_made = false
21
- matches.each do |value|
22
- if to_replace.has_key?(value)
23
- contents.gsub!(Burek::Core.burek_call_regex(value)," t('#{to_replace[value]}')")
24
- changes_made = true
25
- end
26
- end
27
-
28
- return contents if changes_made
29
- end
30
-
31
- end
32
- end
@@ -1,120 +0,0 @@
1
- module Burek
2
- module LocalesCreator
3
-
4
- # Creates new or updates exisitng locales files. Called by Burek::Finder
5
- #
6
- # ==== Attributes
7
- # * +new_translations+ - A hash of new translations that were found in search folders.
8
- # Values represent burek call captions and keys represent locales file paths in which those captions were found.
9
- #
10
- # ==== Returns
11
- # A hash of burek calls that need to be replaced by translation calls.
12
- # Keys contain captions of burek calls and values contain key which translation keys should contain.
13
- #
14
- def self.create_locales(new_translations)
15
- to_replace = {}
16
- # Create files for each locale
17
- Burek.config.get(:locales).each do |locale|
18
- new_translations.each do |key,value|
19
- path_parts = key.split("/")
20
- item_name = path_parts.pop
21
- path_parts_no_filename = path_parts[0..-2]
22
-
23
- translation_file = key_to_file_path(key, locale)
24
- translations_hash = initialize_translations_hash(translation_file, locale)
25
-
26
- # Save info for replacing burek calls with translation calls.
27
- # Keys are burek call captions and values are keys to be used in translation calls.
28
- to_replace[value] = path_parts_to_key(path_parts_no_filename, item_name)
29
-
30
- # Nest in hashes
31
- cur_hash = translations_hash[locale]
32
- path_parts_no_filename.each do |item|
33
- cur_hash[item] = {} unless cur_hash.has_key?(item)
34
- cur_hash = cur_hash[item]
35
- end
36
- cur_hash[string_to_key(item_name)] = ( locale == Burek.config.get(:locales).first ? value : Burek.config.get(:translation_placeholder) )
37
-
38
- # Save to file
39
- Burek::LocalesCreator.translations_hash_to_file(translations_hash, translation_file)
40
- end
41
- end
42
-
43
- return to_replace
44
- end
45
-
46
- # Stores a translation hash in to a file
47
- #
48
- def self.translations_hash_to_file(translations_hash, translation_file)
49
- clean_yaml = yaml_to_i18n_file(translations_hash.to_yaml)
50
- translation_file.gsub!("//","/")
51
- File.open(translation_file, "w:UTF-8") do |f|
52
- f.write clean_yaml
53
- end
54
- end
55
-
56
- # Removes a first line from a yaml file which is added to standard YAML files
57
- # but rails does not like it. It continas ---- or something like that
58
- #
59
- def self.yaml_to_i18n_file(yaml)
60
- yaml.lines.to_a[1..-1].join
61
- end
62
-
63
- # Intializes a translation hash by either loading existing translation file
64
- # or creating a hash that contains an empty hash under key which is name of current locale.
65
- def self.initialize_translations_hash(translation_file, locale)
66
- if File.exists?(translation_file)
67
- translations_hash = YAML::load_file(translation_file) #Load
68
- else
69
- translations_hash = {}
70
- translations_hash[locale] = {}
71
- end
72
- return translations_hash
73
- end
74
-
75
- def self.string_to_key(string)
76
- string.split(' ')[0..3].join('_').downcase.gsub(/[^0-9a-z_]/i, '')
77
- end
78
-
79
- # Creates a translation call key from path where burek call was found and burek call caption
80
- #
81
- def self.path_parts_to_key(path_parts, item_name)
82
- regular_translation_key = path_parts.join('.')
83
- regular_translation_key += "." unless regular_translation_key.nil? || regular_translation_key.empty?
84
- regular_translation_key += "#{string_to_key(item_name)}"
85
- end
86
-
87
- # Generates a file path to locales file from a file path where burek call was found.
88
- # It will nest locales file under same subfolders in which burek call was found.
89
- # If burek call was nested deeper then defined as subfolder depth limit in config file
90
- # it will nest it under first X folders (where x is subfolder depth limit)
91
- #
92
- # ==== Attributes
93
- # * +key+ - file path where burek call was found
94
- # * +locale+ - locale for which this file is created
95
- #
96
- # ==== Returns
97
- # A string that contains a path where locale file should be created.
98
- # NOTE: Even though subfolders are created, the actuall locale file is NOT created.
99
- #
100
- def self.key_to_file_path(key, locale)
101
- path_parts = key.split("/")
102
- path_parts.pop
103
-
104
- translation_file = Burek.config.get(:translations_path)
105
- path_parts.each_with_index do |item, index|
106
- if index == Burek.config.get(:subfolder_depth) || item == path_parts.last
107
- translation_file += "#{item}.#{locale}.yml"
108
- break
109
- else
110
- translation_file += "/#{item}/"
111
- unless File.directory?(translation_file)
112
- Dir.mkdir(translation_file)
113
- end
114
- end
115
- end
116
- return translation_file
117
- end
118
-
119
- end
120
- end