[F/OS] ComponentsTranslate: Dynamic Component Translation for App Inventor

ComponentsTranslate Extension

The ComponentsTranslate extension enables dynamic translation of text in your App Inventor components using a JSON file containing translations. The extension supports multiple languages and allows you to switch between languages seamlessly.


Features

  • Translate text in components dynamically.
  • Support for multiple languages using a JSON file.
  • Switch between languages without restarting the app.
  • Automatically map translated text back to its original content.

How to Use

1. Load the JSON File

  • Create a JSON file with translations.
  • Add the file to your Assets in App Inventor.

Example JSON file (translations.json):

{
  "Welcome to the App": {
    "en": "Welcome to the App",
    "es": "Bienvenido a la Aplicación",
    "fr": "Bienvenue dans l'Application",
    "pt": "Bem-vindo ao Aplicativo"
  },
  "Submit": {
    "en": "Submit",
    "es": "Enviar",
    "fr": "Soumettre",
    "pt": "Enviar"
  },
  "Cancel": {
    "en": "Cancel",
    "es": "Cancelar",
    "fr": "Annuler",
    "pt": "Cancelar"
  }
}

2. Add the Extension

  • Download the .aix file of the extension and import it into your App Inventor project.

3. Initialize the Extension

Use the LoadTranslations block to load the JSON file:

When Screen1.Initialize
    Call ComponentsTranslate.LoadTranslations("translations.json")
    Call ComponentsTranslate.TargetLanguage("pt")

4. Translate Text Dynamically

Translate a given text using the ReplaceTexts function:

When Button1.Click
    Set Label1.Text to Call ComponentsTranslate.ReplaceTexts("Welcome to the App")

The text will be translated based on the current target language.


Block Details

LoadTranslations

  • Description: Loads a JSON file with translations from the app's assets.
  • Parameters: fileName (String) - The name of the JSON file in the assets.

ReplaceTexts

  • Description: Translates the given text based on the loaded translations.
  • Parameters: text (String) - The text to be translated.
  • Returns: Translated text (String).

TargetLanguage

  • Description: Sets or retrieves the target language for translations.
  • Parameters: language (String) - The language code (e.g., en, pt, fr).

Error Handling

If a translation is not found, the original text will be returned. Events such as OnTranslationsLoaded and OnJsonLoadError provide feedback on the loading process.


Example Usage

Workflow in App Inventor

  1. Set the Target Language:

    Call ComponentsTranslate.TargetLanguage("es")
    
  2. Translate Text:

    Call ComponentsTranslate.ReplaceTexts("Submit")
    
  3. Switch Languages:

    Call ComponentsTranslate.TargetLanguage("fr")
    

Result

  • For es: "Enviar"
  • For fr: "Soumettre"

Developer Notes

Persistent Storage

The extension uses SharedPreferences to map translated text back to its original content dynamically. This ensures translations work regardless of the current language.

JSON File Format

  • Each key represents the original text.
  • Each value contains a mapping of language codes to translations.

Blocks

ComponentsTranslate

Extension to translate components using a JSON file with translations.

Events:

OnTranslationsLoaded

OnTranslationsLoadedBlock

Event fired when translations are successfully loaded.

Parameter Type
success boolean
message text

OnJsonLoadError

OnJsonLoadErrorBlock

Event fired when there is an error loading the JSON file.

Parameter Type
errorMessage text

Methods:

LoadTranslations

LoadTranslationsBlock

Load translations from a JSON file named 'translations.json' located in the assets directory.

Parameter Type
fileName text

ReplaceTexts

ReplaceTextsBlock

Translate a given text based on the loaded JSON file.

Parameter Type
text text

Return Type: text

Properties:

TargetLanguage

TargetLanguageBlock

Set the target language for translations (e.g., 'en' for English, 'es' for Spanish).

TargetLanguage

TargetLanguageBlock (1)

Set the target language for translations (e.g., 'en' for English, 'es' for Spanish).

This documentation was generated using Akshat Developer's Documentation Generator.

Files

Contributing

Contributions are welcome! Feel free to open issues or submit pull requests to improve the functionality or documentation.


License

This extension is licensed under the MIT.

5 Likes

I did not find translations.json in your demo aia.

And your translations procedure looks not working:
If you set language to es, then the buttons's text will be in es, not in en. Then when you tried to change language again, the button's text (in es) will not be a key in your json.

1 Like

I have also developed a similar extension, which also offers theming options.

1 Like

See the new files

NEWS

Now you can use a JSON file of assets or a WEB file like Google Drive or WEB file with direct link to the file.

Hello,

I really like this extension.

Unfortunately, with this extension a project can't be built.

Could you please look in to it to get it work with App Inventor again?

Thanks!!

--
Best, Pál

Hello!

I can't build neither apk nor aab file of my project.
The error messages are just these:

App Inventor is unable to compile this project.
The compiler error output was
[ReadBuildInfo] Starting Task
[ReadBuildInfo] Task errored
.

Equiv_Ratio_Solver.aia (277.5 KB)

What colud be here the problem?
How can I solve it?

--
Best, from Pal

image
Equiv_Ratio_Solver1_1.aia (276.5 KB)

test this aia..

You removed from WhenScreen1.Initialize block the
ComponentsTranslate1.TargetLanguage to TaifunTools1.Language
and from the additional blocks the total of ComponentsTranslate1 block too.

Now I can do the build of this .aia too.

Why? What is wrong with these ComponentsTranslate1 blocks?

This Component causes the error to not to build. May be multiple extensions used same lib

But I have managed to build an apk file with these additions in the same project before!
Then I just added some more ComponentsTranslate1 blocks.

Then I got this error that the apk is not being built.

Can we find the cause of this error for sure?
What is the solution to this problem other than not using the ComponentsTranslate1 extension in this case?

use logcat to find the exact cause of the error

Edit: But see, I have tested the extension with an empty project, but still it throws such error. The extension developer should have to tell about this error but meanwhile logcat should have to be produced.

I have tested in kodular project also with an empty project but still there too i got the same error
image

@Passos_0213 , Can you look in to this?

I will do that, but don't know how to use logcat in this case?

Already @Taifun added a guide

Thanks for the report. I'll check if it's some deprecated library.

1 Like