traduki-clt 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a95a9db2179c133dbc105bc0ebc027d97d1f1f32
4
+ data.tar.gz: 52997cff51d7e60f99e28214bef6ccb29528dece
5
+ SHA512:
6
+ metadata.gz: b00ea116c68097757c53755d811df6188792cc546f80c8a2e2f3a4596d29aa5bc49e64cf88efe1af1955a025cf456ce08b92888ced3ac8998d9d61022757055f
7
+ data.tar.gz: 300c92666b239ddf5731502707c5c77cfe8d2e14a2f8b63b6bb2ca3d8b1467dfa2b0c2fa3b8119466407eb766d06b203da35eaeb497b8de525517d4ae255990c
data/.gitignore ADDED
@@ -0,0 +1,16 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
13
+
14
+ *.gem
15
+
16
+ /.idea/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,11 @@
1
+ Metrics/LineLength:
2
+ Max: 110
3
+
4
+ Metrics/MethodLength:
5
+ Max: 20
6
+
7
+ Metrics/AbcSize:
8
+ Max: 20
9
+
10
+ Style/Documentation:
11
+ Enabled: false
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.1
5
+ before_install: gem install bundler -v 1.15.2
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at vez@crazybaby.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in traduki.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Vergil Choi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # Traduki
2
+
3
+ Generate your keys in code files of translation.
4
+
5
+ Manage your keys automatically without pains.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'traduki'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install traduki
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/vergil-choi/traduki. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
40
+
41
+ ## Code of Conduct
42
+
43
+ Everyone interacting in the Traduki project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/vergil-choi/traduki/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/bin/traduki ADDED
@@ -0,0 +1,19 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'traduki'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ # require 'irb'
14
+ # IRB.start(__FILE__)
15
+
16
+ # Traduki.init '/Users/vergilchoi/Documents/Repositories/CrazybabyCore/CrazybabyCore'
17
+ # Traduki.generate
18
+
19
+ Traduki::CLI.start(ARGV)
@@ -0,0 +1,81 @@
1
+ require 'traduki/config'
2
+ require 'traduki/generator'
3
+ require 'thor'
4
+ require 'rainbow/ext/string'
5
+
6
+ module Traduki
7
+ class CLI < Thor
8
+ class_option :verbose, type: :boolean, aliases: :v
9
+
10
+ default_task :generate
11
+
12
+ desc 'version', 'Display version of traduki'
13
+
14
+ def version
15
+ puts Traduki::VERSION
16
+ end
17
+
18
+ desc 'generate [PATH] [-v|--verbose]', 'Generate translations'
19
+ option :clean, type: :boolean, default: false, aliases: :c
20
+ def generate(path = nil)
21
+ puts "\n"
22
+ generator = UserInterface.generator path
23
+ generator.generate options[:clean]
24
+ if options[:verbose]
25
+ generator.parser.keys.each do |p, ks|
26
+ ks.each do |k|
27
+ puts k[0].color(:blue) + ' - ' + p
28
+ end
29
+ end
30
+ end
31
+
32
+ puts "#{generator.parser.keys_count} keys has been found and collected.".color(:green)
33
+ puts "\n"
34
+ end
35
+
36
+ desc 'clean', 'Generate and clean not exist keys, also files'
37
+
38
+ def clean(path = nil)
39
+ generator = UserInterface.generator path
40
+ generator.generate
41
+ generator.scan
42
+ UserInterface.print_garbage generator if options[:verbose]
43
+ UserInterface.clean_check generator
44
+ print 'Remove all keys and files are not present in your code files? [y/N]'.color :yellow
45
+ UserInterface.clean generator if STDIN.gets.chomp =~ /^[Yy]/
46
+ end
47
+
48
+ desc 'extract', 'Generate or upgrade config file'
49
+
50
+ def extract(path = nil)
51
+ Traduki.config.workdir = path.dup if path
52
+ Traduki.config.create_config
53
+ puts 'Config file has beed put at ' + Traduki.config.workdir + '/traduki.yml'
54
+ end
55
+
56
+ end
57
+
58
+ class UserInterface
59
+ def self.generator(path)
60
+ Traduki.config.workdir = path.dup if path
61
+ Generator.new
62
+ end
63
+
64
+ def self.print_garbage(generator)
65
+ generator.garbage.each do |f|
66
+ path = f.clone
67
+ path.slice! Traduki.config.workdir
68
+ puts path[1, path.length - 1]
69
+ end
70
+ end
71
+
72
+ def self.clean(generator)
73
+ generator.clean && puts('Cleaned.'.color(:green))
74
+ end
75
+
76
+ def self.clean_check(generator)
77
+ puts('Nothing to clean.'.color(:green)) unless generator.need_clean?
78
+ exit unless generator.need_clean?
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,75 @@
1
+ require 'pathname'
2
+ require 'yaml'
3
+ require 'json'
4
+
5
+ module Traduki
6
+ class Config
7
+ attr_accessor :workdir
8
+ attr_reader :langdir
9
+ attr_reader :languages
10
+ # attr_reader :flat
11
+
12
+ def initialize(path = Dir.pwd)
13
+ @workdir = path.path
14
+ Dir.chdir @workdir
15
+ load_config
16
+ end
17
+
18
+ def load_config_file
19
+ begin
20
+ @config = YAML.load_file(@workdir + '/traduki.yml') || {}
21
+ rescue Errno::ENOENT
22
+ @config = {}
23
+ end
24
+ end
25
+
26
+ def load_config
27
+ load_config_file
28
+
29
+ # @flat = @config['flat']
30
+ # @flat = false if @config['flat'].nil?
31
+ @languages = @config['languages'] || %w[en_US zh_CN]
32
+ @relative_path = @config['path'] || 'Languages'
33
+ @langdir = (@workdir + "/#{@relative_path}/").path
34
+ end
35
+
36
+ def create_config
37
+ File.open(@workdir + '/traduki.yml', 'w') do |f|
38
+ f.write YAML.dump generate_config
39
+ end
40
+ end
41
+
42
+ def create_internal_config
43
+ File.open(@langdir + '/traduki.json', 'w') { |f| f.write "// Please DON'T edit this file.\n" }
44
+ File.open(@langdir + '/traduki.json', 'a') { |f| f.write JSON.dump generate_config }
45
+ end
46
+
47
+ def generate_config
48
+ {
49
+ # 'flat' => @flat,
50
+ 'languages' => @languages,
51
+ 'path' => @relative_path
52
+ }
53
+ end
54
+
55
+ def workdir=(value)
56
+ @workdir = value.path
57
+ @langdir = (value + "/#{@relative_path}/").path
58
+ Dir.chdir value.path
59
+ load_config
60
+ end
61
+
62
+ end
63
+
64
+ def self.config
65
+ @config ||= Config.new
66
+ end
67
+ end
68
+
69
+ class String
70
+ def path
71
+ components = clone.split('/').reject { |c| c == '' }
72
+
73
+ @path ||= '/' + components.join('/')
74
+ end
75
+ end
@@ -0,0 +1,110 @@
1
+ require 'traduki/config'
2
+ require 'traduki/parser'
3
+ require 'traduki/translation'
4
+ require 'fileutils'
5
+
6
+ module Traduki
7
+ class Generator
8
+ attr_reader :parser
9
+ attr_reader :garbage
10
+
11
+ def initialize
12
+ @parser = Parser.new.parse
13
+ @garbage = []
14
+ @translations = []
15
+ end
16
+
17
+ def generate(clean = false)
18
+ Traduki.config.languages.each do |lang|
19
+ trans = Translation[lang]
20
+ @parser.keys.each do |path, metas|
21
+ metas.each do |meta|
22
+ trans.add(meta[0], path, meta[1], meta[2])
23
+ end
24
+ end
25
+ trans.clean if clean
26
+ trans.save
27
+ end
28
+ Traduki.config.create_internal_config unless @parser.keys.count.zero?
29
+ end
30
+
31
+ # DANGEROUS!! MUST call `generate` before clean, or all files will be removed!
32
+ def scan
33
+ single_file_mode_scan
34
+ end
35
+
36
+ def single_file_mode_scan
37
+ Dir[Traduki.config.langdir + '/**/*'].each do |path|
38
+ next if !File.directory?(path) && (
39
+ Traduki.config.languages.include?(File.basename(path, '.json')) ||
40
+ File.basename(path, '.json') == 'metadata'
41
+ )
42
+ @garbage.push path unless config? path
43
+ end
44
+ end
45
+
46
+ def multi_file_mode_scan
47
+ scan_files_outside
48
+ scan_files_inside
49
+ end
50
+
51
+ def scan_files_inside
52
+ Traduki.config.languages.each do |lang|
53
+ Dir[Traduki.config.langdir + "/#{lang}/**/*"].each do |path|
54
+ next if File.directory? path
55
+ key = path.clone
56
+ key.slice!(Traduki.config.langdir + "/#{lang}/")
57
+ @garbage.push path unless @parser.keys.key? key.path
58
+ end
59
+ end
60
+ end
61
+
62
+ def scan_files_outside
63
+ Dir[Traduki.config.langdir + '/*.*'].each { |p| @garbage.push p unless config? p }
64
+ end
65
+
66
+ def clean_empty_dirs
67
+ Dir[Traduki.config.langdir + '/*']
68
+ .select { |d| File.directory? d } \
69
+ .each { |d| rmdir_r_empty d }
70
+ end
71
+
72
+ def rmdir_r_empty(dir)
73
+ if empty? dir
74
+ FileUtils.rm_rf dir
75
+ else
76
+ Dir[dir + '/*']
77
+ .select { |d| File.directory? d } \
78
+ .each { |d| rmdir_r_empty d }
79
+ FileUtils.rm_rf dir if empty? dir
80
+ end
81
+ end
82
+
83
+ def clean
84
+ @translations.each do |t|
85
+ t.clean
86
+ t.save
87
+ end
88
+ @garbage.each { |f| FileUtils.rm_rf f }
89
+ clean_empty_dirs
90
+ true
91
+ end
92
+
93
+ def config?(path)
94
+ p = path.clone
95
+ p.slice! Traduki.config.langdir
96
+ p == '/traduki.json'
97
+ end
98
+
99
+ def empty?(dir)
100
+ entries = Dir.entries(dir)
101
+ flag = true
102
+ entries.map! { |c| flag = false unless c.start_with? '.' }
103
+ flag || Dir.empty?(dir)
104
+ end
105
+
106
+ def need_clean?
107
+ @garbage.count > 0
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,83 @@
1
+ module Traduki
2
+ class HashTool
3
+ def self.validate_dotkey(dotkey)
4
+ raise InvalidDotkey, dotkey unless /^[a-z0-9.-]+$/ =~ dotkey
5
+ end
6
+
7
+ def self.get_value(dict, dotkey)
8
+ validate_dotkey dotkey
9
+ temp = dict
10
+ dotkey.split('.').each do |key|
11
+ temp = temp[key]
12
+ end
13
+ return temp['_'] if temp.is_a?(Hash) && !temp['_'].nil?
14
+ temp
15
+ end
16
+
17
+ def self.safe_set(dict, dotkey)
18
+ validate_dotkey dotkey
19
+ temp = dict
20
+ keys = dotkey.split('.')
21
+ keys.each_with_index do |key, index|
22
+ next if index == keys.count - 1
23
+ temp[key] = {} if temp[key].nil?
24
+ temp[key] = { '_' => temp[key] } if temp[key].is_a? String
25
+ temp = temp[key]
26
+ end
27
+ _safe_set temp, keys.last, dotkey
28
+ end
29
+
30
+ def self._safe_set(temp, key, value)
31
+ if temp[key].is_a? Hash
32
+ temp[key]['_'] = value unless temp[key]['_']
33
+ else
34
+ temp[key] = value unless temp[key]
35
+ end
36
+ end
37
+
38
+ def self.key?(dict, dotkey)
39
+ validate_dotkey dotkey
40
+ flag = true
41
+ temp = dict
42
+ dotkey.split('.').each do |key|
43
+ flag = temp.key? key
44
+ break unless flag
45
+ temp = temp[key]
46
+ end
47
+ flag
48
+ end
49
+
50
+ def self.contrastiat(dotkeys)
51
+ contrast = {}
52
+ dotkeys.split('.').each { |key| set_value contrast, key, key }
53
+ contrast
54
+ end
55
+
56
+ def self.clean(dict, contrast)
57
+ result = contrast.clone
58
+ result.each do |key, _|
59
+ if result[key].is_a?(Hash) && dict[key].is_a?(Hash)
60
+ clean(dict[key], result[key])
61
+ next
62
+ end
63
+ if result[key].is_a?(String) && dict[key].is_a?(String)
64
+ result[key] = dict[key]
65
+ end
66
+
67
+ if result[key].is_a?(String) && dict[key].is_a?(Hash)
68
+ result[key] = dict['_'] unless dict['_'].nil?
69
+ end
70
+
71
+ if result[key].is_a?(Hash) && dict[key].is_a?(String)
72
+ result[key]['_'] = dict[key]
73
+ end
74
+ end
75
+ result
76
+ end
77
+
78
+ class HashToolError < StandardError
79
+ end
80
+ class InvalidDotkey < HashToolError
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,71 @@
1
+ require 'traduki/config'
2
+ require 'pp'
3
+
4
+ module Traduki
5
+ class Parser
6
+ REGEXES = {
7
+ :'.m' => /__\([\n\s]*@"([a-z0-9.-]+)"/,
8
+ :'.mm' => /__\([\n\s]*[@]?"([a-z0-9.-]+)/,
9
+ :'.swift' => /__\([\n\s]*"([a-z0-9.-]+)"(?:[\n\s]*,[\n\s]*"([^"]*)")*([\n\s]*,[\n\s]*\[(?:[\n\s]*,*[\n\s]*"[^"]*"[\n\s]*:[\n\s]*[^,]+)*[\n\s]*\])*/,
10
+ :'.js' => /__\([\n\s]*["']([a-z0-9.-]+)["']/,
11
+ :'.py' => /__\([\n\s]*["']([a-z0-9.-]+)["']/
12
+ }.freeze
13
+
14
+ PARAM_REGEXS = {
15
+ :'.m' => /"([^"]+)"[\n\s]*:/,
16
+ :'.mm' => /"([^"]+)"[\n\s]*:/,
17
+ :'.swift' => /"([^"]+)"[\n\s]*:/,
18
+ :'.js' => /"([^"]+)"[\n\s]*:/,
19
+ :'.py' => /"([^"]+)"[\n\s]*:/
20
+ }.freeze
21
+
22
+ SUPPORTED_TYPE = %w[.m .mm .swift .js .py].freeze
23
+
24
+ attr_reader :keys
25
+
26
+ def initialize
27
+ @keys = {}
28
+ @contrasts = {}
29
+ parse
30
+ end
31
+
32
+ def parse
33
+ @keys = {}
34
+ Dir.glob(SUPPORTED_TYPE.map { |s| "#{Traduki.config.workdir}/**/*#{s}" }) do |file_path|
35
+ unless File.directory? file_path
36
+ keys = Parser.extract(file_path)
37
+ @keys[slice file_path] = keys unless keys.empty?
38
+ end
39
+ end
40
+ self
41
+ end
42
+
43
+ def slice(path)
44
+ newpath = path.clone.path
45
+ newpath.slice!(Traduki.config.workdir)
46
+ newpath
47
+ end
48
+
49
+ def self.extract(file_path)
50
+ file = open file_path
51
+ extname = File.extname(file.path)
52
+ return nil unless SUPPORTED_TYPE.include? extname
53
+ regex = REGEXES[extname.to_sym]
54
+ file.read.scan(regex).map do |k|
55
+ k[2] = scan_params(k[2], extname) if k[2]
56
+ k
57
+ end
58
+ end
59
+
60
+ def self.scan_params(params, extname)
61
+ params.scan(PARAM_REGEXS[extname.to_sym]).flatten
62
+ end
63
+
64
+ def keys_count
65
+ count = 0
66
+ @keys.each { |_, v| count += v.count }
67
+ count
68
+ end
69
+
70
+ end
71
+ end
@@ -0,0 +1,20 @@
1
+ require 'traduki/translation'
2
+ require 'traduki/parser'
3
+ require 'traduki/config'
4
+ require 'json'
5
+
6
+
7
+ module Traduki
8
+ class Reader
9
+ @cache = {}
10
+
11
+ def self.[](lang)
12
+ trans_of((Traduki.config.langdir + '/' + lang + '.json').path)
13
+ end
14
+
15
+ def self.trans_of(lang)
16
+ @cache[lang] ||= Translation.new lang
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,100 @@
1
+ require 'traduki/config'
2
+ require 'traduki/hash_tool'
3
+ require 'json'
4
+ require 'fileutils'
5
+
6
+ module Traduki
7
+ class Translation
8
+ attr_reader :path
9
+ attr_reader :data
10
+
11
+ def initialize(lang = nil)
12
+ raise EmptyPath, 'Language cannot be nil.' if lang.nil?
13
+ @lang = lang
14
+ @path = Traduki.config.langdir + '/' + lang + '.json'
15
+ @metapath = Traduki.config.langdir + '/' + 'metadata.json'
16
+ @contrast = {}
17
+ @meta_contrast = []
18
+ load_data
19
+ load_meta
20
+ end
21
+
22
+ def self.[](lang)
23
+ new lang
24
+ end
25
+
26
+ def [](dotkey)
27
+ HashTool.get_value @data, dotkey
28
+ end
29
+
30
+ def add(dotkey, path, desc = nil, placeholders = nil)
31
+ occurences = []
32
+ occurences = @meta[dotkey][:occurences] if @meta[dotkey] && @meta[dotkey][:occurences]
33
+ @meta[dotkey] = {
34
+ occurences: (occurences + [path]).uniq,
35
+ placeholders: placeholders,
36
+ desc: desc
37
+ }
38
+ @meta_contrast.push dotkey
39
+ HashTool.safe_set @data, dotkey
40
+ HashTool.safe_set @contrast, dotke
41
+ end
42
+
43
+ def key?(dotkey)
44
+ HashTool.key? @data, dotkey
45
+ end
46
+
47
+ def clean
48
+ @data = HashTool.clean @data, @contrast
49
+ new_meta = {}
50
+ @meta_contrast.each { |m| new_meta[m] = @meta[m] }
51
+ @meta = new_meta
52
+ self
53
+ end
54
+
55
+ def clean_contrast
56
+ @contrast = {}
57
+ end
58
+
59
+ def save
60
+ dir = File.split(@path).first
61
+ FileUtils.mkpath dir unless Dir.exist? dir
62
+ # File.open(@path, 'w') { |f| f.write "// Please DON'T edit this file.\n" }
63
+ File.open(@path, 'w') { |f| f.write JSON.generate @data }
64
+ save_meta
65
+ end
66
+
67
+ def save_meta
68
+ dir = File.split(@metapath).first
69
+ FileUtils.mkpath dir unless Dir.exist? dir
70
+ # File.open(@metapath, 'w') { |f| f.write "// Please DON'T edit this file.\n" }
71
+ File.open(@metapath, 'w') { |f| f.write JSON.generate @meta }
72
+ end
73
+
74
+ def load_data
75
+ @data = JSON.parse File.read @path
76
+ rescue Errno::ENOENT
77
+ @data = {}
78
+ rescue Errno::EACCES
79
+ raise FileCannotAccess
80
+ end
81
+
82
+ def load_meta
83
+ @meta = JSON.parse File.read @metapath
84
+ @keys = @meta.keys
85
+ rescue Errno::ENOENT
86
+ @meta = {}
87
+ @keys = []
88
+ rescue Errno::EACCES
89
+ raise FileCannotAccess
90
+ end
91
+
92
+ end
93
+
94
+ class TranslationError < StandardError
95
+ end
96
+ class EmptyPath < TranslationError
97
+ end
98
+ class FileCannotAccess < TranslationError
99
+ end
100
+ end
@@ -0,0 +1,3 @@
1
+ module Traduki
2
+ VERSION = '0.1.0'.freeze
3
+ end
data/lib/traduki.rb ADDED
@@ -0,0 +1,15 @@
1
+ require 'traduki/version'
2
+ require 'traduki/config'
3
+ require 'traduki/generator'
4
+ require 'traduki/cli'
5
+
6
+ # Root module
7
+ module Traduki
8
+ # Output result of translations
9
+ def self.generate(path = Dir.pwd)
10
+ Traduki.config.workdir = path
11
+ generator = Generator.new
12
+ generator.generate
13
+ # generator.clean
14
+ end
15
+ end
data/traduki.gemspec ADDED
@@ -0,0 +1,36 @@
1
+ # coding: utf-8
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'traduki/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'traduki-clt'
9
+ spec.version = Traduki::VERSION
10
+ spec.authors = ['Vergil Choi']
11
+ spec.email = ['gameboy0824@126.com']
12
+
13
+ spec.summary = 'Manage your language translations with no pains.'
14
+ spec.description = <<-DESC
15
+ Generate your keys in code files of translation.
16
+
17
+ Manage your keys automatically without pains.
18
+ DESC
19
+ spec.homepage = 'https://github.com/vergil-choi/traduki'
20
+ spec.license = 'MIT'
21
+
22
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
23
+ spec.bindir = 'bin'
24
+ spec.executables = ['traduki']
25
+ spec.require_paths = ['lib']
26
+ # spec.bindir = 'exe'
27
+ # spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+
29
+ spec.add_runtime_dependency 'json', '~> 2.1'
30
+ spec.add_runtime_dependency 'thor', '~> 0.19'
31
+ spec.add_runtime_dependency 'rainbow', '~> 2.2'
32
+
33
+ spec.add_development_dependency 'bundler', '~> 1.15'
34
+ spec.add_development_dependency 'rake', '~> 10.0'
35
+ spec.add_development_dependency 'rspec', '~> 3.0'
36
+ end
metadata ADDED
@@ -0,0 +1,153 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: traduki-clt
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Vergil Choi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-07-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: thor
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.19'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.19'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rainbow
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.2'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.2'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.15'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.15'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.0'
97
+ description: |2
98
+ Generate your keys in code files of translation.
99
+
100
+ Manage your keys automatically without pains.
101
+ email:
102
+ - gameboy0824@126.com
103
+ executables:
104
+ - traduki
105
+ extensions: []
106
+ extra_rdoc_files: []
107
+ files:
108
+ - ".gitignore"
109
+ - ".rspec"
110
+ - ".rubocop.yml"
111
+ - ".travis.yml"
112
+ - CODE_OF_CONDUCT.md
113
+ - Gemfile
114
+ - LICENSE.txt
115
+ - README.md
116
+ - Rakefile
117
+ - bin/setup
118
+ - bin/traduki
119
+ - lib/traduki.rb
120
+ - lib/traduki/cli.rb
121
+ - lib/traduki/config.rb
122
+ - lib/traduki/generator.rb
123
+ - lib/traduki/hash_tool.rb
124
+ - lib/traduki/parser.rb
125
+ - lib/traduki/reader.rb
126
+ - lib/traduki/translation.rb
127
+ - lib/traduki/version.rb
128
+ - traduki.gemspec
129
+ homepage: https://github.com/vergil-choi/traduki
130
+ licenses:
131
+ - MIT
132
+ metadata: {}
133
+ post_install_message:
134
+ rdoc_options: []
135
+ require_paths:
136
+ - lib
137
+ required_ruby_version: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - ">="
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ required_rubygems_version: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - ">="
145
+ - !ruby/object:Gem::Version
146
+ version: '0'
147
+ requirements: []
148
+ rubyforge_project:
149
+ rubygems_version: 2.6.11
150
+ signing_key:
151
+ specification_version: 4
152
+ summary: Manage your language translations with no pains.
153
+ test_files: []