Iavra

Iavra Localization - Menu

Dec 24th, 2015 (edited)
4,302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*:
  2.  * @plugindesc Adds a "Language" option to the options menu, that can be used to change game language on the fly.
  3.  * <Iavra Localization Menu>
  4.  * @author Iavra
  5.  *
  6.  * @param Option Label
  7.  * @desc Label to be used for the option in the options menu.
  8.  * @default Language
  9.  *
  10.  * @param Language Labels
  11.  * @desc Comma-separated list of "language:label" pairs, to be used for the language selection.
  12.  * @default en:English
  13.  *
  14.  * @help
  15.  * Simply place this plugin in your plugin list after "Iavra Localization - Core". The parameters "Option Label" and
  16.  * "Language Labels" are used to define the text to be shown. You are free to use escape codes inside those parameters,
  17.  * as well, though i recommend against localizing the language labels.
  18.  */
  19.  
  20. var IAVRA = IAVRA || {};
  21. if(!IAVRA.I18N) { throw new Error("This plugin needs 'Iavra Localization - Core' to work."); }
  22.  
  23. (function($) {
  24.     "use strict";
  25.  
  26.     /**
  27.      * Read plugin parameters independent from the plugin's actual filename.
  28.      */
  29.     var _p = $plugins.filter(function(p) { return p.description.contains('<Iavra Localization Menu>'); })[0].parameters;
  30.     var _param_labelOption = _p['Option Label'];
  31.     var _param_labelLanguages = _p['Language Labels'].split(/\s*,\s*/).reduce(function(map, entry) {
  32.         var split = entry.split(':'); if(split.length === 2) { map[split[0]] = split[1]; } return map;
  33.     }, {});
  34.  
  35.     /**
  36.      * Returns the previous language in the list or the last language, if the current one is already the first.
  37.      */
  38.     var _prevLanguage = function(language) {
  39.         var languages = $.I18N.languages(), index = languages.indexOf(language) + 1;
  40.         return languages[index >= languages.length ? 0 : index];
  41.     };
  42.  
  43.     /**
  44.      * Returns the next language in the list or the first language, of the current one is already the last.
  45.      */
  46.     var _nextLanguage = function(language) {
  47.         var languages = $.I18N.languages(), index = languages.indexOf(language) - 1;
  48.         return languages[index < 0 ? languages.length - 1 : index];
  49.     };
  50.  
  51.     /**
  52.      * Applies the given language and returns it or the default fallback, if the given value was invalid. We don't
  53.      * directly change the language, after it was selected in the options menu, because otherwise you would see a
  54.      * mixed of different languages, while changing options.
  55.      */
  56.     var _applyLanguage = function(language) {
  57.         IAVRA.I18N.language = language;
  58.         return IAVRA.I18N.language;
  59.     };
  60.  
  61.     //=============================================================================
  62.     // Window_Options
  63.     //=============================================================================
  64.  
  65.     (function($) {
  66.  
  67.         /**
  68.          * Adds our command to the command list.
  69.          */
  70.         var alias_makeCommandList = $.prototype.makeCommandList;
  71.         $.prototype.makeCommandList = function() {
  72.             alias_makeCommandList.call(this);
  73.             this.addCommand(_param_labelOption, 'language');
  74.         };
  75.  
  76.         /**
  77.          * Returns the status text as given in the "Language Labels" parameter.
  78.          */
  79.         var alias_statusText = $.prototype.statusText;
  80.         $.prototype.statusText = function(index) {
  81.             var symbol = this.commandSymbol(index);
  82.             var value = this.getConfigValue(symbol);
  83.             return symbol === 'language' ? _param_labelLanguages[value] : alias_statusText.call(this, index);
  84.         };
  85.  
  86.         /**
  87.          * On Ok, select the next language.
  88.          */
  89.         var alias_processOk = $.prototype.processOk;
  90.         $.prototype.processOk = function() {
  91.             var symbol = this.commandSymbol(this.index()), value = this.getConfigValue(symbol);
  92.             if(symbol === 'language') { return this.changeValue(symbol, _nextLanguage(value)); }
  93.             alias_processOk.call(this);
  94.         };
  95.  
  96.         /**
  97.          * On Left, select the previous language.
  98.          */
  99.         var alias_cursorLeft = $.prototype.cursorLeft;
  100.         $.prototype.cursorLeft = function(wrap) {
  101.             var symbol = this.commandSymbol(this.index()), value = this.getConfigValue(symbol);
  102.             if(symbol === 'language') { return this.changeValue(symbol, _prevLanguage(value)); }
  103.             alias_cursorLeft.call(this, wrap);
  104.         };
  105.  
  106.         /**
  107.          * On right, select the next language.
  108.          */
  109.         var alias_cursorRight = $.prototype.cursorRight;
  110.         $.prototype.cursorRight = function(wrap) {
  111.             var symbol = this.commandSymbol(this.index()), value = this.getConfigValue(symbol);
  112.             if(symbol === 'language') { return this.changeValue(symbol, _nextLanguage(value)); }
  113.             alias_cursorRight.call(this, wrap);
  114.         };
  115.  
  116.     })(Window_Options);
  117.  
  118.     //=============================================================================
  119.     // ConfigManager
  120.     //=============================================================================
  121.  
  122.     (function($) {
  123.  
  124.         /**
  125.          * The current language. Automatically gets kept in sync with the core plugin, whenever the current language
  126.          * is written to or read from the config savefile.
  127.          */
  128.         var _language;
  129.  
  130.         /**
  131.          * Store the currently selected language in the config savefile, so it's persisted between games. Before
  132.          * saving the language, it gets applied, so we can correctly fallback to the default language, if needed.
  133.          */
  134.         var alias_makeData = $.makeData;
  135.         $.makeData = function() {
  136.             var config = alias_makeData.call(this);
  137.             config.language = this.language = _applyLanguage(this.language);
  138.             return config;
  139.         };
  140.  
  141.         /**
  142.          * Load the current language from the config savefile. Also, apply it, so we can display the correct language
  143.          * and fallback to the default language, if the savefile entry doesn't exist or is invalid.
  144.          */
  145.         var alias_applyData = $.applyData;
  146.         $.applyData = function(config) {
  147.             alias_applyData.call(this, config);
  148.             this.language = _applyLanguage(config.language);
  149.         };
  150.  
  151.         /**
  152.          * Read and set the current language. Language is managed locally, because otherwise we would experience some
  153.          * strange sideffects in the options menu.
  154.          */
  155.         Object.defineProperty($, 'language', {
  156.             get: function() { return _language; },
  157.             set: function(value) { _language = value; }
  158.         });
  159.  
  160.     })(ConfigManager);
  161.  
  162. })(IAVRA);
Add Comment
Please, Sign In to add comment