Last active
August 16, 2016 12:23
-
-
Save lime/ae61cb2bcc68d35aaf137c137610bbac to your computer and use it in GitHub Desktop.
Generate .phraseapp.yml for multiple locale files in Rails
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
phraseapp: | |
project_id: YOUR-PROJECT-ID | |
file_format: yml | |
push: | |
sources: | |
- file: config/locales/activerecord.<locale_name>.yml | |
params: | |
tags: activerecord | |
update_translations: true | |
skip_upload_tags: true | |
- file: config/locales/admin/foo.<locale_name>.yml | |
params: | |
tags: admin-foo | |
update_translations: true | |
skip_upload_tags: true | |
- file: config/locales/bar.<locale_name>.yml | |
params: | |
tags: bar | |
update_translations: true | |
skip_upload_tags: true | |
pull: | |
targets: | |
- file: config/locales/activerecord.<locale_name>.yml | |
params: | |
tag: activerecord | |
- file: config/locales/admin/foo.<locale_name>.yml | |
params: | |
tag: admin-foo | |
- file: config/locales/bar.<locale_name>.yml | |
params: | |
tag: bar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'yaml' | |
require 'active_support/core_ext/hash/keys' | |
require 'active_support/core_ext/string/inflections' | |
tag_regex = Regexp.new('config/locales/(?<path>.+)\.\w+\.yml') | |
locale_regex = Regexp.new('(?<=.)*\w+(?=\.yml)') | |
tag_blacklist = ['rails'] | |
files_and_tags = Dir.glob('config/locales/**/*.yml').map do |file| | |
file_with_placeholder = file.sub(locale_regex, '<locale_name>') | |
tag = file[tag_regex, :path].parameterize.dasherize | |
[file_with_placeholder, tag] | |
end.reject { |file, tag| tag_blacklist.include?(tag) }.uniq | |
config = { | |
phraseapp: { | |
project_id: 'YOUR-PROJECT-ID', | |
file_format: 'yml', | |
push: { | |
sources: files_and_tags.map do |file, tag| | |
{ | |
file: file, | |
params: { | |
tags: tag, | |
update_translations: true, | |
skip_upload_tags: true | |
} | |
} | |
end | |
}, | |
pull: { | |
targets: files_and_tags.map do |file, tag| | |
{ | |
file: file, | |
params: { | |
tag: tag | |
} | |
} | |
end | |
} | |
} | |
} | |
File.open('.phraseapp.yml', 'w') do |file| | |
YAML.dump(config.deep_stringify_keys, file) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment