diff options
author | Sebastian Celis <sebastian@sebastiancelis.com> | 2016-04-03 21:06:54 +0300 |
---|---|---|
committer | Sebastian Celis <sebastian@sebastiancelis.com> | 2016-04-03 21:06:54 +0300 |
commit | 1e1b73fda2d570e325ac5db3de501935964fb931 (patch) | |
tree | 5af2eb523b044104336fb0621debf47fffd8a1ee /lib/twine | |
parent | 035afe3df3607682307bfdbf223e85a9520c42f4 (diff) | |
parent | ce5c9a182851a86a102877af448eb7c889c9615d (diff) |
Merge pull request #147 from sebastianludwig/naming
Better naming
Diffstat (limited to 'lib/twine')
-rw-r--r-- | lib/twine/cli.rb | 121 | ||||
-rw-r--r-- | lib/twine/formatters/abstract.rb | 84 | ||||
-rw-r--r-- | lib/twine/formatters/android.rb | 10 | ||||
-rw-r--r-- | lib/twine/formatters/apple.rb | 4 | ||||
-rw-r--r-- | lib/twine/formatters/django.rb | 14 | ||||
-rw-r--r-- | lib/twine/formatters/flash.rb | 6 | ||||
-rw-r--r-- | lib/twine/formatters/gettext.rb | 20 | ||||
-rw-r--r-- | lib/twine/formatters/jquery.rb | 12 | ||||
-rw-r--r-- | lib/twine/formatters/tizen.rb | 6 | ||||
-rw-r--r-- | lib/twine/output_processor.rb | 30 | ||||
-rw-r--r-- | lib/twine/runner.rb | 116 | ||||
-rw-r--r-- | lib/twine/twine_file.rb (renamed from lib/twine/stringsfile.rb) | 88 |
12 files changed, 262 insertions, 249 deletions
diff --git a/lib/twine/cli.rb b/lib/twine/cli.rb index 9705a06..295922d 100644 --- a/lib/twine/cli.rb +++ b/lib/twine/cli.rb @@ -3,57 +3,57 @@ require 'optparse' module Twine module CLI NEEDED_COMMAND_ARGUMENTS = { - 'generate-string-file' => 3, - 'generate-all-string-files' => 3, - 'consume-string-file' => 3, - 'consume-all-string-files' => 3, + 'generate-localization-file' => 3, + 'generate-all-localization-files' => 3, + 'consume-localization-file' => 3, + 'consume-all-localization-files' => 3, 'generate-loc-drop' => 3, 'consume-loc-drop' => 3, - 'validate-strings-file' => 2 + 'validate-twine-file' => 2 } def self.parse(args) options = { include: :all } parser = OptionParser.new do |opts| - opts.banner = 'Usage: twine COMMAND STRINGS_FILE [INPUT_OR_OUTPUT_PATH] [--lang LANG1,LANG2...] [--tags TAG1,TAG2,TAG3...] [--format FORMAT]' + opts.banner = 'Usage: twine COMMAND TWINE_FILE [INPUT_OR_OUTPUT_PATH] [--lang LANG1,LANG2...] [--tags TAG1,TAG2,TAG3...] [--format FORMAT]' opts.separator '' - opts.separator 'The purpose of this script is to convert back and forth between multiple data formats, allowing us to treat our strings (and translations) as data stored in a text file. We can then use the data file to create drops for the localization team, consume similar drops returned by the localization team, and create formatted string files to ship with your products.' + opts.separator 'The purpose of this script is to convert back and forth between multiple data formats, allowing us to treat our strings (and translations) as data stored in a text file. We can then use the data file to create drops for the localization team, consume similar drops returned by the localization team, and create formatted localization files to ship with your products.' opts.separator '' opts.separator 'Commands:' opts.separator '' - opts.separator '- generate-string-file' - opts.separator ' Generates a string file in a certain LANGUAGE given a particular FORMAT. This script will attempt to guess both the language and the format given the filename and extension. For example, "ko.xml" will generate a Korean language file for Android.' + opts.separator '- generate-localization-file' + opts.separator ' Generates a localization file in a certain LANGUAGE given a particular FORMAT. This script will attempt to guess both the language and the format given the filename and extension. For example, "ko.xml" will generate a Korean language file for Android.' opts.separator '' - opts.separator '- generate-all-string-files' - opts.separator ' Generates all the string files necessary for a given project. The parent directory to all of the locale-specific directories in your project should be specified as the INPUT_OR_OUTPUT_PATH. This command will most often be executed by your build script so that each build always contains the most recent strings.' + opts.separator '- generate-all-localization-files' + opts.separator ' Generates all the localization files necessary for a given project. The parent directory to all of the locale-specific directories in your project should be specified as the INPUT_OR_OUTPUT_PATH. This command will most often be executed by your build script so that each build always contains the most recent translations.' opts.separator '' - opts.separator '- consume-string-file' - opts.separator ' Slurps all of the strings from a translated strings file into the specified STRINGS_FILE. If you have some files returned to you by your translators you can use this command to incorporate all of their changes. This script will attempt to guess both the language and the format given the filename and extension. For example, "ja.strings" will assume that the file is a Japanese iOS strings file.' + opts.separator '- consume-localization-file' + opts.separator ' Slurps all of the translations from a localization file into the specified TWINE_FILE. If you have some files returned to you by your translators you can use this command to incorporate all of their changes. This script will attempt to guess both the language and the format given the filename and extension. For example, "ja.strings" will assume that the file is a Japanese iOS strings file.' opts.separator '' - opts.separator '- consume-all-string-files' - opts.separator ' Slurps all of the strings from a directory into the specified STRINGS_FILE. If you have some files returned to you by your translators you can use this command to incorporate all of their changes. This script will attempt to guess both the language and the format given the filename and extension. For example, "ja.strings" will assume that the file is a Japanese iOS strings file.' + opts.separator '- consume-all-localization-files' + opts.separator ' Slurps all of the translations from a directory into the specified TWINE_FILE. If you have some files returned to you by your translators you can use this command to incorporate all of their changes. This script will attempt to guess both the language and the format given the filename and extension. For example, "ja.strings" will assume that the file is a Japanese iOS strings file.' opts.separator '' opts.separator '- generate-loc-drop' - opts.separator ' Generates a zip archive of strings files in any format. The purpose of this command is to create a very simple archive that can be handed off to a translation team. The translation team can unzip the archive, translate all of the strings in the archived files, zip everything back up, and then hand that final archive back to be consumed by the consume-loc-drop command.' + opts.separator ' Generates a zip archive of localization files in a given format. The purpose of this command is to create a very simple archive that can be handed off to a translation team. The translation team can unzip the archive, translate all of the strings in the archived files, zip everything back up, and then hand that final archive back to be consumed by the consume-loc-drop command.' opts.separator '' opts.separator '- consume-loc-drop' opts.separator ' Consumes an archive of translated files. This archive should be in the same format as the one created by the generate-loc-drop command.' opts.separator '' - opts.separator '- validate-strings-file' - opts.separator ' Validates that the given strings file is parseable, contains no duplicates, and that every string has a tag. Exits with a non-zero exit code if those criteria are not met.' + opts.separator '- validate-twine-file' + opts.separator ' Validates that the given Twine file is parseable, contains no duplicates, and that no key contains invalid characters. Exits with a non-zero exit code if those criteria are not met.' opts.separator '' opts.separator 'General Options:' opts.separator '' opts.on('-l', '--lang LANGUAGES', Array, 'The language code(s) to use for the specified action.') do |l| options[:languages] = l end - opts.on('-t', '--tags TAG1,TAG2,TAG3', Array, 'The tag(s) to use for the specified action. Only strings with that tag will be processed. Omit this option to match', - ' all strings in the strings data file.') do |t| + opts.on('-t', '--tags TAG1,TAG2,TAG3', Array, 'The tag(s) to use for the specified action. Only definitions with that tag will be processed. Omit this option to match', + ' all definitions in the Twine data file.') do |t| options[:tags] = t end opts.on('-u', '--[no-]untagged', 'If you have specified tags using the --tags flag, then only those tags will be selected. If you also want to select', - ' all strings that are untagged, then you can specify this option to do so.') do |u| + ' all definitions that are untagged, then you can specify this option to do so.') do |u| options[:untagged] = u end formats = Formatters.formatters.map(&:format_name).map(&:downcase) @@ -61,36 +61,36 @@ module Twine " Additional formatters can be placed in the formats/ directory.") do |f| options[:format] = f end - opts.on('-a', '--[no-]consume-all', 'Normally, when consuming a string file, Twine will ignore any string keys that do not exist in your master file.') do |a| + opts.on('-a', '--[no-]consume-all', 'Normally, when consuming a localization file, Twine will ignore any translation keys that do not exist in your Twine file.') do |a| options[:consume_all] = true end opts.on('-i', '--include SET', [:all, :translated, :untranslated], - "This flag will determine which strings are included when generating strings files. It's possible values:", - " all: All strings both translated and untranslated for the specified language are included. This is the default value.", - " translated: Only translated strings are included.", - " untranslated: Only untranslated strings are included.") do |i| + "This flag will determine which definitions are included when generating localization files. It's possible values are:", + " all: All definitions both translated and untranslated for the specified language are included. This is the default value.", + " translated: Only definitions with translation for the specified language are included.", + " untranslated: Only definitions without translation for the specified language are included.") do |i| options[:include] = i end - opts.on('-o', '--output-file OUTPUT_FILE', 'Write the new strings database to this file instead of replacing the original file. This flag is only useful when', - ' running the consume-string-file or consume-loc-drop commands.') do |o| + opts.on('-o', '--output-file OUTPUT_FILE', 'Write a new Twine file at this location instead of replacing the original file. This flag is only useful when', + ' running the consume-localization-file or consume-loc-drop commands.') do |o| options[:output_path] = o end - opts.on('-n', '--file-name FILE_NAME', 'When running the generate-all-string-files command, this flag may be used to overwrite the default file name of the format.') do |n| + opts.on('-n', '--file-name FILE_NAME', 'When running the generate-all-localization-files command, this flag may be used to overwrite the default file name of the format.') do |n| options[:file_name] = n end - opts.on('-r', '--[no-]create-folders', "When running the generate-all-string-files command, this flag may be used to create output folders for all languages,", + opts.on('-r', '--[no-]create-folders', "When running the generate-all-localization-files command, this flag may be used to create output folders for all languages,", " if they don't exist yet. As a result all languages will be exported, not only the ones where an output folder already", " exists.") do |r| options[:create_folders] = r end - opts.on('-d', '--developer-language LANG', 'When writing the strings data file, set the specified language as the "developer language". In practice, this just', - ' means that this language will appear first in the strings data file. When generating files this language will be', - ' used as default language and its translations will be used if a key is not localized for the output language.') do |d| + opts.on('-d', '--developer-language LANG', 'When writing the Twine data file, set the specified language as the "developer language". In practice, this just', + ' means that this language will appear first in the Twine data file. When generating files this language will be', + ' used as default language and its translations will be used if a definition is not localized for the output language.') do |d| options[:developer_language] = d end - opts.on('-c', '--[no-]consume-comments', 'Normally, when consuming a string file, Twine will ignore all comments in the file. With this flag set, any comments', - ' encountered will be read and parsed into the strings data file. This is especially useful when creating your first', - ' strings data file from an existing project.') do |c| + opts.on('-c', '--[no-]consume-comments', 'Normally, when consuming a localization file, Twine will ignore all comments in the file. With this flag set, any comments', + ' encountered will be read and parsed into the Twine data file. This is especially useful when creating your first', + ' Twine data file from an existing project.') do |c| options[:consume_comments] = c end opts.on('-e', '--encoding ENCODING', 'Twine defaults to encoding all output files in UTF-8. This flag will tell Twine to use an alternate encoding for these', @@ -99,10 +99,10 @@ module Twine ' UTF-16LE or UTF16-BE.') do |e| options[:output_encoding] = e end - opts.on('--[no-]validate', 'Validate the strings file before formatting it.') do |validate| + opts.on('--[no-]validate', 'Validate the Twine file before formatting it.') do |validate| options[:validate] = validate end - opts.on('-p', '--[no-]pedantic', 'When validating a strings file, perform additional checks that go beyond pure validity (like presence of tags).') do |p| + opts.on('-p', '--[no-]pedantic', 'When validating a Twine file, perform additional checks that go beyond pure validity (like presence of tags).') do |p| options[:pedantic] = p end opts.on('-h', '--help', 'Show this message.') do |h| @@ -116,13 +116,13 @@ module Twine opts.separator '' opts.separator 'Examples:' opts.separator '' - opts.separator '> twine generate-string-file strings.txt ko.xml --tags FT' - opts.separator '> twine generate-all-string-files strings.txt Resources/Locales/ --tags FT,FB' - opts.separator '> twine consume-string-file strings.txt ja.strings' - opts.separator '> twine consume-all-string-files strings.txt Resources/Locales/ --developer-language en --tags DefaultTag1,DefaultTag2' - opts.separator '> twine generate-loc-drop strings.txt LocDrop5.zip --tags FT,FB --format android --lang de,en,en-GB,ja,ko' - opts.separator '> twine consume-loc-drop strings.txt LocDrop5.zip' - opts.separator '> twine validate-strings-file strings.txt' + opts.separator '> twine generate-localization-file twine.txt ko.xml --tags FT' + opts.separator '> twine generate-all-localization-files twine.txt Resources/Locales/ --tags FT,FB' + opts.separator '> twine consume-localization-file twine.txt ja.strings' + opts.separator '> twine consume-all-localization-files twine.txt Resources/Locales/ --developer-language en --tags DefaultTag1,DefaultTag2' + opts.separator '> twine generate-loc-drop twine.txt LocDrop5.zip --tags FT,FB --format android --lang de,en,en-GB,ja,ko' + opts.separator '> twine consume-loc-drop twine.txt LocDrop5.zip' + opts.separator '> twine validate-twine-file twine.txt' end begin parser.parse! args @@ -136,6 +136,19 @@ module Twine exit false end + # TODO: Remove this mapping of deprecated commands some time in the future - added on 31.03. + deprecated_command_mappings = { + 'generate-string-file' => 'generate-localization-file', + 'generate-all-string-file' => 'generate-all-localization-files', + 'consume-string-file' => 'consume-localization-file', + 'consume-all-string-files' => 'consume-all-localization-files' + } + mapped_command = deprecated_command_mappings[args[0]] + if mapped_command + Twine::stderr.puts "WARNING: Twine commands names have changed. `#{args[0]}` is now `#{mapped_command}`. The old command is deprecated will soon stop working. For more information please check the documentation at https://github.com/mobiata/twine" + args[0] = mapped_command + end + number_of_needed_arguments = NEEDED_COMMAND_ARGUMENTS[args[0]] unless number_of_needed_arguments raise Twine::Error.new "Invalid command: #{args[0]}" @@ -143,9 +156,9 @@ module Twine options[:command] = args[0] if args.length < 2 - raise Twine::Error.new 'You must specify your strings file.' + raise Twine::Error.new 'You must specify your twine file.' end - options[:strings_file] = args[1] + options[:twine_file] = args[1] if args.length < number_of_needed_arguments raise Twine::Error.new 'Not enough arguments.' @@ -154,19 +167,19 @@ module Twine end case options[:command] - when 'generate-string-file' + when 'generate-localization-file' options[:output_path] = args[2] if options[:languages] and options[:languages].length > 1 - raise Twine::Error.new 'Please only specify a single language for the generate-string-file command.' + raise Twine::Error.new 'Please only specify a single language for the generate-localization-file command.' end - when 'generate-all-string-files' + when 'generate-all-localization-files' options[:output_path] = args[2] - when 'consume-string-file' + when 'consume-localization-file' options[:input_path] = args[2] if options[:languages] and options[:languages].length > 1 - raise Twine::Error.new 'Please only specify a single language for the consume-string-file command.' + raise Twine::Error.new 'Please only specify a single language for the consume-localization-file command.' end - when 'consume-all-string-files' + when 'consume-all-localization-files' options[:input_path] = args[2] when 'generate-loc-drop' options[:output_path] = args[2] @@ -175,7 +188,7 @@ module Twine end when 'consume-loc-drop' options[:input_path] = args[2] - when 'validate-strings-file' + when 'validate-twine-file' end return options diff --git a/lib/twine/formatters/abstract.rb b/lib/twine/formatters/abstract.rb index 012346e..c337a60 100644 --- a/lib/twine/formatters/abstract.rb +++ b/lib/twine/formatters/abstract.rb @@ -3,11 +3,11 @@ require 'fileutils' module Twine module Formatters class Abstract - attr_accessor :strings + attr_accessor :twine_file attr_accessor :options def initialize - @strings = StringsFile.new + @twine_file = TwineFile.new @options = {} end @@ -30,47 +30,47 @@ module Twine def set_translation_for_key(key, lang, value) value = value.gsub("\n", "\\n") - if @strings.strings_map.include?(key) - row = @strings.strings_map[key] - reference = @strings.strings_map[row.reference_key] if row.reference_key + if @twine_file.definitions_by_key.include?(key) + definition = @twine_file.definitions_by_key[key] + reference = @twine_file.definitions_by_key[definition.reference_key] if definition.reference_key if !reference or value != reference.translations[lang] - row.translations[lang] = value + definition.translations[lang] = value end elsif @options[:consume_all] - Twine::stderr.puts "Adding new string '#{key}' to strings data file." - current_section = @strings.sections.find { |s| s.name == 'Uncategorized' } + Twine::stderr.puts "Adding new definition '#{key}' to twine file." + current_section = @twine_file.sections.find { |s| s.name == 'Uncategorized' } unless current_section - current_section = StringsSection.new('Uncategorized') - @strings.sections.insert(0, current_section) + current_section = TwineSection.new('Uncategorized') + @twine_file.sections.insert(0, current_section) end - current_row = StringsRow.new(key) - current_section.rows << current_row + current_definition = TwineDefinition.new(key) + current_section.definitions << current_definition if @options[:tags] && @options[:tags].length > 0 - current_row.tags = @options[:tags] + current_definition.tags = @options[:tags] end - @strings.strings_map[key] = current_row - @strings.strings_map[key].translations[lang] = value + @twine_file.definitions_by_key[key] = current_definition + @twine_file.definitions_by_key[key].translations[lang] = value else - Twine::stderr.puts "Warning: '#{key}' not found in strings data file." + Twine::stderr.puts "Warning: '#{key}' not found in twine file." end - if !@strings.language_codes.include?(lang) - @strings.add_language_code(lang) + if !@twine_file.language_codes.include?(lang) + @twine_file.add_language_code(lang) end end def set_comment_for_key(key, comment) return unless @options[:consume_comments] - if @strings.strings_map.include?(key) - row = @strings.strings_map[key] + if @twine_file.definitions_by_key.include?(key) + definition = @twine_file.definitions_by_key[key] - reference = @strings.strings_map[row.reference_key] if row.reference_key + reference = @twine_file.definitions_by_key[definition.reference_key] if definition.reference_key if !reference or comment != reference.raw_comment - row.comment = comment + definition.comment = comment end end end @@ -88,35 +88,35 @@ module Twine end def format_file(lang) - output_processor = Processors::OutputProcessor.new(@strings, @options) - processed_strings = output_processor.process(lang) + output_processor = Processors::OutputProcessor.new(@twine_file, @options) + processed_twine_file = output_processor.process(lang) - return nil if processed_strings.strings_map.empty? + return nil if processed_twine_file.definitions_by_key.empty? header = format_header(lang) result = "" result += header + "\n" if header - result += format_sections(processed_strings, lang) + result += format_sections(processed_twine_file, lang) end def format_header(lang) end - def format_sections(strings, lang) - sections = strings.sections.map { |section| format_section(section, lang) } + def format_sections(twine_file, lang) + sections = twine_file.sections.map { |section| format_section(section, lang) } sections.compact.join("\n") end def format_section_header(section) end - def should_include_row(row, lang) - row.translated_string_for_lang(lang) + def should_include_definition(definition, lang) + return !definition.translation_for_lang(lang).nil? end def format_section(section, lang) - rows = section.rows.select { |row| should_include_row(row, lang) } - return if rows.empty? + definitions = section.definitions.select { |definition| should_include_definition(definition, lang) } + return if definitions.empty? result = "" @@ -125,22 +125,22 @@ module Twine result += "\n#{section_header}" if section_header end - rows.map! { |row| format_row(row, lang) } - rows.compact! # remove nil entries - rows.map! { |row| "\n#{row}" } # prepend newline - result += rows.join + definitions.map! { |definition| format_definition(definition, lang) } + definitions.compact! # remove nil definitions + definitions.map! { |definition| "\n#{definition}" } # prepend newline + result += definitions.join end - def format_row(row, lang) - [format_comment(row, lang), format_key_value(row, lang)].compact.join + def format_definition(definition, lang) + [format_comment(definition, lang), format_key_value(definition, lang)].compact.join end - def format_comment(row, lang) + def format_comment(definition, lang) end - def format_key_value(row, lang) - value = row.translated_string_for_lang(lang) - key_value_pattern % { key: format_key(row.key.dup), value: format_value(value.dup) } + def format_key_value(definition, lang) + value = definition.translation_for_lang(lang) + key_value_pattern % { key: format_key(definition.key.dup), value: format_value(value.dup) } end def key_value_pattern diff --git a/lib/twine/formatters/android.rb b/lib/twine/formatters/android.rb index 448826d..caddec8 100644 --- a/lib/twine/formatters/android.rb +++ b/lib/twine/formatters/android.rb @@ -37,7 +37,7 @@ module Twine path_arr = path.split(File::SEPARATOR) path_arr.each do |segment| if segment == 'values' - return @strings.language_codes[0] + return @twine_file.language_codes[0] else # The language is defined by a two-letter ISO 639-1 language code, optionally followed by a two letter ISO 3166-1-alpha-2 region code (preceded by lowercase "r"). # see http://developer.android.com/guide/topics/resources/providing-resources.html#AlternativeResources @@ -105,7 +105,7 @@ module Twine "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<!-- Android Strings File -->\n<!-- Generated by Twine #{Twine::VERSION} -->\n<!-- Language: #{lang} -->" end - def format_sections(strings, lang) + def format_sections(twine_file, lang) result = '<resources>' result += super + "\n" @@ -117,8 +117,8 @@ module Twine "\t<!-- SECTION: #{section.name} -->" end - def format_comment(row, lang) - "\t<!-- #{row.comment.gsub('--', '—')} -->\n" if row.comment + def format_comment(definition, lang) + "\t<!-- #{definition.comment.gsub('--', '—')} -->\n" if definition.comment end def key_value_pattern @@ -137,7 +137,7 @@ module Twine # 4) escape non resource identifier @ signs (http://developer.android.com/guide/topics/resources/accessing-resources.html#ResourcesFromXml) resource_identifier_regex = /@(?!([a-z\.]+:)?[a-z+]+\/[a-zA-Z_]+)/ # @[<package_name>:]<resource_type>/<resource_name> value.gsub!(resource_identifier_regex, '\@') - # 5) replace beginning and end spaces with \0020. Otherwise Android strips them. + # 5) replace beginning and end spaces with \u0020. Otherwise Android strips them. value.gsub(/\A *| *\z/) { |spaces| '\u0020' * spaces.length } end diff --git a/lib/twine/formatters/apple.rb b/lib/twine/formatters/apple.rb index 9546e2b..056ada7 100644 --- a/lib/twine/formatters/apple.rb +++ b/lib/twine/formatters/apple.rb @@ -73,8 +73,8 @@ module Twine "\"%{key}\" = \"%{value}\";\n" end - def format_comment(row, lang) - "/* #{row.comment.gsub('*/', '* /')} */\n" if row.comment + def format_comment(definition, lang) + "/* #{definition.comment.gsub('*/', '* /')} */\n" if definition.comment end def format_key(key) diff --git a/lib/twine/formatters/django.rb b/lib/twine/formatters/django.rb index b63657e..dd60c4f 100644 --- a/lib/twine/formatters/django.rb +++ b/lib/twine/formatters/django.rb @@ -63,7 +63,7 @@ module Twine end def format_file(lang) - @default_lang = @strings.language_codes[0] + @default_lang = @twine_file.language_codes[0] result = super @default_lang = nil result @@ -77,12 +77,12 @@ module Twine "#--------- #{section.name} ---------#\n" end - def format_row(row, lang) - [format_comment(row, lang), format_base_translation(row), format_key_value(row, lang)].compact.join + def format_definition(definition, lang) + [format_comment(definition, lang), format_base_translation(definition), format_key_value(definition, lang)].compact.join end - def format_base_translation(row) - base_translation = row.translations[@default_lang] + def format_base_translation(definition) + base_translation = definition.translations[@default_lang] "# base translation: \"#{base_translation}\"\n" if base_translation end @@ -91,8 +91,8 @@ module Twine "msgstr \"%{value}\"\n" end - def format_comment(row, lang) - "#. #{escape_quotes(row.comment)}\n" if row.comment + def format_comment(definition, lang) + "#. #{escape_quotes(definition.comment)}\n" if definition.comment end def format_key(key) diff --git a/lib/twine/formatters/flash.rb b/lib/twine/formatters/flash.rb index 8cc29da..3aaee70 100644 --- a/lib/twine/formatters/flash.rb +++ b/lib/twine/formatters/flash.rb @@ -44,7 +44,7 @@ module Twine end end - def format_sections(strings, lang) + def format_sections(twine_file, lang) super + "\n" end @@ -56,8 +56,8 @@ module Twine "## #{section.name} ##\n" end - def format_comment(row, lang) - "# #{row.comment}\n" if row.comment + def format_comment(definition, lang) + "# #{definition.comment}\n" if definition.comment end def key_value_pattern diff --git a/lib/twine/formatters/gettext.rb b/lib/twine/formatters/gettext.rb index 71b9a84..90553bd 100644 --- a/lib/twine/formatters/gettext.rb +++ b/lib/twine/formatters/gettext.rb @@ -64,7 +64,7 @@ module Twine end def format_file(lang) - @default_lang = strings.language_codes[0] + @default_lang = twine_file.language_codes[0] result = super @default_lang = nil result @@ -78,25 +78,25 @@ module Twine "# SECTION: #{section.name}" end - def should_include_row(row, lang) - super and row.translated_string_for_lang(@default_lang) + def should_include_definition(definition, lang) + super and !definition.translation_for_lang(@default_lang).nil? end - def format_comment(row, lang) - "#. \"#{escape_quotes(row.comment)}\"\n" if row.comment + def format_comment(definition, lang) + "#. \"#{escape_quotes(definition.comment)}\"\n" if definition.comment end - def format_key_value(row, lang) - value = row.translated_string_for_lang(lang) - [format_key(row.key.dup), format_base_translation(row), format_value(value.dup)].compact.join + def format_key_value(definition, lang) + value = definition.translation_for_lang(lang) + [format_key(definition.key.dup), format_base_translation(definition), format_value(value.dup)].compact.join end def format_key(key) "msgctxt \"#{key}\"\n" end - def format_base_translation(row) - "msgid \"#{row.translations[@default_lang]}\"\n" + def format_base_translation(definition) + "msgid \"#{definition.translations[@default_lang]}\"\n" end def format_value(value) diff --git a/lib/twine/formatters/jquery.rb b/lib/twine/formatters/jquery.rb index 310f041..8d60116 100644 --- a/lib/twine/formatters/jquery.rb +++ b/lib/twine/formatters/jquery.rb @@ -48,8 +48,8 @@ module Twine "{\n#{super}\n}\n" end - def format_sections(strings, lang) - sections = strings.sections.map { |section| format_section(section, lang) } + def format_sections(twine_file, lang) + sections = twine_file.sections.map { |section| format_section(section, lang) } sections.join(",\n\n") end @@ -57,11 +57,11 @@ module Twine end def format_section(section, lang) - rows = section.rows.dup + definitions = section.definitions.dup - rows.map! { |row| format_row(row, lang) } - rows.compact! # remove nil entries - rows.join(",\n") + definitions.map! { |definition| format_definition(definition, lang) } + definitions.compact! # remove nil definitions + definitions.join(",\n") end def key_value_pattern diff --git a/lib/twine/formatters/tizen.rb b/lib/twine/formatters/tizen.rb index 8c9fae8..21ba771 100644 --- a/lib/twine/formatters/tizen.rb +++ b/lib/twine/formatters/tizen.rb @@ -94,7 +94,7 @@ module Twine "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<!-- Tizen Strings File -->\n<!-- Generated by Twine #{Twine::VERSION} -->\n<!-- Language: #{lang} -->" end - def format_sections(strings, lang) + def format_sections(twine_file, lang) result = '<string_table Bversion="2.0.0.201311071819" Dversion="20120315">' result += super + "\n" @@ -106,8 +106,8 @@ module Twine "\t<!-- SECTION: #{section.name} -->" end - def format_comment(row, lang) - "\t<!-- #{row.comment.gsub('--', '—')} -->\n" if row.comment + def format_comment(definition, lang) + "\t<!-- #{definition.comment.gsub('--', '—')} -->\n" if definition.comment end def key_value_pattern diff --git a/lib/twine/output_processor.rb b/lib/twine/output_processor.rb index 405e37a..8924ad1 100644 --- a/lib/twine/output_processor.rb +++ b/lib/twine/output_processor.rb @@ -2,13 +2,13 @@ module Twine module Processors class OutputProcessor - def initialize(strings, options) - @strings = strings + def initialize(twine_file, options) + @twine_file = twine_file @options = options end def default_language - @options[:developer_language] || @strings.language_codes[0] + @options[:developer_language] || @twine_file.language_codes[0] end def fallback_languages(language) @@ -20,30 +20,30 @@ module Twine end def process(language) - result = StringsFile.new + result = TwineFile.new - result.language_codes.concat @strings.language_codes - @strings.sections.each do |section| - new_section = StringsSection.new section.name + result.language_codes.concat @twine_file.language_codes + @twine_file.sections.each do |section| + new_section = TwineSection.new section.name - section.rows.each do |row| - next unless row.matches_tags?(@options[:tags], @options[:untagged]) + section.definitions.each do |definition| + next unless definition.matches_tags?(@options[:tags], @options[:untagged]) - value = row.translated_string_for_lang(language) + value = definition.translation_for_lang(language) next if value && @options[:include] == :untranslated if value.nil? && @options[:include] != :translated - value = row.translated_string_for_lang(fallback_languages(language)) + value = definition.translation_for_lang(fallback_languages(language)) end next unless value - new_row = row.dup - new_row.translations[language] = value + new_definition = definition.dup + new_definition.translations[language] = value - new_section.rows << new_row - result.strings_map[new_row.key] = new_row + new_section.definitions << new_definition + result.definitions_by_key[new_definition.key] = new_definition end result.sections << new_section diff --git a/lib/twine/runner.rb b/lib/twine/runner.rb index 92a6c71..3f14116 100644 --- a/lib/twine/runner.rb +++ b/lib/twine/runner.rb @@ -8,42 +8,42 @@ module Twine def self.run(args) options = CLI.parse(args) - strings = StringsFile.new - strings.read options[:strings_file] - runner = new(options, strings) + twine_file = TwineFile.new + twine_file.read options[:twine_file] + runner = new(options, twine_file) case options[:command] - when 'generate-string-file' - runner.generate_string_file - when 'generate-all-string-files' - runner.generate_all_string_files - when 'consume-string-file' - runner.consume_string_file - when 'consume-all-string-files' - runner.consume_all_string_files + when 'generate-localization-file' + runner.generate_localization_file + when 'generate-all-localization-files' + runner.generate_all_localization_files + when 'consume-localization-file' + runner.consume_localization_file + when 'consume-all-localization-files' + runner.consume_all_localization_files when 'generate-loc-drop' runner.generate_loc_drop when 'consume-loc-drop' runner.consume_loc_drop - when 'validate-strings-file' - runner.validate_strings_file + when 'validate-twine-file' + runner.validate_twine_file end end - def initialize(options = {}, strings = StringsFile.new) + def initialize(options = {}, twine_file = TwineFile.new) @options = options - @strings = strings + @twine_file = twine_file end - def write_strings_data(path) + def write_twine_data(path) if @options[:developer_language] - @strings.set_developer_language_code(@options[:developer_language]) + @twine_file.set_developer_language_code(@options[:developer_language]) end - @strings.write(path) + @twine_file.write(path) end - def generate_string_file - validate_strings_file if @options[:validate] + def generate_localization_file + validate_twine_file if @options[:validate] lang = nil lang = @options[:languages][0] if @options[:languages] @@ -51,13 +51,13 @@ module Twine formatter, lang = prepare_read_write(@options[:output_path], lang) output = formatter.format_file(lang) - raise Twine::Error.new "Nothing to generate! The resulting file would not contain any strings." unless output + raise Twine::Error.new "Nothing to generate! The resulting file would not contain any translations." unless output IO.write(@options[:output_path], output, encoding: encoding) end - def generate_all_string_files - validate_strings_file if @options[:validate] + def generate_all_localization_files + validate_twine_file if @options[:validate] if !File.directory?(@options[:output_path]) if @options[:create_folders] @@ -76,7 +76,7 @@ module Twine file_name = @options[:file_name] || formatter.default_file_name if @options[:create_folders] - @strings.language_codes.each do |lang| + @twine_file.language_codes.each do |lang| output_path = File.join(@options[:output_path], formatter.output_path_for_language(lang)) FileUtils.mkdir_p(output_path) @@ -85,7 +85,7 @@ module Twine output = formatter.format_file(lang) unless output - Twine::stderr.puts "Skipping file at path #{file_path} since it would not contain any strings." + Twine::stderr.puts "Skipping file at path #{file_path} since it would not contain any translations." next end @@ -107,7 +107,7 @@ module Twine file_path = File.join(output_path, file_name) output = formatter.format_file(lang) unless output - Twine::stderr.puts "Skipping file at path #{file_path} since it would not contain any strings." + Twine::stderr.puts "Skipping file at path #{file_path} since it would not contain any translations." next end @@ -121,18 +121,18 @@ module Twine end - def consume_string_file + def consume_localization_file lang = nil if @options[:languages] lang = @options[:languages][0] end - read_string_file(@options[:input_path], lang) - output_path = @options[:output_path] || @options[:strings_file] - write_strings_data(output_path) + read_localization_file(@options[:input_path], lang) + output_path = @options[:output_path] || @options[:twine_file] + write_twine_data(output_path) end - def consume_all_string_files + def consume_all_localization_files if !File.directory?(@options[:input_path]) raise Twine::Error.new("Directory does not exist: #{@options[:output_path]}") end @@ -140,19 +140,19 @@ module Twine Dir.glob(File.join(@options[:input_path], "**/*")) do |item| if File.file?(item) begin - read_string_file(item) + read_localization_file(item) rescue Twine::Error => e Twine::stderr.puts "#{e.message}" end end end - output_path = @options[:output_path] || @options[:strings_file] - write_strings_data(output_path) + output_path = @options[:output_path] || @options[:twine_file] + write_twine_data(output_path) end def generate_loc_drop - validate_strings_file if @options[:validate] + validate_twine_file if @options[:validate] require_rubyzip @@ -165,7 +165,7 @@ module Twine zipfile.mkdir('Locales') formatter = formatter_for_format(@options[:format]) - @strings.language_codes.each do |lang| + @twine_file.language_codes.each do |lang| if @options[:languages] == nil || @options[:languages].length == 0 || @options[:languages].include?(lang) file_name = lang + formatter.extension temp_path = File.join(temp_dir, file_name) @@ -173,7 +173,7 @@ module Twine output = formatter.format_file(lang) unless output - Twine::stderr.puts "Skipping file #{file_name} since it would not contain any strings." + Twine::stderr.puts "Skipping file #{file_name} since it would not contain any translations." next end @@ -201,7 +201,7 @@ module Twine FileUtils.mkdir_p(File.dirname(real_path)) zipfile.extract(entry.name, real_path) begin - read_string_file(real_path) + read_localization_file(real_path) rescue Twine::Error => e Twine::stderr.puts "#{e.message}" end @@ -209,28 +209,28 @@ module Twine end end - output_path = @options[:output_path] || @options[:strings_file] - write_strings_data(output_path) + output_path = @options[:output_path] || @options[:twine_file] + write_twine_data(output_path) end - def validate_strings_file - total_strings = 0 + def validate_twine_file + total_definitions = 0 all_keys = Set.new duplicate_keys = Set.new keys_without_tags = Set.new invalid_keys = Set.new valid_key_regex = /^[A-Za-z0-9_]+$/ - @strings.sections.each do |section| - section.rows.each do |row| - total_strings += 1 + @twine_file.sections.each do |section| + section.definitions.each do |definition| + total_definitions += 1 - duplicate_keys.add(row.key) if all_keys.include? row.key - all_keys.add(row.key) + duplicate_keys.add(definition.key) if all_keys.include? definition.key + all_keys.add(definition.key) - keys_without_tags.add(row.key) if row.tags == nil or row.tags.length == 0 + keys_without_tags.add(definition.key) if definition.tags == nil or definition.tags.length == 0 - invalid_keys << row.key unless row.key =~ valid_key_regex + invalid_keys << definition.key unless definition.key =~ valid_key_regex end end @@ -238,14 +238,14 @@ module Twine join_keys = lambda { |set| set.map { |k| " " + k }.join("\n") } unless duplicate_keys.empty? - errors << "Found duplicate string key(s):\n#{join_keys.call(duplicate_keys)}" + errors << "Found duplicate key(s):\n#{join_keys.call(duplicate_keys)}" end if @options[:pedantic] - if keys_without_tags.length == total_strings - errors << "None of your strings have tags." + if keys_without_tags.length == total_definitions + errors << "None of your definitions have tags." elsif keys_without_tags.length > 0 - errors << "Found strings without tags:\n#{join_keys.call(keys_without_tags)}" + errors << "Found definitions without tags:\n#{join_keys.call(keys_without_tags)}" end end @@ -255,7 +255,7 @@ module Twine raise Twine::Error.new errors.join("\n\n") unless errors.empty? - Twine::stdout.puts "#{@options[:strings_file]} is valid." + Twine::stdout.puts "#{@options[:twine_file]} is valid." end private @@ -274,7 +274,7 @@ module Twine def determine_language_given_path(path) code = File.basename(path, File.extname(path)) - return code if @strings.language_codes.include? code + return code if @twine_file.language_codes.include? code end def formatter_for_format(format) @@ -284,12 +284,12 @@ module Twine def find_formatter(&block) formatter = Formatters.formatters.find &block return nil unless formatter - formatter.strings = @strings + formatter.twine_file = @twine_file formatter.options = @options formatter end - def read_string_file(path, lang = nil) + def read_localization_file(path, lang = nil) unless File.file?(path) raise Twine::Error.new("File does not exist: #{path}") end @@ -317,7 +317,7 @@ module Twine raise Twine::Error.new "Unable to determine language for #{path}" end - @strings.language_codes << lang unless @strings.language_codes.include? lang + @twine_file.language_codes << lang unless @twine_file.language_codes.include? lang return formatter, lang end diff --git a/lib/twine/stringsfile.rb b/lib/twine/twine_file.rb index a494d62..b889828 100644 --- a/lib/twine/stringsfile.rb +++ b/lib/twine/twine_file.rb @@ -1,15 +1,5 @@ module Twine - class StringsSection - attr_reader :name - attr_reader :rows - - def initialize(name) - @name = name - @rows = [] - end - end - - class StringsRow + class TwineDefinition attr_reader :key attr_accessor :comment attr_accessor :tags @@ -37,7 +27,7 @@ module Twine # The user did not specify any tags. Everything passes. return true elsif @tags == nil - # This row has no tags. + # This definition has no tags. return reference ? reference.matches_tags?(tags, include_untagged) : include_untagged elsif @tags.empty? return include_untagged @@ -48,18 +38,28 @@ module Twine return false end - def translated_string_for_lang(lang) + def translation_for_lang(lang) translation = [lang].flatten.map { |l| @translations[l] }.first - translation = reference.translated_string_for_lang(lang) if translation.nil? && reference + translation = reference.translation_for_lang(lang) if translation.nil? && reference return translation end end - class StringsFile + class TwineSection + attr_reader :name + attr_reader :definitions + + def initialize(name) + @name = name + @definitions = [] + end + end + + class TwineFile attr_reader :sections - attr_reader :strings_map + attr_reader :definitions_by_key attr_reader :language_codes private @@ -73,7 +73,7 @@ module Twine def initialize @sections = [] - @strings_map = {} + @definitions_by_key = {} @language_codes = [] end @@ -102,7 +102,7 @@ module Twine File.open(path, 'r:UTF-8') do |f| line_num = 0 current_section = nil - current_row = nil + current_definition = nil while line = f.gets parsed = false line.strip! @@ -115,20 +115,20 @@ module Twine if line.length > 4 && line[0, 2] == '[[' match = /^\[\[(.+)\]\]$/.match(line) if match - current_section = StringsSection.new(match[1]) + current_section = TwineSection.new(match[1]) @sections << current_section parsed = true end elsif line.length > 2 && line[0, 1] == '[' key = match_key(line) if key - current_row = StringsRow.new(key) - @strings_map[current_row.key] = current_row + current_definition = TwineDefinition.new(key) + @definitions_by_key[current_definition.key] = current_definition if !current_section - current_section = StringsSection.new('') + current_section = TwineSection.new('') @sections << current_section end - current_section.rows << current_row + current_section.definitions << current_definition parsed = true end else @@ -141,16 +141,16 @@ module Twine case key when 'comment' - current_row.comment = value + current_definition.comment = value when 'tags' - current_row.tags = value.split(',') + current_definition.tags = value.split(',') when 'ref' - current_row.reference_key = value if value + current_definition.reference_key = value if value else if !@language_codes.include? key add_language_code(key) end - current_row.translations[key] = value + current_definition.translations[key] = value end parsed = true end @@ -163,9 +163,9 @@ module Twine end # resolve_references - @strings_map.each do |key, row| - next unless row.reference_key - row.reference = @strings_map[row.reference_key] + @definitions_by_key.each do |key, definition| + next unless definition.reference_key + definition.reference = @definitions_by_key[definition.reference_key] end end @@ -180,26 +180,26 @@ module Twine f.puts "[[#{section.name}]]" - section.rows.each do |row| - f.puts "\t[#{row.key}]" + section.definitions.each do |definition| + f.puts "\t[#{definition.key}]" - value = write_value(row, dev_lang, f) - if !value && !row.reference_key - puts "Warning: #{row.key} does not exist in developer language '#{dev_lang}'" + value = write_value(definition, dev_lang, f) + if !value && !definition.reference_key + puts "Warning: #{definition.key} does not exist in developer language '#{dev_lang}'" end - if row.reference_key - f.puts "\t\tref = #{row.reference_key}" + if definition.reference_key + f.puts "\t\tref = #{definition.reference_key}" end - if row.tags && row.tags.length > 0 - tag_str = row.tags.join(',') + if definition.tags && definition.tags.length > 0 + tag_str = definition.tags.join(',') f.puts "\t\ttags = #{tag_str}" end - if row.raw_comment and row.raw_comment.length > 0 - f.puts "\t\tcomment = #{row.raw_comment}" + if definition.raw_comment and definition.raw_comment.length > 0 + f.puts "\t\tcomment = #{definition.raw_comment}" end @language_codes[1..-1].each do |lang| - write_value(row, lang, f) + write_value(definition, lang, f) end end end @@ -208,8 +208,8 @@ module Twine private - def write_value(row, language, file) - value = row.translations[language] + def write_value(definition, language, file) + value = definition.translations[language] return nil unless value if value[0] == ' ' || value[-1] == ' ' || (value[0] == '`' && value[-1] == '`') |