Why is this if statement not working in twig?
The variable html_attributes is not available in a page template. To make it available copy this code from template_preprocess_html() to your page preprocess hook:
use Drupal\Core\Template\Attribute;

/**
 * Implements hook_preprocess_page().
 */
function mytheme_preprocess_page(&amp;$variables) {

  $variables['html_attributes'] = new Attribute();
  $language_interface = \Drupal::languageManager()-&gt;getCurrentLanguage();
  $variables['html_attributes']['lang'] = $language_interface-&gt;getId();
  $variables['html_attributes']['dir'] = $language_interface-&gt;getDirection();

}