Advertisement
Iavra

[Ace] Game Localization - Messages Module

May 28th, 2015
511
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 4.86 KB | None | 0 0
  1. #==============================================================================
  2. # Iavra Game Localization - Messages Module 1.00
  3. # -----------------------------------------------------------------------------
  4. # Description:
  5. # Adds a new message code that can be used to load localized messages.
  6. # -----------------------------------------------------------------------------
  7. # Prerequisites:
  8. # Iavra Game Localization - Core Engine
  9. # -----------------------------------------------------------------------------
  10. # How to Use:
  11. # Language files are built after the following format:
  12. #
  13. # {
  14. #   :messages => {
  15. #     :key => "Localized message"
  16. #   }
  17. # }
  18. #
  19. # All message codes can be used, but backslashes need to be doubled.
  20. # -----------------------------------------------------------------------------
  21. # Terms of Use:
  22. # Free to use for both commercial and non-commercial games. Please give credit.
  23. # -----------------------------------------------------------------------------
  24. # Credits:
  25. # Iavra
  26. # -----------------------------------------------------------------------------
  27. # Changelog:
  28. # - 1.00: Release version.
  29. #==============================================================================
  30.  
  31. ($imported ||= {})[:iavra_i18n_messages] = true
  32.  
  33. fail "This module needs the core engine to function." unless $imported[:iavra_i18n_core]
  34.  
  35. #==============================================================================
  36. # ▼ IAVRA::I18N::MESSAGES
  37. #==============================================================================
  38.  
  39. module IAVRA
  40.     module I18N
  41.         module MESSAGES
  42.            
  43.             #========================================================================
  44.             # ■ ■ ■ ■ ■ CONFIGURATION ■ ■ ■ ■ ■
  45.             #========================================================================
  46.            
  47.             #========================================================================
  48.             # The messagecode that should be replaces by localized messages. Can be
  49.             # changed to avoid conflits with other scripts.
  50.             #========================================================================
  51.            
  52.             MESSAGECODE = ":"
  53.            
  54.             #========================================================================
  55.             # If true, localized messages can in turn include other messages. Be
  56.             # careful to not create a loop, since it will cause your game to crash.
  57.             # If this is false, message codes will instead be replaced by empty
  58.             # string (other codes like \\v[1] will always work).
  59.             #========================================================================
  60.            
  61.             SUB_INCLUDES = false
  62.            
  63.             #========================================================================
  64.             # ■ ■ ■ ■ ■ CONFIGURATION ■ ■ ■ ■ ■
  65.             #========================================================================
  66.            
  67.             #========================================================================
  68.             # The category used inside language files to indicate messages.
  69.             #========================================================================
  70.            
  71.             CATEGORY = :messages
  72.            
  73.             #========================================================================
  74.             # Building the regex
  75.             #========================================================================
  76.            
  77.             REGEX = /\\#{MESSAGECODE}\[(.+)\]/i
  78.            
  79.             #==========================================================================
  80.             # Just a shorthand method.
  81.             #==========================================================================
  82.            
  83.             def self.[](sym)
  84.                 IAVRA::I18N[CATEGORY][sym]
  85.             end
  86.            
  87.         end        
  88.     end
  89. end
  90.  
  91. #==============================================================================
  92. # ▼ Window_Base
  93. #==============================================================================
  94.  
  95. class Window_Base < Window
  96.    
  97.     alias :iavra_i18n_messages_convert_escape_characters :convert_escape_characters
  98.    
  99.     #============================================================================
  100.     # make sure to call our own convert method, too.
  101.     #============================================================================
  102.    
  103.     def convert_escape_characters(text)
  104.         result = iavra_i18n_messages_convert_messagecode(text)
  105.         result = iavra_i18n_messages_convert_escape_characters(result)
  106.         result
  107.     end
  108.    
  109.     #============================================================================
  110.     # Converts our messagecode to a localized messages.
  111.     # If SUB_INCLUDES is set to true, this is done recursively. If not,
  112.     # subincludes are replaced with empty strings, instead.
  113.     #============================================================================
  114.    
  115.     def iavra_i18n_messages_convert_messagecode(text)
  116.         text.gsub(IAVRA::I18N::MESSAGES::REGEX) {
  117.             result = IAVRA::I18N::MESSAGES[$1.to_sym] || ""
  118.             IAVRA::I18N::MESSAGES::SUB_INCLUDES ?
  119.                 iavra_i18n_messages_convert_messagecode(result) :
  120.                 result.gsub(IAVRA::I18N::MESSAGES::REGEX, "")
  121.         }
  122.     end
  123.    
  124. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement