└── addons └── translations_from_json ├── plugin.cfg ├── plugin.gd ├── LICENSE ├── README.md └── import_plugin.gd /addons/translations_from_json/plugin.cfg: -------------------------------------------------------------------------------- 1 | [plugin] 2 | 3 | name="TranslationsFromJson" 4 | description="Load translation texts from JSON." 5 | author="AliYil" 6 | version="2.0" 7 | script="plugin.gd" 8 | -------------------------------------------------------------------------------- /addons/translations_from_json/plugin.gd: -------------------------------------------------------------------------------- 1 | @tool 2 | extends EditorPlugin 3 | 4 | var import_plugin 5 | 6 | func _enter_tree(): 7 | import_plugin = preload("import_plugin.gd").new() 8 | add_import_plugin(import_plugin) 9 | pass 10 | 11 | 12 | func _exit_tree(): 13 | remove_import_plugin(import_plugin) 14 | import_plugin = null 15 | pass 16 | -------------------------------------------------------------------------------- /addons/translations_from_json/LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 AliYil 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /addons/translations_from_json/README.md: -------------------------------------------------------------------------------- 1 | # Translations From JSON for Godot 4.x 2 | 3 | A Godot plugin for importing JSON files for translations. 4 | 5 | This is the Godot 4.x version of the plugin. If you want to use it on 3.x, head over to the [3.x branch](https://github.com/AliYil/TranslationsFromJson/tree/3.x). 6 | 7 | ## Usage 8 | 9 | 1. [Install the plugin into your project](https://docs.godotengine.org/en/4.0/tutorials/plugins/editor/installing_plugins.html) and activate it. 10 | 2. Add JSON translations files into your project in .json format. 11 | 3. Make sure the JSON files are imported as "JSON Translation". 12 | 4. Add your JSON files to translations using Project Settings > Localization > Translations > Add *(Change "All Recognized" to "All files (\*)" to see the JSON file)* 13 | 14 | ## JSON Format 15 | 16 | The json data in file must be an object with locale and messages keys. 17 | 18 | - "locale" field signify which locale the texts are in. 19 | - "message" field must be an object that contains keys and corresponding text as string value. 20 | 21 | ### Example JSON 22 | 23 | ```json 24 | { 25 | "locale": "en", 26 | "messages": { 27 | "HELLOWORLD": "Hello world!", 28 | "ANOTHERTEXT": "This is another text." 29 | } 30 | } 31 | ``` 32 | -------------------------------------------------------------------------------- /addons/translations_from_json/import_plugin.gd: -------------------------------------------------------------------------------- 1 | @tool 2 | extends EditorImportPlugin 3 | 4 | func _get_importer_name(): 5 | return "json_translations_importer" 6 | 7 | func _get_visible_name(): 8 | return "JSON Translation" 9 | 10 | func _get_recognized_extensions(): 11 | return ["json"] 12 | 13 | func _get_save_extension(): 14 | return "translation" 15 | 16 | func _get_resource_type(): 17 | return "Translation" 18 | 19 | func _get_preset_count(): 20 | return 1 21 | 22 | func _get_preset_name(preset): 23 | return "Default" 24 | 25 | func _get_import_options(path, preset_index): 26 | return [] 27 | 28 | func _get_import_order(): 29 | return 0 30 | 31 | func _get_priority(): 32 | return 1 33 | 34 | func _import(source_file, save_path, options, r_platform_variants, r_gen_files): 35 | var text = FileAccess.get_file_as_string(source_file) 36 | 37 | var test_json_conv = JSON.new() 38 | test_json_conv.parse(text) 39 | var json = test_json_conv.get_data() 40 | 41 | var translation = Translation.new() 42 | translation.locale = json["locale"] 43 | 44 | var messages = json["messages"] 45 | for messageKey in messages.keys(): 46 | translation.add_message(messageKey, messages[messageKey]) 47 | 48 | return ResourceSaver.save(translation, "%s.%s" % [save_path, _get_save_extension()]) 49 | --------------------------------------------------------------------------------