2023-01-05 12:25:55 +00:00
/**************************************************************************/
/* editor_translation_parser.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
2020-06-23 11:48:59 +00:00
# include "editor_translation_parser.h"
2020-11-07 22:33:38 +00:00
# include "core/error/error_macros.h"
2021-06-11 12:51:48 +00:00
# include "core/io/file_access.h"
2020-11-07 22:33:38 +00:00
# include "core/object/script_language.h"
2022-05-19 15:00:06 +00:00
# include "core/templates/hash_set.h"
2020-06-23 11:48:59 +00:00
EditorTranslationParser * EditorTranslationParser : : singleton = nullptr ;
2020-07-22 22:07:35 +00:00
Error EditorTranslationParserPlugin : : parse_file ( const String & p_path , Vector < String > * r_ids , Vector < Vector < String > > * r_ids_ctx_plural ) {
2022-08-31 17:24:04 +00:00
TypedArray < String > ids ;
TypedArray < Array > ids_ctx_plural ;
2020-07-22 22:07:35 +00:00
2021-08-22 01:52:44 +00:00
if ( GDVIRTUAL_CALL ( _parse_file , p_path , ids , ids_ctx_plural ) ) {
2020-07-22 22:07:35 +00:00
// Add user's extracted translatable messages.
for ( int i = 0 ; i < ids . size ( ) ; i + + ) {
r_ids - > append ( ids [ i ] ) ;
}
// Add user's collected translatable messages with context or plurals.
for ( int i = 0 ; i < ids_ctx_plural . size ( ) ; i + + ) {
Array arr = ids_ctx_plural [ i ] ;
2020-08-07 11:17:12 +00:00
ERR_FAIL_COND_V_MSG ( arr . size ( ) ! = 3 , ERR_INVALID_DATA , " Array entries written into `msgids_context_plural` in `parse_file()` method should have the form [ \" message \" , \" context \" , \" plural message \" ] " ) ;
2020-07-22 22:07:35 +00:00
Vector < String > id_ctx_plural ;
id_ctx_plural . push_back ( arr [ 0 ] ) ;
id_ctx_plural . push_back ( arr [ 1 ] ) ;
id_ctx_plural . push_back ( arr [ 2 ] ) ;
r_ids_ctx_plural - > append ( id_ctx_plural ) ;
2020-06-23 11:48:59 +00:00
}
2020-07-03 18:24:54 +00:00
return OK ;
2020-06-23 11:48:59 +00:00
} else {
2020-07-03 18:24:54 +00:00
ERR_PRINT ( " Custom translation parser plugin's \" func parse_file(path, extracted_strings) \" is undefined. " ) ;
return ERR_UNAVAILABLE ;
2020-06-23 11:48:59 +00:00
}
}
void EditorTranslationParserPlugin : : get_recognized_extensions ( List < String > * r_extensions ) const {
2021-08-22 01:52:44 +00:00
Vector < String > extensions ;
if ( GDVIRTUAL_CALL ( _get_recognized_extensions , extensions ) ) {
2020-06-23 11:48:59 +00:00
for ( int i = 0 ; i < extensions . size ( ) ; i + + ) {
r_extensions - > push_back ( extensions [ i ] ) ;
}
} else {
ERR_PRINT ( " Custom translation parser plugin's \" func get_recognized_extensions() \" is undefined. " ) ;
}
}
void EditorTranslationParserPlugin : : _bind_methods ( ) {
2021-08-22 01:52:44 +00:00
GDVIRTUAL_BIND ( _parse_file , " path " , " msgids " , " msgids_context_plural " ) ;
GDVIRTUAL_BIND ( _get_recognized_extensions ) ;
2020-06-23 11:48:59 +00:00
}
/////////////////////////
void EditorTranslationParser : : get_recognized_extensions ( List < String > * r_extensions ) const {
2022-05-19 15:00:06 +00:00
HashSet < String > extensions ;
2020-06-23 11:48:59 +00:00
List < String > temp ;
for ( int i = 0 ; i < standard_parsers . size ( ) ; i + + ) {
standard_parsers [ i ] - > get_recognized_extensions ( & temp ) ;
}
for ( int i = 0 ; i < custom_parsers . size ( ) ; i + + ) {
custom_parsers [ i ] - > get_recognized_extensions ( & temp ) ;
}
// Remove duplicates.
2024-04-15 13:18:34 +00:00
for ( const String & E : temp ) {
extensions . insert ( E ) ;
2020-06-23 11:48:59 +00:00
}
2022-05-18 23:43:40 +00:00
for ( const String & E : extensions ) {
r_extensions - > push_back ( E ) ;
2020-06-23 11:48:59 +00:00
}
}
bool EditorTranslationParser : : can_parse ( const String & p_extension ) const {
List < String > extensions ;
get_recognized_extensions ( & extensions ) ;
2024-04-15 13:18:34 +00:00
for ( const String & extension : extensions ) {
if ( p_extension = = extension ) {
2020-06-23 11:48:59 +00:00
return true ;
}
}
return false ;
}
Ref < EditorTranslationParserPlugin > EditorTranslationParser : : get_parser ( const String & p_extension ) const {
// Consider user-defined parsers first.
for ( int i = 0 ; i < custom_parsers . size ( ) ; i + + ) {
List < String > temp ;
custom_parsers [ i ] - > get_recognized_extensions ( & temp ) ;
2024-04-15 13:18:34 +00:00
for ( const String & E : temp ) {
if ( E = = p_extension ) {
2020-06-23 11:48:59 +00:00
return custom_parsers [ i ] ;
}
}
}
for ( int i = 0 ; i < standard_parsers . size ( ) ; i + + ) {
List < String > temp ;
standard_parsers [ i ] - > get_recognized_extensions ( & temp ) ;
2024-04-15 13:18:34 +00:00
for ( const String & E : temp ) {
if ( E = = p_extension ) {
2020-06-23 11:48:59 +00:00
return standard_parsers [ i ] ;
}
}
}
WARN_PRINT ( " No translation parser available for \" " + p_extension + " \" extension. " ) ;
return nullptr ;
}
void EditorTranslationParser : : add_parser ( const Ref < EditorTranslationParserPlugin > & p_parser , ParserType p_type ) {
if ( p_type = = ParserType : : STANDARD ) {
standard_parsers . push_back ( p_parser ) ;
} else if ( p_type = = ParserType : : CUSTOM ) {
custom_parsers . push_back ( p_parser ) ;
}
}
void EditorTranslationParser : : remove_parser ( const Ref < EditorTranslationParserPlugin > & p_parser , ParserType p_type ) {
if ( p_type = = ParserType : : STANDARD ) {
standard_parsers . erase ( p_parser ) ;
} else if ( p_type = = ParserType : : CUSTOM ) {
custom_parsers . erase ( p_parser ) ;
}
}
2020-07-27 14:58:21 +00:00
void EditorTranslationParser : : clean_parsers ( ) {
standard_parsers . clear ( ) ;
custom_parsers . clear ( ) ;
}
2020-06-23 11:48:59 +00:00
EditorTranslationParser * EditorTranslationParser : : get_singleton ( ) {
if ( ! singleton ) {
singleton = memnew ( EditorTranslationParser ) ;
}
return singleton ;
}
EditorTranslationParser : : EditorTranslationParser ( ) {
}
EditorTranslationParser : : ~ EditorTranslationParser ( ) {
memdelete ( singleton ) ;
singleton = nullptr ;
}