tneotia/html-editor-enhanced

[QUESTION] Translate

erikmompean opened this issue · 3 comments

Hi, nice plugin!

There is any way to translate the text on link functionality, pictures...dialogs?

Not directly through the plugin at the moment, but you can use the onButtonPressed function to override what happens on insert link/image button press, and add a custom dialog. You could go into the source and copy how I created the dialog and just change the text to be translated.

If you'd like an example let me know and I can cook something up this evening.

Here's an example:

HtmlEditor(
  controller: controller,
  htmlToolbarOptions: HtmlToolbarOptions(
    onButtonPressed: (ButtonType type, bool? status,
        Function()? updateStatus) async {
      if (type == ButtonType.link) {
        final text = TextEditingController();
        final url = TextEditingController();
        final textFocus = FocusNode();
        final urlFocus = FocusNode();
        final formKey = GlobalKey<FormState>();
        var openNewTab = false;
        await showDialog(
            context: context,
            builder: (BuildContext context) {
          return StatefulBuilder(builder:
              (BuildContext context, StateSetter setState) {
            return AlertDialog(
              title: Text('Insert Link'),
              scrollable: true,
              content: Form(
                key: formKey,
                child: Column(
                    mainAxisSize: MainAxisSize.min,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text('Text to display',
                          style: TextStyle(
                              fontWeight: FontWeight.bold)),
                      SizedBox(height: 10),
                      TextField(
                        controller: text,
                        focusNode: textFocus,
                        textInputAction: TextInputAction.next,
                        decoration: InputDecoration(
                          border: OutlineInputBorder(),
                          hintText: 'Text',
                        ),
                        onSubmitted: (_) {
                          urlFocus.requestFocus();
                        },
                      ),
                      SizedBox(height: 20),
                      Text('URL',
                          style: TextStyle(
                              fontWeight: FontWeight.bold)),
                      SizedBox(height: 10),
                      TextFormField(
                        controller: url,
                        focusNode: urlFocus,
                        textInputAction: TextInputAction.done,
                        decoration: InputDecoration(
                          border: OutlineInputBorder(),
                          hintText: 'URL',
                        ),
                        validator: (String? value) {
                          if (value == null || value.isEmpty) {
                            return 'Please enter a URL!';
                          }
                          return null;
                        },
                      ),
                      Row(
                        children: <Widget>[
                          SizedBox(
                            height: 48.0,
                            width: 24.0,
                            child: Checkbox(
                              value: openNewTab,
                              activeColor: Color(0xFF827250),
                              onChanged: (bool? value) {
                                setState(() {
                                  openNewTab = value!;
                                });
                              },
                            ),
                          ),
                          ElevatedButton(
                            style: ElevatedButton.styleFrom(
                                primary: Theme.of(context)
                                    .dialogBackgroundColor,
                                padding: EdgeInsets.only(
                                    left: 5, right: 5),
                                elevation: 0.0),
                            onPressed: () {
                              setState(() {
                                openNewTab = !openNewTab;
                              });
                            },
                            child: Text('Open in new window',
                                style: TextStyle(
                                    color: Theme.of(context)
                                        .textTheme
                                        .bodyText1
                                        ?.color)),
                          ),
                        ],
                      ),
                    ]),
              ),
              actions: [
                TextButton(
                  onPressed: () {
                    Navigator.of(context).pop();
                  },
                  child: Text('Cancel'),
                ),
                TextButton(
                  onPressed: () async {
                    if (formKey.currentState!.validate()) {
                      controller.insertLink(
                        text.text.isEmpty ? url.text : text.text,
                        url.text,
                        openNewTab,
                      );
                      Navigator.of(context).pop();
                    }
                  },
                  child: Text('OK'),
                )
              ],
            );
          });
        });
        return false;
      }
      return true;
    },
  ),
),

So that overrides the default dialog and then you can customize it however you want - if you just want to translate then you can edit the strings in the example.

Closing for now, hope it helped.