Proper way of Link generation from string How can I generate a link in a proper way in Drupal 8? I have a field where the user can say an internal or external page/website.

Internal:
/node/6
/articles
/articles#Subheading
etc.

External:
www.google.com
//www.google.com
https://www.google.com

How can I transform these into a link with the following condition:
the site root is: www.mydomain.com/mysite/
internal links should have language versions: i.e. en/articles or pt/articles .

I've tried with Link::fromTextAndUrl('My link', Url::fromUri('internal:/articles')), but this requires a URI which is not my goal and it doesn't follow the selected user language.
You do not need internal:/ you just need to have some help text that says an internal path should begin with a slash.

After that, when you create the links, you can do:

use Drupal\Core\Link;
use Drupal\Core\Url;
use Drupal\Component\Utility\UrlHelper;

....
....
....

function or method.... {
  $path = // get config value
  $label = // get config value

  if (UrlHelper::isExternal($path)) {
    $url = Url::fromUri($path);
  } else {
    $url = Url::fromUserInput($path);
  }

  return Link::fromTextAndUrl($label, $url);
}


Of course I am assuming there is always a value... you'd have to add your own checks so you don't get errors calling Url:: for null or empty input, etc.

The return will be an renderable array to use. I am using a similar method to allow input for third party settings on a config entity. This way the value can be internal or external.