Class: Translatomatic::FileTranslator
- Inherits:
-
Object
- Object
- Translatomatic::FileTranslator
- Includes:
- Util
- Defined in:
- lib/translatomatic/file_translator.rb
Overview
The file translator ties together functionality from translators, resource files, and the database to convert files from one language to another.
Instance Attribute Summary collapse
-
#db_translations ⇒ Array<Translatomatic::Model::Text>
readonly
A list of translations saved to the database.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ FileTranslator
constructor
Create a converter to translate files.
-
#stats ⇒ Translatomatic::TranslationStats
Translation statistics.
-
#translate(file, to_locale) ⇒ Translatomatic::ResourceFile
Translate properties of source_file to the target locale.
-
#translate_to_file(source, to_locale) ⇒ Translatomatic::ResourceFile
Translates a resource file and writes results to a target resource file.
Constructor Details
#initialize(options = {}) ⇒ FileTranslator
Create a converter to translate files
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/translatomatic/file_translator.rb', line 12 def initialize( = {}) @dry_run = [:dry_run] @listener = [:listener] @translators = Translatomatic::Translator.resolve([:translator], ) raise t("file_translator.translator_required") if @translators.empty? @translators.each { |i| i.listener = @listener } if @listener # use database by default if we're connected to a database use_db = .include?(:use_database) ? [:use_database] : true @use_db = use_db && ActiveRecord::Base.connected? log.debug(t("file_translator.database_disabled")) unless @use_db @db_translations = [] @translations = {} # map of original text to Translation end |
Instance Attribute Details
#db_translations ⇒ Array<Translatomatic::Model::Text> (readonly)
Returns A list of translations saved to the database.
7 8 9 |
# File 'lib/translatomatic/file_translator.rb', line 7 def db_translations @db_translations end |
Instance Method Details
#stats ⇒ Translatomatic::TranslationStats
Returns Translation statistics.
29 30 31 |
# File 'lib/translatomatic/file_translator.rb', line 29 def stats @stats ||= Translatomatic::TranslationStats.new(@translations) end |
#translate(file, to_locale) ⇒ Translatomatic::ResourceFile
Translate properties of source_file to the target locale. Does not write changes to disk.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/translatomatic/file_translator.rb', line 39 def translate(file, to_locale) file = resource_file(file) to_locale = parse_locale(to_locale) # do nothing if target language is the same as source language return file if file.locale.language == to_locale.language result = Translatomatic::TranslationResult.new(file, to_locale) # translate using strings from the database first each_translator(result) { translate_properties_with_db(result) } # send remaining unknown strings to translator each_translator(result) { translate_properties_with_translator(result) } log.debug(t("file_translator.stats", from_db: stats.from_db, from_translator: stats.from_translator, untranslated: result.untranslated.length)) @listener.untranslated_texts(result.untranslated) if @listener file.properties = result.properties file.locale = to_locale file end |
#translate_to_file(source, to_locale) ⇒ Translatomatic::ResourceFile
Translates a resource file and writes results to a target resource file. The path of the target resource file is automatically determined.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/translatomatic/file_translator.rb', line 69 def translate_to_file(source, to_locale) # Automatically determines the target filename based on target locale. source = resource_file(source) target = Translatomatic::ResourceFile.load(source.path) target.path = source.locale_path(to_locale) log.info(t("file_translator.translating", source: source, source_locale: source.locale, target: target, target_locale: to_locale)) translate(target, to_locale) unless @dry_run target.path.parent.mkpath target.save end target end |