# encoding: UTF-8 #============================================================================== # LonaCraft localization adapter for the online Ruby 3.1.0 build. # The game uses RUS2; LonaCraft originally shipped only RUS. # Resolution for LonaCraft keys: selected language -> RUS (for RUS2) -> ENG. # Other game and mod localization keys are never altered. #============================================================================== module LonaCraft module TextFallback PART = "LonaCraft" BASE_LANG = "ENG" @cache = {} # [language, filename] => parsed text hash @reported = {} # one diagnostic per missing language/key def self.language $lang.to_s.upcase end def self.fallback_languages language == "RUS2" ? ["RUS", BASE_LANG] : [BASE_LANG] end def self.split(key) return nil unless key.is_a?(String) && key.start_with?(PART + ":") parts = key.split(":") return nil unless parts.size == 3 && !parts[1].empty? && !parts[2].empty? [parts[1], parts[2]] end def self.report(kind, message) key = "#{language}|#{kind}" return if @reported[key] @reported[key] = true p message rescue StandardError nil end # Use the mod's actual registered base directory, including when mods # are installed outside the default game folder. Do not assume the # game's Text/RUS2 -> Text/RUS fallback applies to mod paths. def self.path(text, lang, file) return nil unless file.to_s.match?(/\A[A-Za-z0-9_-]+\z/) parts = text.instance_variable_get(:@parts) current = parts.is_a?(Hash) ? parts[PART] : nil return nil unless current && !current.to_s.empty? File.join(File.dirname(current.to_s), lang, "#{file}.txt") end def self.load_language_file(text, lang, file) cache_key = [lang, file] return @cache[cache_key] if @cache.has_key?(cache_key) file_path = path(text, lang, file) return nil unless file_path && File.file?(file_path) begin content = if Text.respond_to?(:read_text_file) Text.read_text_file(file_path) else File.binread(file_path).force_encoding('UTF-8') end raise Encoding::InvalidByteSequenceError, "invalid UTF-8: #{file_path}" unless content.valid_encoding? parsed = text.parse(content) @cache[cache_key] = parsed.is_a?(Hash) ? parsed : {} rescue StandardError => error report("read:#{lang}/#{file}", "LonaCraft Text: #{lang}/#{file}: #{error.class} #{error.message}") nil end end # A key missing in RUS2 prefers the existing Russian translation to ENG. def self.lookup(text, key) parts = split(key) return nil unless parts file, id = parts fallback_languages.each do |lang| next if lang == language data = load_language_file(text, lang, file) next unless data result = data[id] next if result.nil? || result.to_s.empty? report("key:#{key}", "LonaCraft Text: #{key} missing in #{language}; using #{lang}") return result end report("missing:#{key}", "LonaCraft Text: #{key} not translated in #{language} or fallbacks") nil rescue StandardError => error p "LonaCraft Text: fallback #{error.class} #{error.message}" nil end # A missing whole locale file must not invoke the game's blocking msgbox. def self.missing_file(text, file) fallback_languages.each do |lang| next if lang == language data = load_language_file(text, lang, file) next unless data report("file:#{file}", "LonaCraft Text: #{language}/#{file}.txt missing; using #{lang}") return data.dup end nil end def self.get(file, id, default) key = "#{PART}:#{file}:#{id}" result = $game_text ? $game_text[key] : nil return default if result.nil? || result == key || result.to_s.empty? result.to_s rescue StandardError default end end end class Text unless method_defined?(:lonacraft_text_fb_orig) alias_method :lonacraft_text_fb_orig, :[] def [](key, noError = false) result = lonacraft_text_fb_orig(key, noError) if LonaCraft::TextFallback.split(key) && (result == key || result.to_s.empty?) fallback = LonaCraft::TextFallback.lookup(self, key) return fallback unless fallback.nil? end result end end unless method_defined?(:lonacraft_text_fb_load_orig) alias_method :lonacraft_text_fb_load_orig, :load_file def load_file(*args) part, file = args.length >= 2 ? [args[0], args[1]] : [nil, args[0]] if part == LonaCraft::TextFallback::PART path = LonaCraft::TextFallback.path(self, LonaCraft::TextFallback.language, file) unless path.nil? || File.file?(path) backup = LonaCraft::TextFallback.missing_file(self, file) return backup unless backup.nil? end end lonacraft_text_fb_load_orig(*args) end end end