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
-
Set the Target Language:
Call ComponentsTranslate.TargetLanguage("es")
-
Translate Text:
Call ComponentsTranslate.ReplaceTexts("Submit")
-
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
Event fired when translations are successfully loaded.
Parameter | Type |
---|---|
success | boolean |
message | text |
OnJsonLoadError
Event fired when there is an error loading the JSON file.
Parameter | Type |
---|---|
errorMessage | text |
Methods:
LoadTranslations
Load translations from a JSON file named 'translations.json' located in the assets directory.
Parameter | Type |
---|---|
fileName | text |
ReplaceTexts
Translate a given text based on the loaded JSON file.
Parameter | Type |
---|---|
text | text |
Return Type: text
Properties:
TargetLanguage
Set the target language for translations (e.g., 'en' for English, 'es' for Spanish).
TargetLanguage
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
-
AIX (Generated with Fast Cli ): com.bosonshiggs.componentstranslate.aix (80.3 KB)
-
AIA: ComponentsTranslate.aia (84.2 KB)
-
JSON FILE: translations.json (941 Bytes)
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.