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.
- 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.
- 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"
}
}- Download the
.aixfile of the extension and import it into your App Inventor project.
Use the LoadTranslations block to load the JSON file:
When Screen1.Initialize
Call ComponentsTranslate.LoadTranslations("translations.json")
Call ComponentsTranslate.TargetLanguage("pt")
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.
- Description: Loads a JSON file with translations from the app's assets.
- Parameters:
fileName(String) - The name of the JSON file in the assets.
- Description: Translates the given text based on the loaded translations.
- Parameters:
text(String) - The text to be translated. - Returns: Translated text (String).
- Description: Sets or retrieves the target language for translations.
- Parameters:
language(String) - The language code (e.g.,en,pt,fr).
If a translation is not found, the original text will be returned. Events such as OnTranslationsLoaded and OnJsonLoadError provide feedback on the loading process.
-
Set the Target Language:
Call ComponentsTranslate.TargetLanguage("es") -
Translate Text:
Call ComponentsTranslate.ReplaceTexts("Submit") -
Switch Languages:
Call ComponentsTranslate.TargetLanguage("fr")
- For
es:"Enviar" - For
fr:"Soumettre"
The extension uses SharedPreferences to map translated text back to its original content dynamically. This ensures translations work regardless of the current language.
- Each key represents the original text.
- Each value contains a mapping of language codes to translations.
Contributions are welcome! Feel free to open issues or submit pull requests to improve the functionality or documentation.
This extension is licensed under the MIT License.