Advertisement
Holy87

Multilanguage Script - EN

Aug 5th, 2015
469
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 32.59 KB | None | 0 0
  1. =begin
  2.  ==============================================================================
  3.   ■ Holy87's Multilanguage script
  4.       version 1.0.3
  5.       User difficulty: ★★★
  6.       License: CC-BY. Everyone can distribute this script and use in their free
  7.       and commercial games. Credit required.
  8.  ==============================================================================
  9.     This script allow the game to be localizated in more languages! You can
  10.     creat emany language files to allow players to play in their mother language.
  11.  ==============================================================================
  12.   ■ Compatibility
  13.     DataManager -> alias load_normal_database
  14.     Bitmap -> alias initialize, draw_text
  15.  ==============================================================================
  16.   ■ Changelog
  17.  ==============================================================================
  18.   ■ Installation and instructions
  19.     Install this script before main and under Materials
  20.     REQUIRES HOLY87'S SUPPORT MODULE.
  21.     here http://pastebin.com/H5pJ3fXr
  22.     If you've installed my Game Options script, then the language option will
  23.     appear.
  24.    
  25.   ■ Instructions to how set the game
  26.      
  27.     ● First of all, create the Localization folder inside the root folder of
  28.       the project. Inside of that you must create so many files as how much
  29.       language you want to add. For example: I want to create the Italian language
  30.       file. So i create the it.ini file, where it is the abbreviation of the
  31.       italian language and ini is the file extension (just change .txt in .ini).
  32.       Open the file, and write inside the tag where you will insert the language
  33.       informations, like this:
  34.       <header>
  35.       Language:Italiano
  36.       ID:1040
  37.       </header>
  38.       Language is the name showed in the options, while ID is the system language
  39.       in order to be automatically setted to Italian if the user is.
  40.       All the language codes can be find here
  41.       http://support.microsoft.com/kb/193080
  42.       https://msdn.microsoft.com/en-US/library/ee825488(v=cs.20).aspx
  43.      
  44.     ● The second thing to do is to create the strings that we need to be inserted
  45.       in the gae. Create the strings tag like we've done with header. For example:
  46.       <strings>
  47.       greetings:Ciao!
  48.       defeat:Sei stato sconfitto.
  49.       </strings>
  50.       It's important that the strings will be on the SAME LINE. Set the symbol
  51.       \n if you want a new line of the text.
  52.      
  53.     ● In the project, insert the tag {S: StringName} to replace the tag with the string
  54.       in the proper language file. For example. if in a game message or in any other
  55.       part
  56.      
  57.     ● Really, there is another way for the database objects. You can create other
  58.       tags with items, skills, actors, enemies etc.. headers, that contain the
  59.       informations of any attribute For example, writing
  60.       <skills>
  61.       ID:1, name:Attack, description:Attack with the equipped weapon; message1: attacks!
  62.       </skills>
  63.       The semicolon separates the attributes. To show the ; in the game, weite \;
  64.       So the skill 1 in the database will have name, description and message identified
  65.       by the language file. Once completed, you have to edit the file for the other
  66.       languages.
  67.      
  68.     ● And for the pictures?
  69.       If you have pictures that show a text and you want to use a different image
  70.       depending on the selected language, you just need insert more images with
  71.       a difference in the name, adding _lang. For example, I have an image
  72.       called Cloud.png in the Pictures folder. Also I have created a Cloud_it.png
  73.       and Cloud_fr.png picture in the same folder. If the game will have the
  74.       italian language, when I go to load the Cloud picture I will see Cloud_it,
  75.       in french Cloud_en and in all other languages simply Cloud.
  76.      
  77.  ==============================================================================
  78. =end
  79. #==============================================================================
  80. # ** SETTINGS
  81. #------------------------------------------------------------------------------
  82. #  Set the options of the script
  83. #==============================================================================
  84. module H87Localization
  85.   module Settings
  86.     #--------------------------------------------------------------------------
  87.     # * The default language if there are no languages for the installed system
  88.     #--------------------------------------------------------------------------
  89.     DEFAULT_LANGUAGE = "en"
  90.     #--------------------------------------------------------------------------
  91.     # * Folder of the language files
  92.     #--------------------------------------------------------------------------
  93.     LANG_FOLDER = "Localization"
  94.     #--------------------------------------------------------------------------
  95.     # * Settings command (if you have my menu settings)
  96.     #--------------------------------------------------------------------------
  97.     OPTION_LANGUAGE = "Language"
  98.     #--------------------------------------------------------------------------
  99.     # * Settings help text
  100.     #--------------------------------------------------------------------------
  101.     OPTION_DESCRIPTION = "Select your language."
  102.   end
  103. end
  104.  
  105. #---------------------------- END OF CONFIGURATION ----------------------------
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116. $imported = {} if $imported == nil
  117. $imported["H87_Localization"] = true
  118.  
  119. #==============================================================================
  120. # ** module H87Localization
  121. #------------------------------------------------------------------------------
  122. #  Module for language-check processing.
  123. #==============================================================================
  124. module H87Localization
  125.                          
  126.   REG_EXCAPE = /\{S:[ ]+(.+?)\}/
  127.   #--------------------------------------------------------------------------
  128.   # * Gets a string. If it contains a reference to a localized strings, returns
  129.   #   the localized strings. Otherwise, the string itself.
  130.   #   str: string to check
  131.   #--------------------------------------------------------------------------
  132.   def self.check_localized_string(str)
  133.     DataManager.load_localization if $data_localization.nil?
  134.     r = REG_EXCAPE
  135.     return str.to_s.gsub(r){|s|get_localized_string($1) if s =~ r}
  136.   end
  137.   #--------------------------------------------------------------------------
  138.   # * Returns the selected language (if set by user or system default)
  139.   #--------------------------------------------------------------------------
  140.   def self.selected_system_language
  141.     DataManager.load_h87settings unless $game_settings
  142.     if $game_settings["sys_lang"].nil?
  143.       return default_game_language
  144.     else
  145.       return $game_settings["sys_lang"]
  146.     end
  147.   end
  148.   #--------------------------------------------------------------------------
  149.   # * Returns the default language if not setted by user
  150.   #--------------------------------------------------------------------------
  151.   def self.default_game_language
  152.     proper_l(system_language)
  153.   end
  154.   #--------------------------------------------------------------------------
  155.   # * Returns the Windows language.
  156.   #--------------------------------------------------------------------------
  157.   def self.system_language
  158.     code = Win.language
  159.     return default_language if lang_from_code(code).nil?
  160.     return lang_from_code(code)
  161.   end
  162.   #--------------------------------------------------------------------------
  163.   # * Checks if there is the language with sub-language, otherwise returns
  164.   #   the traditional language
  165.   #--------------------------------------------------------------------------
  166.   def self.proper_l(lang)
  167.     if language_avaiable?(lang)
  168.       return lang
  169.     else
  170.       return dialet?(lang) ? proper_l(base_language(lang)) : default_language
  171.     end
  172.   end
  173.   #--------------------------------------------------------------------------
  174.   # * Returns the traditional language from the dialet.
  175.   #   lang: language (i.e. "en-uk" -> "en")
  176.   #--------------------------------------------------------------------------
  177.   def base_language(lang)
  178.     return lang unless dialet?(lang)
  179.     return lang.split("-")[0]
  180.   end
  181.   #--------------------------------------------------------------------------
  182.   # * Returns the language short name (es. "en") from a code (es. 1033)
  183.   #--------------------------------------------------------------------------
  184.   def self.lang_from_code(code)
  185.     installed_languages.each do |language|
  186.       return language.short if language.id == code
  187.     end
  188.     return nil
  189.   end
  190.   #--------------------------------------------------------------------------
  191.   # * Return true if a language is installed
  192.   #   lang: language short name
  193.   #--------------------------------------------------------------------------
  194.   def self.language_avaiable?(lang)
  195.     installed_languages.each do |language|
  196.       return true if lang == language.short
  197.     end
  198.     return false
  199.   end
  200.   #--------------------------------------------------------------------------
  201.   # * Return the language file name
  202.   #   lang: language short name
  203.   #--------------------------------------------------------------------------
  204.   def self.language_file(lang)
  205.     language_folder+"/#{lang}.ini"
  206.   end
  207.   #--------------------------------------------------------------------------
  208.   # * Return the language files folder
  209.   #--------------------------------------------------------------------------
  210.   def self.language_folder
  211.     return Settings::LANG_FOLDER
  212.   end
  213.   #--------------------------------------------------------------------------
  214.   # * Return the default language when the player's language is not avaiable
  215.   #--------------------------------------------------------------------------
  216.   def self.default_language
  217.     Settings::DEFAULT_LANGUAGE
  218.   end
  219.   #--------------------------------------------------------------------------
  220.   # * Return true if the language is a traditional language or a sub-language.
  221.   #   lang: language. I.E.: en-uk => true, en => false
  222.   #--------------------------------------------------------------------------
  223.   def self.dialet?(lang)
  224.     return lang =~ /[.]+\-[.]+/
  225.   end
  226.   #--------------------------------------------------------------------------
  227.   # * Return the avaiable languages list
  228.   #--------------------------------------------------------------------------
  229.   def self.installed_languages
  230.     @languages = check_installed_languages if @languages.nil?
  231.     return @languages
  232.   end
  233.   #--------------------------------------------------------------------------
  234.   # * Checks all the languages in language folder.
  235.   #--------------------------------------------------------------------------
  236.   def self.check_installed_languages
  237.     languages = []
  238.     Dir.foreach(language_folder) {|x|
  239.       next if x == "." or x == ".."
  240.       file_name = language_folder+"/"+x
  241.       next if File.directory?(file_name)
  242.       dl = Data_Localization.new
  243.       File.open(file_name,"r") do |file|
  244.         if dl.language_ok?(file.read)
  245.           short = File.basename(file_name,".*")
  246.           name = dl.lang_name
  247.           id = dl.lang_id
  248.           languages.push(Language.new(id, short, name))
  249.         end
  250.       end
  251.     }
  252.     return languages
  253.   end
  254.   #--------------------------------------------------------------------------
  255.   # * Return the Option hash (if Option Menu is installed)
  256.   #--------------------------------------------------------------------------
  257.   def self.create_option
  258.     { :type => :variable,
  259.       :text => Settings::OPTION_LANGUAGE,
  260.       :help => Settings::OPTION_DESCRIPTION,
  261.       :var  => "sys_lang",
  262.       :default => default_game_language,
  263.       :open_popup => true,
  264.       :method => :update_language,
  265.       :values => installed_languages_names
  266.     }
  267.   end
  268.   #--------------------------------------------------------------------------
  269.   # * Return the name of all avaiable languages
  270.   #--------------------------------------------------------------------------
  271.   def self.installed_languages_names
  272.     hash = {}
  273.     for language in installed_languages
  274.       hash[language.short] = language.name
  275.     end
  276.     return hash
  277.   end
  278.   #--------------------------------------------------------------------------
  279.   # * Return the proper string from a tag.
  280.   #--------------------------------------------------------------------------
  281.   def self.get_localized_string(string_id)
  282.     DataManager.load_localization unless $data_localization
  283.     return $data_localization.get_string(string_id)
  284.   end
  285. end
  286.  
  287. #==============================================================================
  288. # ** class Option
  289. #------------------------------------------------------------------------------
  290. #  Loading language after selecting a new lang.
  291. #==============================================================================
  292. class Option
  293.   #--------------------------------------------------------------------------
  294.   # * Reload the language after selection
  295.   #--------------------------------------------------------------------------
  296.   def update_language(lang)
  297.     DataManager.load_localization unless $data_localization
  298.     $data_localization.load_language(lang)
  299.   end
  300. end
  301.  
  302. #==============================================================================
  303. # ** Bitmap
  304. #------------------------------------------------------------------------------
  305. #  Methods for taking the right bitmap and draw proper text
  306. #==============================================================================
  307. class Bitmap
  308.   #--------------------------------------------------------------------------
  309.   # * Alias
  310.   #--------------------------------------------------------------------------
  311.   alias h87localization_dt draw_text unless $@
  312.   alias h87localization_init initialize unless $@
  313.   #--------------------------------------------------------------------------
  314.   # * Object initialization
  315.   #--------------------------------------------------------------------------
  316.   def initialize(*args)
  317.     if args[0].is_a?(String)
  318.       begin
  319.         DataManager.load_h87settings unless $game_settings
  320.         DataManager.load_localization unless $data_localization
  321.         args2 = args.clone
  322.         args2[0]+="_#{$game_settings["sys_lang"]}"
  323.         h87localization_init(*args2)
  324.       rescue
  325.         h87localization_init(*args)
  326.       end
  327.     else
  328.       h87localization_init(*args)
  329.     end
  330.   end
  331.   #--------------------------------------------------------------------------
  332.   # * draw_text
  333.   #--------------------------------------------------------------------------
  334.   def draw_text(*args)
  335.     a_index = args[0].is_a?(Rect) ? 1 : 4
  336.     args[a_index] = H87Localization.check_localized_string(args[a_index])
  337.     h87localization_dt(*args)
  338.   end
  339. end
  340.  
  341. #==============================================================================
  342. # ** class Object
  343. #------------------------------------------------------------------------------
  344. #  Changing sprintf method
  345. #==============================================================================
  346. class Object
  347.   alias unlocalized_sprintf sprintf unless $@
  348.   #--------------------------------------------------------------------------
  349.   # * sprintf
  350.   #--------------------------------------------------------------------------
  351.   def sprintf(*args)
  352.     args[0]=H87Localization.check_localized_string(args[0])
  353.     unlocalized_sprintf(*args)
  354.   end
  355. end
  356.  
  357. #==============================================================================
  358. # ** class Language
  359. #------------------------------------------------------------------------------
  360. #  A simple class for handling languages
  361. #==============================================================================
  362. class Language
  363.   #--------------------------------------------------------------------------
  364.   # * Public instance variables
  365.   #--------------------------------------------------------------------------
  366.   attr_accessor :id
  367.   attr_accessor :short
  368.   attr_accessor :name
  369.   #--------------------------------------------------------------------------
  370.   # * Object initialization
  371.   #--------------------------------------------------------------------------
  372.   def initialize(id, short, name)
  373.     @id = id.to_i
  374.     @short = short
  375.     @name = name
  376.   end
  377. end
  378.  
  379. #==============================================================================
  380. # ** module DataManager
  381. #------------------------------------------------------------------------------
  382. #  For language loading
  383. #==============================================================================
  384. module DataManager
  385.   #--------------------------------------------------------------------------
  386.   # * alias
  387.   #--------------------------------------------------------------------------
  388.   class << self
  389.     alias h87loc_load_n_db load_normal_database
  390.     alias h87loc_load_b_db load_battle_test_database
  391.   end
  392.   # --------------------------------------------------------------------------
  393.   # * database loading
  394.   # --------------------------------------------------------------------------
  395.   def self.load_normal_database
  396.     h87loc_load_n_db
  397.     load_localization
  398.   end
  399.   # --------------------------------------------------------------------------
  400.   # * bt loading
  401.   # --------------------------------------------------------------------------
  402.   def self.load_battle_test_database
  403.     h87loc_load_b_db
  404.     load_localization if $data_localization.nil?
  405.   end
  406.   #--------------------------------------------------------------------------
  407.   # * language loading
  408.   #--------------------------------------------------------------------------
  409.   def self.load_localization
  410.     $data_localization = Data_Localization.new
  411.     $data_localization.load_language(H87Localization.selected_system_language)
  412.   end
  413. end
  414.  
  415. #==============================================================================
  416. # ** class Window_Base
  417. #------------------------------------------------------------------------------
  418. #  Changing method for advanced text drawing
  419. #==============================================================================
  420. class Window_Base < Window
  421.   alias h87loc_dte draw_text_ex unless $@
  422.   #--------------------------------------------------------------------------
  423.   # * draw_text_ex
  424.   #--------------------------------------------------------------------------
  425.   def draw_text_ex(x, y, text)
  426.     h87loc_dte(x, y, H87Localization.check_localized_string(text))
  427.   end
  428. end
  429.  
  430. #==============================================================================
  431. # ** class Game_Message
  432. #------------------------------------------------------------------------------
  433. #  Changing the text message
  434. #==============================================================================
  435. class Game_Message
  436.   alias h87loc_add add unless $@
  437.   #--------------------------------------------------------------------------
  438.   # * Add Text
  439.   #--------------------------------------------------------------------------
  440.   def add(text)
  441.     h87loc_add(H87Localization.check_localized_string(text))
  442.   end
  443. end
  444.  
  445. #==============================================================================
  446. # ** class Game_Interpreter
  447. #------------------------------------------------------------------------------
  448. #  Game Interpreter class
  449. #==============================================================================
  450. class Game_Interpreter
  451.   alias h87loc_setup_choices setup_choices unless $@
  452.   #--------------------------------------------------------------------------
  453.   # * Alias method for setup_choices, makes the choice window fit the text.
  454.   #--------------------------------------------------------------------------
  455.   def setup_choices(params)
  456.     par0 = params.clone
  457.     par0[0].map! {|text| H87Localization.check_localized_string(text) }
  458.     h87loc_setup_choices(par0)
  459.   end
  460. end
  461.  
  462. #==============================================================================
  463. # ** class Data_Localization
  464. #------------------------------------------------------------------------------
  465. #  Localization class for language and strings handling. Used in
  466. #  $data_localization instance.
  467. #==============================================================================
  468. class Data_Localization
  469.   #--------------------------------------------------------------------------
  470.   # * public instance variables
  471.   #--------------------------------------------------------------------------
  472.   attr_reader :lang_name
  473.   attr_accessor :language
  474.   #--------------------------------------------------------------------------
  475.   # * object initialization
  476.   #--------------------------------------------------------------------------
  477.   def initialize(lang = nil)
  478.     @strings = {}
  479.     load_language(lang, false) if lang
  480.   end
  481.   #--------------------------------------------------------------------------
  482.   # * language ID/code
  483.   #--------------------------------------------------------------------------
  484.   def lang_id
  485.     return @lang_id.to_i
  486.   end
  487.   #--------------------------------------------------------------------------
  488.   # * Checks if a game string contains a loalized string
  489.   #--------------------------------------------------------------------------
  490.   def parse_string(string)
  491.     H87Localization.check_localized_string(string)
  492.   end
  493.   #--------------------------------------------------------------------------
  494.   # * Takes a string from the loaded strings of the active language
  495.   #--------------------------------------------------------------------------
  496.   def get_string(string_id)
  497.     if @strings[string_id] != nil
  498.       return @strings[string_id].gsub(/\\n[^\[^<]/i,"\n")
  499.     else
  500.       println "String not found for value #{string_id}"
  501.       return string_from_other_languages(string_id, @language)
  502.     end
  503.   end
  504.   #--------------------------------------------------------------------------
  505.   # * Loads a new language
  506.   #   lang_name: language short name
  507.   #--------------------------------------------------------------------------
  508.   def load_language(lang_name, database = true)
  509.     @language = lang_name
  510.     file = H87Localization.language_file(lang_name)
  511.     unless File.exist?(file)
  512.       println "Language file #{file} not found."
  513.       return
  514.     end
  515.     File.open(file, "r") do |content|
  516.       @readstring = content.read
  517.     end
  518.     if language_ok?(@readstring)
  519.       load_database(@readstring) if $data_system && database
  520.       load_strings(@readstring)
  521.       println "Language loaded: #{file}."
  522.     else
  523.       println "Cannot read the #{file} file."
  524.       return
  525.     end
  526.   end
  527.   #--------------------------------------------------------------------------
  528.   # * Returns true if the file is a language file
  529.   #--------------------------------------------------------------------------
  530.   def language_ok?(readstring)
  531.     return false unless readstring =~ category_regexp("header")
  532.     info = string_array($1)
  533.     begin
  534.       @lang_name = info["Language"]
  535.       @lang_id = info["ID"]
  536.       return true
  537.     rescue
  538.       return false
  539.     end
  540.   end
  541.   #--------------------------------------------------------------------------
  542.   # * Load the localized database from file
  543.   #--------------------------------------------------------------------------
  544.   def load_database(readstring)
  545.     load_items(readstring)
  546.     load_skills(readstring)
  547.     load_weapons(readstring)
  548.     load_armors(readstring)
  549.     load_enemies(readstring)
  550.     load_actors(readstring)
  551.     load_classes(readstring)
  552.     load_states(readstring)
  553.     load_terms(readstring)
  554.   end
  555.   #--------------------------------------------------------------------------
  556.   # * Load items array
  557.   #--------------------------------------------------------------------------
  558.   def load_items(readstring)
  559.     item_list = load_data(readstring, "items",:id, :name, :description)
  560.     change_array($data_items, item_list)
  561.   end
  562.   #--------------------------------------------------------------------------
  563.   # * Load skills array
  564.   #--------------------------------------------------------------------------
  565.   def load_skills(readstring)
  566.     item_list = load_data(readstring, "skills",:id, :name, :description,
  567.     :message1, :message2)
  568.     change_array($data_skills, item_list)
  569.   end
  570.   #--------------------------------------------------------------------------
  571.   # * load weapons array
  572.   #--------------------------------------------------------------------------
  573.   def load_weapons(readstring)
  574.     item_list = load_data(readstring, "weapons",:id, :name, :description)
  575.     change_array($data_weapons, item_list)
  576.   end
  577.   #--------------------------------------------------------------------------
  578.   # * load armors array
  579.   #--------------------------------------------------------------------------
  580.   def load_armors(readstring)
  581.     item_list = load_data(readstring, "armors",:id, :name, :description)
  582.     change_array($data_armors, item_list)
  583.   end
  584.   #--------------------------------------------------------------------------
  585.   # * load enemies array
  586.   #--------------------------------------------------------------------------
  587.   def load_enemies(readstring)
  588.     item_list = load_data(readstring, "enemies",:id, :name)
  589.     change_array($data_enemies, item_list)
  590.   end
  591.   #--------------------------------------------------------------------------
  592.   # * load actors array
  593.   #--------------------------------------------------------------------------
  594.   def load_actors(readstring)
  595.     item_list = load_data(readstring, "actors",:id, :name, :title, :description)
  596.     change_array($data_actors, item_list)
  597.   end
  598.   #--------------------------------------------------------------------------
  599.   # * load class array
  600.   #--------------------------------------------------------------------------
  601.   def load_classes(readstring)
  602.     item_list = load_data(readstring, "classes",:id, :name)
  603.     change_array($data_classes, item_list)
  604.   end
  605.   #--------------------------------------------------------------------------
  606.   # * load states array
  607.   #--------------------------------------------------------------------------
  608.   def load_states(readstring)
  609.     item_list = load_data(readstring, "states",:id, :name, :message1, :message2,
  610.     :message3, :message4)
  611.     change_array($data_states, item_list)
  612.   end
  613.   #--------------------------------------------------------------------------
  614.   # * load terms array
  615.   #--------------------------------------------------------------------------
  616.   def load_terms(readstring)
  617.     terms = $data_system.terms
  618.     basic = get_string_array(readstring, "basic")
  619.     params = get_string_array(readstring, "params")
  620.     etypes = get_string_array(readstring, "etypes")
  621.     commands = get_string_array(readstring, "commands")
  622.     set_terms(terms.basic, basic)
  623.     set_terms(terms.params, params)
  624.     set_terms(terms.etypes, etypes)
  625.     set_terms(terms.commands, commands)
  626.   end
  627.   #--------------------------------------------------------------------------
  628.   # * Takes a substring from the file depending from the <type>...</type> tag
  629.   #--------------------------------------------------------------------------
  630.   def category_regexp(cat_name)
  631.     return /[.]*<#{cat_name}>(.*)<\/#{cat_name}>[.]*/im
  632.   end
  633.   #--------------------------------------------------------------------------
  634.   # * Load generic game strings
  635.   #--------------------------------------------------------------------------
  636.   def load_strings(readstring)
  637.     @strings = get_string_array(readstring, "strings")
  638.   end
  639.   #--------------------------------------------------------------------------
  640.   # * returns the string array
  641.   #   readstring: file readed
  642.   #   #header: string type
  643.   #--------------------------------------------------------------------------
  644.   def get_string_array(readstring, header)
  645.     if readstring =~ category_regexp(header)
  646.       return string_array($1)
  647.     end
  648.   end
  649.   #--------------------------------------------------------------------------
  650.   # * takes the proper string from file to array
  651.   #--------------------------------------------------------------------------
  652.   def set_terms(terms_array, term_hash)
  653.     return if term_hash.nil?
  654.     for i in 0..terms_array.size-1
  655.       next if term_hash[i.to_s].nil?
  656.       terms_array[i] = term_hash[i.to_s]
  657.     end
  658.   end
  659.   #--------------------------------------------------------------------------
  660.   # * strings array creation
  661.   #--------------------------------------------------------------------------
  662.   def string_array(text)
  663.     strings = {}
  664.     text.split(/[\r\n]+/).each do |string|
  665.       case string
  666.       when /[\s]*([^:.]*+)[\s]*:[\s]*(.+)[\s]*/i
  667.         key = $1
  668.         val = $2
  669.         key.gsub!(/[\t\n]/,""); val.gsub!(/[\t\n]/,"")
  670.         if strings[key].nil?
  671.           strings[key] = val
  672.         else
  673.           println "#{key} is defined twice!\n#{key}->#{strings[key]}\n#{$1}->#{val}"
  674.         end
  675.       end
  676.     end
  677.     return strings
  678.   end
  679.   #--------------------------------------------------------------------------
  680.   # * Return the regexp for taking a parameter
  681.   #--------------------------------------------------------------------------
  682.   def param_regexp
  683.     return /[\s]*(.+)[\s]*:(.+)[\s]*/i
  684.   end
  685.   #--------------------------------------------------------------------------
  686.   # * loads a data file to the system
  687.   #--------------------------------------------------------------------------
  688.   def load_data(*args)
  689.     readstring = args[0]
  690.     if readstring =~ category_regexp(args[1])
  691.       id = args[2]
  692.       text = $1
  693.       hash = {}
  694.       text.each_line do |line|
  695.         item = get_item_from_parameters(line)
  696.         next if item.empty? or item[id] == nil
  697.         idd = item[id].to_i
  698.         hash[idd] = {}
  699.         for i in 3..args.size-1
  700.           hash[idd][args[i]] = item[args[i]]
  701.         end
  702.       end
  703.     end
  704.     return hash
  705.   end
  706.   #--------------------------------------------------------------------------
  707.   # * Exchange the old array of the database from the new array
  708.   #--------------------------------------------------------------------------
  709.   def change_array(array, param_list)
  710.     return if param_list.nil?
  711.     param_list.each_pair do |key, params|
  712.       next if array[key].nil?
  713.       param_copy(array[key], params)
  714.     end
  715.   end
  716.   #--------------------------------------------------------------------------
  717.   # * Copy a parameter from the localized string to the old string
  718.   #   params: param array
  719.   #--------------------------------------------------------------------------
  720.   def param_copy(item, params)
  721.     params.each_pair do |param, value|
  722.       next if value.nil?
  723.       val = value.gsub("\\n","\n")
  724.       eval("item.#{param.to_s} = val")
  725.     end
  726.   end
  727.   #--------------------------------------------------------------------------
  728.   # * Gets a item from ID
  729.   #   line: ID
  730.   #--------------------------------------------------------------------------
  731.   def get_item_from_parameters(line)
  732.     item = {}
  733.     line.split(";").each do |param|
  734.       if param =~ param_regexp
  735.         key = $1; val = $2
  736.         key.gsub!(/[\t\n]/,""); val.gsub!(/[\t\n]/,"")
  737.         item[key.downcase.to_sym] = val
  738.       end
  739.     end
  740.     return item
  741.   end
  742.   #--------------------------------------------------------------------------
  743.   # * Dialet?
  744.   #   lang: language short name
  745.   #--------------------------------------------------------------------------
  746.   def dialet?(lang)
  747.     return H87Localization.dialet?(lang)
  748.   end
  749.   #--------------------------------------------------------------------------
  750.   # * Loads a string from another language.
  751.   #   string_id: string to load
  752.   #   language: current language
  753.   #--------------------------------------------------------------------------
  754.   def string_from_other_languages(string_id, language)
  755.     if H87Localization.dialet?(language)
  756.       traditional = H87Localization.base_language(language)
  757.       return string_from_other_languages(string_id, traditional)
  758.     elsif language != H87Localization.default_language
  759.       return get_language_string(string_id, H87Localization.default_language)
  760.     else
  761.       return "?#{string_id}?"
  762.     end
  763.   end
  764.   #--------------------------------------------------------------------------
  765.   # * Gets a string from another language and stores it
  766.   #--------------------------------------------------------------------------
  767.   def get_language_string(string_id, language)
  768.     dl = Data_Localization.new(language)
  769.     @strings[string_id] = dl.get_string(string_id)
  770.     return @strings[string_id]
  771.   end
  772. end
  773.  
  774. #--------------------------------------------------------------------------
  775. # * Command to add the language selection from Settings menu.
  776. #--------------------------------------------------------------------------
  777. if $imported["H87_Options"]
  778.   H87Options.push_system_option(H87Localization.create_option)
  779. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement