From 396f2eee827293d9b096e6fc954a57c92bf21f95 Mon Sep 17 00:00:00 2001 From: SkyJJ Date: Thu, 23 Jul 2020 00:07:35 +0200 Subject: Update POT generation to handle context and plurals --- doc/classes/EditorTranslationParserPlugin.xml | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) (limited to 'doc/classes') diff --git a/doc/classes/EditorTranslationParserPlugin.xml b/doc/classes/EditorTranslationParserPlugin.xml index d40fc558de..eaa678a25a 100644 --- a/doc/classes/EditorTranslationParserPlugin.xml +++ b/doc/classes/EditorTranslationParserPlugin.xml @@ -5,30 +5,37 @@ Plugins are registered via [method EditorPlugin.add_translation_parser_plugin] method. To define the parsing and string extraction logic, override the [method parse_file] method in script. + Add the extracted strings to argument [code]msgids[/code] or [code]msgids_context_plural[/code] if context or plural is used. The extracted strings will be written into a POT file selected by user under "POT Generation" in "Localization" tab in "Project Settings" menu. - Below shows an example of a custom parser that extracts strings in a CSV file to write into a POT. + Below shows an example of a custom parser that extracts strings from a CSV file to write into a POT. [codeblock] tool extends EditorTranslationParserPlugin - func parse_file(path, extracted_strings): + func parse_file(path, msgids, msgids_context_plural): var file = File.new() file.open(path, File.READ) var text = file.get_as_text() var split_strs = text.split(",", false, 0) for s in split_strs: - extracted_strings.append(s) + msgids.append(s) #print("Extracted string: " + s) func get_recognized_extensions(): return ["csv"] [/codeblock] + To add a translatable string associated with context or plural, add it to [code]msgids_context_plural[/code]: + [codeblock] + msgids_ctx_plural.append(["Test 1", "context", "test 1 plurals"]) # This will add a message with msgid "Test 1", msgctxt "context", and msgid_plural "test 1 plurals". + msgids_ctx_plural.append(["A test without context", "", "plurals"]) # This will add a message with msgid "A test without context" and msgid_plural "plurals". + msgids_ctx_plural.append(["Only with context", "a friendly context", ""]) # This will add a message with msgid "Only with context" and msgctxt "a friendly context". + [/codeblock] [b]Note:[/b] If you override parsing logic for standard script types (GDScript, C#, etc.), it would be better to load the [code]path[/code] argument using [method ResourceLoader.load]. This is because built-in scripts are loaded as [Resource] type, not [File] type. For example: [codeblock] - func parse_file(path, extracted_strings): + func parse_file(path, msgids, msgids_context_plural): var res = ResourceLoader.load(path, "Script") var text = res.get_source_code() # Parsing logic. @@ -53,7 +60,9 @@ - + + + Override this method to define a custom parsing logic to extract the translatable strings. -- cgit v1.2.3 From 0ef758eaeeb5b2f44e132865f0f10baf692e972f Mon Sep 17 00:00:00 2001 From: SkyJJ Date: Fri, 7 Aug 2020 13:17:12 +0200 Subject: Updated Translation architecture to have TranslationPO, did some commit fixes and updated class Reference. --- doc/classes/EditorTranslationParserPlugin.xml | 10 +++++--- doc/classes/Object.xml | 26 +++++++++++++++++-- doc/classes/Translation.xml | 37 +++++++++++++++++++++++++++ doc/classes/TranslationServer.xml | 30 +++++++++++++++++++++- 4 files changed, 97 insertions(+), 6 deletions(-) (limited to 'doc/classes') diff --git a/doc/classes/EditorTranslationParserPlugin.xml b/doc/classes/EditorTranslationParserPlugin.xml index eaa678a25a..f5204e7bab 100644 --- a/doc/classes/EditorTranslationParserPlugin.xml +++ b/doc/classes/EditorTranslationParserPlugin.xml @@ -6,6 +6,7 @@ Plugins are registered via [method EditorPlugin.add_translation_parser_plugin] method. To define the parsing and string extraction logic, override the [method parse_file] method in script. Add the extracted strings to argument [code]msgids[/code] or [code]msgids_context_plural[/code] if context or plural is used. + When adding to [code]msgids_context_plural[/code], you must add the data using the format [code]["A", "B", "C"][/code], where [code]A[/code] represents the extracted string, [code]B[/code] represents the context, and [code]C[/code] represents the plural version of the extracted string. If you want to add only context but not plural, put [code]""[/code] for the plural slot. The idea is the same if you only want to add plural but not context. See the code below for concrete examples. The extracted strings will be written into a POT file selected by user under "POT Generation" in "Localization" tab in "Project Settings" menu. Below shows an example of a custom parser that extracts strings from a CSV file to write into a POT. [codeblock] @@ -28,9 +29,12 @@ [/codeblock] To add a translatable string associated with context or plural, add it to [code]msgids_context_plural[/code]: [codeblock] - msgids_ctx_plural.append(["Test 1", "context", "test 1 plurals"]) # This will add a message with msgid "Test 1", msgctxt "context", and msgid_plural "test 1 plurals". - msgids_ctx_plural.append(["A test without context", "", "plurals"]) # This will add a message with msgid "A test without context" and msgid_plural "plurals". - msgids_ctx_plural.append(["Only with context", "a friendly context", ""]) # This will add a message with msgid "Only with context" and msgctxt "a friendly context". + # This will add a message with msgid "Test 1", msgctxt "context", and msgid_plural "test 1 plurals". + msgids_context_plural.append(["Test 1", "context", "test 1 plurals"]) + # This will add a message with msgid "A test without context" and msgid_plural "plurals". + msgids_context_plural.append(["A test without context", "", "plurals"]) + # This will add a message with msgid "Only with context" and msgctxt "a friendly context". + msgids_context_plural.append(["Only with context", "a friendly context", ""]) [/codeblock] [b]Note:[/b] If you override parsing logic for standard script types (GDScript, C#, etc.), it would be better to load the [code]path[/code] argument using [method ResourceLoader.load]. This is because built-in scripts are loaded as [Resource] type, not [File] type. For example: diff --git a/doc/classes/Object.xml b/doc/classes/Object.xml index ca6b624359..aa6111df6c 100644 --- a/doc/classes/Object.xml +++ b/doc/classes/Object.xml @@ -486,13 +486,35 @@ - + + + - Translates a message using translation catalogs configured in the Project Settings. + Translates a message using translation catalogs configured in the Project Settings. An additional context could be used to specify the translation context. Only works if message translation is enabled (which it is by default), otherwise it returns the [code]message[/code] unchanged. See [method set_message_translation]. + See https://docs.godotengine.org/en/latest/tutorials/i18n/internationalizing_games.html for examples of the usage of this method. + + + + + + + + + + + + + + + Translates a message involving plurals using translation catalogs configured in the Project Settings. An additional context could be used to specify the translation context. + Only works if message translation is enabled (which it is by default), otherwise it returns the [code]message[/code] or [code]plural_message[/code] unchanged. See [method set_message_translation]. + The number [code]n[/code] is the number or quantity of the plural object. It will be used to guide the translation system to fetch the correct plural form for the selected language. + [b]Note:[/b] Negative and floating-point values usually represent physical entities for which singular and plural don't clearly apply. In such cases, use [method tr]. + See https://docs.godotengine.org/en/latest/tutorials/i18n/internationalizing_games.html for examples of the usage of this method. diff --git a/doc/classes/Translation.xml b/doc/classes/Translation.xml index 11245195bf..1989a63362 100644 --- a/doc/classes/Translation.xml +++ b/doc/classes/Translation.xml @@ -18,8 +18,25 @@ + + Adds a message if nonexistent, followed by its translation. + An additional context could be used to specify the translation context or differentiate polysemic words. + + + + + + + + + + + + + Adds a message involving plural translation if nonexistent, followed by its translation. + An additional context could be used to specify the translation context or differentiate polysemic words. @@ -27,6 +44,8 @@ + + Erases a message. @@ -36,6 +55,8 @@ + + Returns a message's translation. @@ -54,6 +75,22 @@ Returns all the messages (keys). + + + + + + + + + + + + + Returns a message's translation involving plurals. + The number [code]n[/code] is the number or quantity of the plural object. It will be used to guide the translation system to fetch the correct plural form for the selected language. + + diff --git a/doc/classes/TranslationServer.xml b/doc/classes/TranslationServer.xml index aaf7a4d160..3369663af6 100644 --- a/doc/classes/TranslationServer.xml +++ b/doc/classes/TranslationServer.xml @@ -50,6 +50,16 @@ Returns a locale's language and its variant (e.g. [code]"en_US"[/code] would return [code]"English (United States)"[/code]). + + + + + + + Returns the [Translation] instance based on the [code]locale[/code] passed in. + It will return a [code]nullptr[/code] if there is no [Translation] instance that matches the [code]locale[/code]. + + @@ -73,8 +83,26 @@ + + + + Returns the current locale's translation for the given message (key) and context. + + + + + + + + + + + + + - Returns the current locale's translation for the given message (key). + Returns the current locale's translation for the given message (key), plural_message and context. + The number [code]n[/code] is the number or quantity of the plural object. It will be used to guide the translation system to fetch the correct plural form for the selected language. -- cgit v1.2.3 From ce3461dc88fb4abc9460328b06502890e06f50d6 Mon Sep 17 00:00:00 2001 From: SkyJJ Date: Wed, 12 Aug 2020 22:52:17 +0200 Subject: Update GDScriptTranslationParserPlugin to use GDSriptParser instead of RegEx. --- doc/classes/Object.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'doc/classes') diff --git a/doc/classes/Object.xml b/doc/classes/Object.xml index aa6111df6c..077067a0f4 100644 --- a/doc/classes/Object.xml +++ b/doc/classes/Object.xml @@ -514,7 +514,7 @@ Only works if message translation is enabled (which it is by default), otherwise it returns the [code]message[/code] or [code]plural_message[/code] unchanged. See [method set_message_translation]. The number [code]n[/code] is the number or quantity of the plural object. It will be used to guide the translation system to fetch the correct plural form for the selected language. [b]Note:[/b] Negative and floating-point values usually represent physical entities for which singular and plural don't clearly apply. In such cases, use [method tr]. - See https://docs.godotengine.org/en/latest/tutorials/i18n/internationalizing_games.html for examples of the usage of this method. + See https://docs.godotengine.org/en/latest/tutorials/i18n/localization_using_gettext.html for examples of the usage of this method. -- cgit v1.2.3