Merge pull request #4257 from Simounet/feat/lang-attribute

Use lang attribute
This commit is contained in:
Jérémy Benoist 2020-01-28 10:06:30 +01:00 committed by GitHub
commit 8afcb3a647
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 51 additions and 9 deletions

View file

@ -787,6 +787,19 @@ class Entry
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
*/

View file

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

View file

@ -5,7 +5,7 @@
{% block content %}
<div id="article">
<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>
<div id="article_toolbar">
@ -96,7 +96,7 @@
</div>
</aside>
</div>
<article>
<article{% if entry.language is defined and entry.language is not null %} lang="{{ entry.getHTMLLanguage() }}"{% endif %}>
{{ entry.content | raw }}
</article>
</div>

View file

@ -223,7 +223,7 @@
{% block content %}
<div id="article">
<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>
<aside>
<div class="tools">
@ -276,7 +276,7 @@
</div>
</aside>
<article>
<article{% if entry.language is defined and entry.language is not null %} lang="{{ entry.getHTMLLanguage() }}"{% endif %}>
{{ entry.content | raw }}
</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());
}
}
}