Skip to content

Commit

Permalink
Merge pull request #4257 from Simounet/feat/lang-attribute
Browse files Browse the repository at this point in the history
Use lang attribute
  • Loading branch information
j0k3r committed Jan 28, 2020
2 parents 97c6561 + 416d44d commit 8afcb3a
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 9 deletions.
13 changes: 13 additions & 0 deletions src/Wallabag/CoreBundle/Entity/Entry.php
Expand Up @@ -787,6 +787,19 @@ public function getLanguage()
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
*/
Expand Down
11 changes: 6 additions & 5 deletions src/Wallabag/CoreBundle/Resources/views/base.html.twig
@@ -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">
Expand Down
Expand Up @@ -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">
Expand Down Expand Up @@ -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>
Expand Down
Expand Up @@ -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">
Expand Down Expand Up @@ -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>

Expand Down
28 changes: 28 additions & 0 deletions tests/Wallabag/CoreBundle/Entity/EntryTest.php
@@ -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());
}
}
}

0 comments on commit 8afcb3a

Please sign in to comment.