<commit_before># Translator - the class formerly known as LanguageHandler2
#
# License: Apache License, Version 2.0

require 'sketchup'

module CommunityExtensions
  module STL
  
    # Class that handles string localizations.
    # 
    # It is written to be able compatible with LanguageHandler which ships with
    # SketchUp (Current version 8M4). Existing .string files can be used and
    # methods are aliases so the class can be used as a drop in replacement.
    # 
    # Enhanced features include:
    # * One-line comments can start anywhere.
    # * Multi-line comments can start and stop anywhere outside a string.
    # * ; at the end of a key - value pair is optional.
    # * Strings can be split up by using the + operator.
    # * Strings can cross multiple lines.
    # * Strict enforcement of the format ensures an error is raised instead of
    #   producing junk data.
    # * Can be easily extened to include advanced features such as
    #   escape-characters if needed.
    # 
    # See Tests folder for sample .strings files.
    class Translator
      
      STATE_SEARCH             = 0 # Looking for " or /
      STATE_IN_KEY             = 1 # Looking for "
      STATE_EXPECT_EQUAL       = 2 # Looking for =
      STATE_EXPECT_VALUE       = 3 # Looking for "
      STATE_IN_VALUE           = 4 # Looking for "
      STATE_EXPECT_END         = 5 # Looking for ;
      STATE_EXPECT_COMMENT     = 6 # Found / - Expecting * or / next
      STATE_IN_COMMENT_MULTI   = 7 # Found /* - Looking for */
      STATE_EXPECT_COMMENT_END = 8 # Found * - Expecting / next
      STATE_IN_COMMENT_SINGLE  = 9 # Found // - Looking for end of line
      
      TOKEN_WHITESPACE     = /\s/
      TOKEN_CONCAT         = ?+
      TOKEN_QUOTE          = ?"
      TOKEN_EQUAL          = ?=
      TOKEN_END            = ?;
      TOKEN_EOL            = /\n|\r/
      TOKEN_COMMENT_START  = ?/
      TOKEN_COMMENT_MULTI  = ?*
      TOKEN_COMMENT_SINGLE = ?/
      
      class ParseError < StandardError; end
      
      # A second optional Hash argument can be used to spesify behaviour that
      # differ from LanguageHandler.
      # 
      # Option Keys:
      # * :custom_path - String pointing to a custom path where the localized
      #                  strings are. If ommitted the Translator will look in
      #                  SketchUp's Resource folder.
      # * :debug       - Set to true for a detailed trace of the parsing.
      #
      # @param [String] filename
      # @param [Nil,Hash] options
      def initialize(filename, options = nil)
        @path = nil
        @debug = false
        unless options.nil?
          unless options.is_a?(Hash)
            raise ArgumentError, 'Second argument must be Nil or a Hash.'
          end
          @path  = options[:custom_path]
          @debug = options[:debug]
        end
        @strings = parse(filename, @path)
      end
      
      # If the requested string is not in the localization dictionary then the
      # original string is returned.
      #
      # @param [String] string The String to be localized.
      #
      # @return [String] Localized string
      def get(string)
        @strings[string]
      end
      alias :GetString :get

      # @return [Hash] The dictionary Hash used for localization.
      def dictionary
        @strings
      end
      alias :GetStrings :dictionary

      # @param [String] custom_path
      #
      # @return [String]
      def self.GetResourceSubPath(custom_path = nil)
        if custom_path
          full_file_path = File.join(custom_path, Sketchup.get_locale)
        else
          full_file_path = Sketchup.get_resource_path('')
        end
        components = full_file_path.split(File::SEPARATOR)
        components[-2, 2].join(File::SEPARATOR)
      end
      
      # Prints out the dictionary for visual inspection.
      #
      # @return [String]
      def print_dictionary
        output = ''
        for key, value in @strings
          output << "====================\n"
          output << "#{key}\n"
          output << "--------------------\n"
          output << "#{value}\n"
        end
        puts output
      end
      
      # @return [String]
      def inspect
        object_hex_id = "0x%x" % (self.object_id << 1)
        size = @strings.size
        locale = Sketchup.get_locale
        "<#{self.class}::#{object_hex_id} - Strings:#{size} (#{locale})>"
      end
      
      private
      
      # Parses the given file and returns a Hash lookup.
      #
      # @param [String] filename
      # @param [String] custom_path
      #
      # @return [Hash]
      def parse(filename, custom_path = nil)
        # Find the correct file based on the current locale setting in SketchUp.
        # If no path has been given it'll revert back to the Resource folder in
        # SketchUp, like LanguageHandler does.
        if custom_path
          full_file_path = File.join(custom_path, Sketchup.get_locale, filename)
        else
          full_file_path = Sketchup.get_resource_path(filename)
        end
        
        # Ensure the file is valid.<commit_msg>fixes `invalid file` because of mixed forward/backward slash<commit_after># Translator - the class formerly known as LanguageHandler2
#
# License: Apache License, Version 2.0

require 'sketchup'

module CommunityExtensions
  module STL
  
    # Class that handles string localizations.
    # 
    # It is written to be able compatible with LanguageHandler which ships with
    # SketchUp (Current version 8M4). Existing .string files can be used and
    # methods are aliases so the class can be used as a drop in replacement.
    # 
    # Enhanced features include:
    # * One-line comments can start anywhere.
    # * Multi-line comments can start and stop anywhere outside a string.
    # * ; at the end of a key - value pair is optional.
    # * Strings can be split up by using the + operator.
    # * Strings can cross multiple lines.
    # * Strict enforcement of the format ensures an error is raised instead of
    #   producing junk data.
    # * Can be easily extened to include advanced features such as
    #   escape-characters if needed.
    # 
    # See Tests folder for sample .strings files.
    class Translator
      
      STATE_SEARCH             = 0 # Looking for " or /
      STATE_IN_KEY             = 1 # Looking for "
      STATE_EXPECT_EQUAL       = 2 # Looking for =
      STATE_EXPECT_VALUE       = 3 # Looking for "
      STATE_IN_VALUE           = 4 # Looking for "
      STATE_EXPECT_END         = 5 # Looking for ;
      STATE_EXPECT_COMMENT     = 6 # Found / - Expecting * or / next
      STATE_IN_COMMENT_MULTI   = 7 # Found /* - Looking for */
      STATE_EXPECT_COMMENT_END = 8 # Found * - Expecting / next
      STATE_IN_COMMENT_SINGLE  = 9 # Found // - Looking for end of line
      
      TOKEN_WHITESPACE     = /\s/
      TOKEN_CONCAT         = ?+
      TOKEN_QUOTE          = ?"
      TOKEN_EQUAL          = ?=
      TOKEN_END            = ?;
      TOKEN_EOL            = /\n|\r/
      TOKEN_COMMENT_START  = ?/
      TOKEN_COMMENT_MULTI  = ?*
      TOKEN_COMMENT_SINGLE = ?/
      
      class ParseError < StandardError; end
      
      # A second optional Hash argument can be used to spesify behaviour that
      # differ from LanguageHandler.
      # 
      # Option Keys:
      # * :custom_path - String pointing to a custom path where the localized
      #                  strings are. If ommitted the Translator will look in
      #                  SketchUp's Resource folder.
      # * :debug       - Set to true for a detailed trace of the parsing.
      #
      # @param [String] filename
      # @param [Nil,Hash] options
      def initialize(filename, options = nil)
        @path = nil
        @debug = false
        unless options.nil?
          unless options.is_a?(Hash)
            raise ArgumentError, 'Second argument must be Nil or a Hash.'
          end
          @path  = options[:custom_path]
          @debug = options[:debug]
        end
        @strings = parse(filename, @path)
      end
      
      # If the requested string is not in the localization dictionary then the
      # original string is returned.
      #
      # @param [String] string The String to be localized.
      #
      # @return [String] Localized string
      def get(string)
        @strings[string]
      end
      alias :GetString :get

      # @return [Hash] The dictionary Hash used for localization.
      def dictionary
        @strings
      end
      alias :GetStrings :dictionary

      # @param [String] custom_path
      #
      # @return [String]
      def self.GetResourceSubPath(custom_path = nil)
        if custom_path
          full_file_path = File.join(custom_path, Sketchup.get_locale)
        else
          full_file_path = Sketchup.get_resource_path('')
        end
        components = full_file_path.split(File::SEPARATOR)
        components[-2, 2].join(File::SEPARATOR)
      end
      
      # Prints out the dictionary for visual inspection.
      #
      # @return [String]
      def print_dictionary
        output = ''
        for key, value in @strings
          output << "====================\n"
          output << "#{key}\n"
          output << "--------------------\n"
          output << "#{value}\n"
        end
        puts output
      end
      
      # @return [String]
      def inspect
        object_hex_id = "0x%x" % (self.object_id << 1)
        size = @strings.size
        locale = Sketchup.get_locale
        "<#{self.class}::#{object_hex_id} - Strings:#{size} (#{locale})>"
      end
      
      private
      
      # Parses the given file and returns a Hash lookup.
      #
      # @param [String] filename
      # @param [String] custom_path
      #
      # @return [Hash]
      def parse(filename, custom_path = nil)
        # Find the correct file based on the current locale setting in SketchUp.
        # If no path has been given it'll revert back to the Resource folder in
        # SketchUp, like LanguageHandler does.
        if custom_path
          full_file_path = File.join(File.expand_path(custom_path), Sketchup.get_locale, filename)
        else
          full_file_path = Sketchup.get_resource_path(filename)
        end
        
        # Ensure the file is valid.
{'fraction_non_alphanumeric': 0.08342225240081483, 'fraction_numerical': 0.0036860995246871665, 'mean_word_length': 1.867074527252503, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7196718', 'n_tokens_mistral': 2958, 'n_tokens_neox': 2854, 'n_words': 958}