Use lang attribute

This commit is contained in:
Simounet 2019-01-11 21:09:49 +01:00
parent 7937ed0c20
commit 416d44d0ae
No known key found for this signature in database
GPG key ID: 77D3B7DC794EB770
5 changed files with 51 additions and 9 deletions

View file

@ -787,6 +787,19 @@ class Entry
return $this->language; return $this->language;
} }
/**
* Format the entry language to a valid html lang attribute.
*/
public function getHTMLLanguage()
{
$parsedLocale = \Locale::parseLocale($this->getLanguage());
$lang = '';
$lang .= $parsedLocale['language'] ?? '';
$lang .= isset($parsedLocale['region']) ? '-' . $parsedLocale['region'] : '';
return $lang;
}
/** /**
* @return string|null * @return string|null
*/ */

View file

@ -1,9 +1,10 @@
<!DOCTYPE html> <!DOCTYPE html>
<!--[if lte IE 6]><html class="no-js ie6 ie67 ie678" lang="en"><![endif]--> {% set lang = app.request.locale|default('') -%}
<!--[if lte IE 7]><html class="no-js ie7 ie67 ie678" lang="en"><![endif]--> <!--[if lte IE 6]><html class="no-js ie6 ie67 ie678"{% if lang is not empty %} lang="{{ lang }}"{% endif %}><![endif]-->
<!--[if IE 8]><html class="no-js ie8 ie678" lang="en"><![endif]--> <!--[if lte IE 7]><html class="no-js ie7 ie67 ie678"{% if lang is not empty %} lang="{{ lang }}"{% endif %}><![endif]-->
<!--[if gt IE 8]><html class="no-js" lang="en"><![endif]--> <!--[if IE 8]><html class="no-js ie8 ie678"{% if lang is not empty %} lang="{{ lang }}"{% endif %}><![endif]-->
<html> <!--[if gt IE 8]><html class="no-js"{% if lang is not empty %} lang="{{ lang }}"{% endif %}><![endif]-->
<html{% if lang is not empty %} lang="{{ lang }}"{% endif %}>
<head> <head>
{% block head %} {% block head %}
<meta name="viewport" content="initial-scale=1.0"> <meta name="viewport" content="initial-scale=1.0">

View file

@ -5,7 +5,7 @@
{% block content %} {% block content %}
<div id="article"> <div id="article">
<header class="mbm"> <header class="mbm">
<h1>{{ entry.title|e|default('entry.default_title'|trans)|raw }} <a href="{{ path('edit', { 'id': entry.id }) }}" class="nostyle" title="{{ 'entry.view.edit_title'|trans }}">✎</a></h1> <h1><span{% if entry.language is defined and entry.language is not null %} lang="{{ entry.getHTMLLanguage() }}"{% endif %}>{{ entry.title|e|default('entry.default_title'|trans)|raw }}</span> <a href="{{ path('edit', { 'id': entry.id }) }}" class="nostyle" title="{{ 'entry.view.edit_title'|trans }}">✎</a></h1>
</header> </header>
<div id="article_toolbar"> <div id="article_toolbar">
@ -96,7 +96,7 @@
</div> </div>
</aside> </aside>
</div> </div>
<article> <article{% if entry.language is defined and entry.language is not null %} lang="{{ entry.getHTMLLanguage() }}"{% endif %}>
{{ entry.content | raw }} {{ entry.content | raw }}
</article> </article>
</div> </div>

View file

@ -223,7 +223,7 @@
{% block content %} {% block content %}
<div id="article"> <div id="article">
<header class="mbm"> <header class="mbm">
<h1>{{ entry.title|striptags|default('entry.default_title'|trans)|raw }} <a href="{{ path('edit', { 'id': entry.id }) }}" title="{{ 'entry.view.edit_title'|trans }}">✎</a></h1> <h1><span{% if entry.language is defined and entry.language is not null %} lang="{{ entry.getHTMLLanguage() }}"{% endif %}>{{ entry.title|striptags|default('entry.default_title'|trans)|raw }}</span> <a href="{{ path('edit', { 'id': entry.id }) }}" title="{{ 'entry.view.edit_title'|trans }}">✎</a></h1>
</header> </header>
<aside> <aside>
<div class="tools"> <div class="tools">
@ -276,7 +276,7 @@
</div> </div>
</aside> </aside>
<article> <article{% if entry.language is defined and entry.language is not null %} lang="{{ entry.getHTMLLanguage() }}"{% endif %}>
{{ entry.content | raw }} {{ entry.content | raw }}
</article> </article>

View file

@ -0,0 +1,28 @@
<?php
namespace Tests\Wallabag\CoreBundle\Entity;
use Tests\Wallabag\CoreBundle\WallabagCoreTestCase;
use Wallabag\CoreBundle\Entity\Entry;
class EntryTest extends WallabagCoreTestCase
{
public function testGetLanguage()
{
$this->logInAs('admin');
$entry = new Entry($this->getLoggedInUser());
$languages = [
'en_GB' => 'en-GB',
'en_US' => 'en-US',
'en-gb' => 'en-GB',
'en-US' => 'en-US',
'fr' => 'fr',
'fr_FR' => 'fr-FR',
'ja' => 'ja',
];
foreach ($languages as $entryLang => $lang) {
$entry->setLanguage($entryLang);
$this->assertSame($lang, $entry->getHTMLLanguage());
}
}
}