Skip to content

Commit

Permalink
Replace languages.inc globals with I18n\Languages consts (#1121)
Browse files Browse the repository at this point in the history
The include/languages.inc file was not removed as it's used in other
repositories. It should be removed after migration.

Tests were added to ensure that the global variables and the constants
are in sync with each other.

Signed-off-by: Maurício Meneghini Fauth <mauricio@mfauth.net>
  • Loading branch information
MauricioFauth authored Nov 14, 2024
1 parent 817a3e7 commit cdf5907
Show file tree
Hide file tree
Showing 15 changed files with 190 additions and 58 deletions.
13 changes: 8 additions & 5 deletions docs.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php

use phpweb\I18n\Languages;

$_SERVER['BASE_PAGE'] = 'docs.php';
include_once __DIR__ . '/include/prepend.inc';

Expand Down Expand Up @@ -27,19 +30,19 @@
<?php

// List all manual languages viewable online
$lastlang = end($ACTIVE_ONLINE_LANGUAGES);
foreach ($ACTIVE_ONLINE_LANGUAGES as $langcode => $langname) {
$lastlang = array_key_last(Languages::ACTIVE_ONLINE_LANGUAGES);
foreach (Languages::ACTIVE_ONLINE_LANGUAGES as $langcode => $langname) {
if (!file_exists($_SERVER["DOCUMENT_ROOT"] . "/manual/{$langcode}/index.php")) {
continue;
}

// Make preferred language bold
if ($langcode == $LANG) { echo "<strong>"; }
if ($langcode === $LANG) { echo "<strong>"; }

echo '<a href="/manual/' . $langcode . '/">' . $langname . '</a>';
echo ($lastlang != $langname) ? ",\n" : "\n";
echo ($lastlang !== $langcode) ? ",\n" : "\n";

if ($langcode == $LANG) { echo "</strong>"; }
if ($langcode === $LANG) { echo "</strong>"; }
}

?>
Expand Down
11 changes: 7 additions & 4 deletions download-docs.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
<?php

use phpweb\I18n\Languages;

$_SERVER['BASE_PAGE'] = 'download-docs.php';
include_once __DIR__ . '/include/prepend.inc';

if (!empty($_GET['active_langs'])) {
echo serialize($ACTIVE_ONLINE_LANGUAGES);
echo serialize(Languages::ACTIVE_ONLINE_LANGUAGES);
exit;
}

Expand Down Expand Up @@ -90,8 +93,8 @@
$filepath = $filename = '';

// Go through all possible manual languages
foreach ($LANGUAGES as $langcode => $language) {
if (isset($INACTIVE_ONLINE_LANGUAGES[$langcode]) && $MYSITE !== 'http://docs.php.net/') {
foreach (Languages::LANGUAGES as $langcode => $language) {
if (isset(Languages::INACTIVE_ONLINE_LANGUAGES[$langcode]) && $MYSITE !== 'http://docs.php.net/') {
continue;
}

Expand Down Expand Up @@ -180,7 +183,7 @@
$cellclass = "";
}

echo "<tr>\n<th class=\"subl\">" . $LANGUAGES[$langcode] . "</th>\n";
echo "<tr>\n<th class=\"subl\">" . Languages::LANGUAGES[$langcode] . "</th>\n";

foreach ($formats as $formatname => $extension) {

Expand Down
8 changes: 4 additions & 4 deletions error.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
*/

use phpweb\I18n\Languages;
use phpweb\UserPreferences;

// Ensure that our environment is set up
include_once __DIR__ . '/include/prepend.inc';
include_once __DIR__ . '/include/languages.inc';
include_once __DIR__ . '/include/errors.inc';

// Get URI for this request, strip leading slash
Expand Down Expand Up @@ -79,7 +79,7 @@
// default language manual accessibility on mirror sites through /manual/filename)
// @todo do we rely on this? how about removing it...
if (preg_match("!^manual/([^/]*)$!", $URI, $array)) {
if (!isset($INACTIVE_ONLINE_LANGUAGES[$array[1]])) {
if (!isset(Languages::INACTIVE_ONLINE_LANGUAGES[$array[1]])) {
mirror_redirect("/manual/$LANG/$array[1]");
}
} elseif (preg_match("!^manual/html/([^/]+)$!", $URI, $array)) {
Expand Down Expand Up @@ -705,10 +705,10 @@
// ============================================================================
// For manual pages for inactive languages, point visitors to the English page
if (preg_match("!^manual/([^/]+)/([^/]+).php$!", $URI, $match) &&
isset($INACTIVE_ONLINE_LANGUAGES[$match[1]])) {
isset(Languages::INACTIVE_ONLINE_LANGUAGES[$match[1]])) {
$try = find_manual_page("en", $match[2]);
if ($try) {
error_inactive_manual_page($INACTIVE_ONLINE_LANGUAGES[$match[1]], $try);
error_inactive_manual_page(Languages::INACTIVE_ONLINE_LANGUAGES[$match[1]], $try);
}
}

Expand Down
6 changes: 4 additions & 2 deletions include/errors.inc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
not available.
*/

use phpweb\I18n\Languages;

// A 'good looking' 404 error message page
function error_404(): void
{
Expand Down Expand Up @@ -37,7 +39,7 @@ function error_404_manual(): void
// An error message page for manual pages from inactive languages
function error_inactive_manual_page($lang_name, $en_page): void
{
global $MYSITE, $ACTIVE_ONLINE_LANGUAGES;
global $MYSITE;
status_header(404);
site_header('Page gone', ["noindex"]);
echo "<h1>Page gone</h1>\n" .
Expand All @@ -48,7 +50,7 @@ function error_inactive_manual_page($lang_name, $en_page): void
echo "<p>The English page is available at <a href=\"{$en_url}\">{$en_url}</a></p>\n";
echo "<p>Several other languages are also available:</p>\n";
echo "<ul>\n";
foreach ($ACTIVE_ONLINE_LANGUAGES as $alt_lang => $alt_lang_name) {
foreach (Languages::ACTIVE_ONLINE_LANGUAGES as $alt_lang => $alt_lang_name) {
if ($alt_lang === "en") {
continue;
}
Expand Down
3 changes: 2 additions & 1 deletion include/langchooser.inc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
*/

use phpweb\I18n\Languages;
use phpweb\LangChooser;

require_once __DIR__ . '/../src/autoload.php';
Expand All @@ -37,7 +38,7 @@ $_SERVER['STRIPPED_URI'] = htmlspecialchars($_SERVER['REQUEST_URI'], ENT_QUOTES,

// The code is encapsulated in a function,
// so the variable namespace is not polluted
list($LANG, $EXPL_LANG) = (new LangChooser($LANGUAGES, $INACTIVE_ONLINE_LANGUAGES, $userPreferences->languageCode, default_language() ?: ''))->chooseCode(
list($LANG, $EXPL_LANG) = (new LangChooser(Languages::LANGUAGES, Languages::INACTIVE_ONLINE_LANGUAGES, $userPreferences->languageCode, default_language() ?: ''))->chooseCode(
$_REQUEST['lang'] ?? null,
$_SERVER['REQUEST_URI'],
$_SERVER['HTTP_ACCEPT_LANGUAGE'] ?? null,
Expand Down
31 changes: 4 additions & 27 deletions include/languages.inc
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php
/**
* This file is deprecated, and it exists for backward compatibility purposes only.
* It must be kept in sync with {@see \phpweb\I18n\Languages}.
*/

/*
This is a list of all manual languages hosted
within PHP Git repositories (https://github.com/php/doc-{lang})
*/
$LANGUAGES = [
'en' => 'English',
'de' => 'German',
Expand All @@ -20,32 +20,9 @@ $LANGUAGES = [
'zh' => 'Chinese (Simplified)',
];

/*
The following languages are inactive, which means they will not:
- Show up via the language select box at php.net
- Be selectable via my.php
- Accept redirections to the translation, despite ACCEPT_LANGUAGE
- Be listed at php.net/docs or php.net/download-docs
However, translation status for these languages is available at:
- https://doc.php.net/
*/
$INACTIVE_ONLINE_LANGUAGES = [
'pl' => 'Polish',
'ro' => 'Romanian',
];

$ACTIVE_ONLINE_LANGUAGES = array_diff($LANGUAGES, $INACTIVE_ONLINE_LANGUAGES);

// Convert between language codes back and forth
// [We use non-standard languages codes and so conversion
// is needed when communicating with the outside world]
function language_convert(string $langcode): string
{
global $LANGUAGES;
switch ($langcode) {
case 'zh_cn': return 'zh';
default:
// Fallback on english if we got something wacky
return array_key_exists($langcode, $LANGUAGES) ? $langcode : 'en';
}
}
3 changes: 2 additions & 1 deletion include/layout.inc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use phpweb\I18n\Languages;
use phpweb\Navigation\NavItem;

$_SERVER['STATIC_ROOT'] = $MYSITE;
Expand Down Expand Up @@ -463,7 +464,7 @@ META

$config["headsup"] = get_news_changes();

$lang = language_convert($config["lang"]);
$lang = (new Languages())->convert($config["lang"]);
$curr = $config["current"];
$classes = $config['classes'];

Expand Down
10 changes: 5 additions & 5 deletions include/shared-manual.inc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ $PGI = []; $SIDEBAR_DATA = '';
// User note display functions
// =============================================================================

use phpweb\I18n\Languages;
use phpweb\UserNotes\Sorter;
use phpweb\UserNotes\UserNote;

Expand Down Expand Up @@ -271,7 +272,6 @@ function manual_navigation_methodname($methodname) {
// including HTTP header information
function manual_setup($setup): void {
global $PGI, $MYSITE, $USERNOTES;
global $ACTIVE_ONLINE_LANGUAGES;

//TODO: get rid of this hack to get the related items into manual_footer
global $__RELATED;
Expand All @@ -281,7 +281,7 @@ function manual_setup($setup): void {
}
$PGI = $setup;
// Set base href for this manual page
$base = 'manual/' . language_convert($setup['head'][1]) . "/";
$base = 'manual/' . (new Languages())->convert($setup['head'][1]) . "/";
$_SERVER['BASE_PAGE'] = $base . $setup['this'][0];
$_SERVER['BASE_HREF'] = $MYSITE . $_SERVER['BASE_PAGE'];

Expand Down Expand Up @@ -313,7 +313,7 @@ function manual_setup($setup): void {
$config = [
"current" => "docs",
"breadcrumbs" => $breadcrumbs,
"languages" => array_keys($ACTIVE_ONLINE_LANGUAGES),
"languages" => array_keys(Languages::ACTIVE_ONLINE_LANGUAGES),
"meta-navigation" => [
"contents" => $base . $setup["home"][0],
"index" => $base . $setup["up"][0],
Expand All @@ -340,12 +340,12 @@ PAGE_TOOLS;
}

function manual_language_chooser($currentlang, $currentpage) {
global $ACTIVE_ONLINE_LANGUAGES, $LANG;
global $LANG;

// Prepare the form with all the options
$othersel = ' selected="selected"';
$out = [];
foreach ($ACTIVE_ONLINE_LANGUAGES as $lang => $text) {
foreach (Languages::ACTIVE_ONLINE_LANGUAGES as $lang => $text) {
$selected = '';
if ($lang == $currentlang) {
$selected = ' selected="selected"';
Expand Down
9 changes: 4 additions & 5 deletions include/site.inc
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use phpweb\I18n\Languages;

// Define some constants, and $MIRRORS array
// Mirror type constants
const MIRROR_DOWNLOAD = 0;
Expand Down Expand Up @@ -31,9 +33,6 @@ $MIRRORS = [
*/
$COUNTRIES = include __DIR__ . '/countries.inc';

// Define $LANGUAGES array
include __DIR__ . '/languages.inc';

// Returns true if the current (or specified)
// site is the primary mirror site
function is_primary_site($site = false)
Expand Down Expand Up @@ -261,14 +260,14 @@ if (!isset($MIRRORS[$MYSITE])) {

// Override mirror language with local preference
if (isset($_SERVER['MIRROR_LANGUAGE'])) {
if (isset($LANGUAGES[$_SERVER['MIRROR_LANGUAGE']])) {
if (isset(Languages::LANGUAGES[$_SERVER['MIRROR_LANGUAGE']])) {
$MIRRORS[$MYSITE][6] = $_SERVER['MIRROR_LANGUAGE'];
}
}

// Fallback to English in case the language
// set is definitely not supported
if (!isset($LANGUAGES[$MIRRORS[$MYSITE][6]])) {
if (!isset(Languages::LANGUAGES[$MIRRORS[$MYSITE][6]])) {
$MIRRORS[$MYSITE][6] = "en";
}

Expand Down
4 changes: 3 additions & 1 deletion js/search-index.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use phpweb\I18n\Languages;

$_GET["lang"] = "en";
if (!isset($_GET["lang"])) {
header("Location: http://php.net");
Expand All @@ -9,7 +11,7 @@
$_SERVER["DOCUMENT_ROOT"] = __DIR__ . "/../";
}
include __DIR__ . '/../include/prepend.inc';
if (!isset($ACTIVE_ONLINE_LANGUAGES[$_GET["lang"]])) {
if (!isset(Languages::ACTIVE_ONLINE_LANGUAGES[$_GET["lang"]])) {
header("Location: http://php.net");
}
$lang = $_GET["lang"];
Expand Down
5 changes: 4 additions & 1 deletion manual/help-translate.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php

use phpweb\I18n\Languages;

$_SERVER['BASE_PAGE'] = 'manual/help-translate.php';
include_once __DIR__ . '/../include/prepend.inc';
include_once __DIR__ . '/../include/shared-manual.inc';
Expand Down Expand Up @@ -26,7 +29,7 @@
// $archived are manuals we have old versions of
$archived = ['da', 'kr', 'pl', 'tw'];

foreach ($INACTIVE_ONLINE_LANGUAGES as $cc => $lang) {
foreach (Languages::INACTIVE_ONLINE_LANGUAGES as $cc => $lang) {
$link = 'no archive';
if (in_array($cc, $archived, true)) {
$link = '<a href="http://docs.php.net/manual/' . $cc . '">archive</a>';
Expand Down
5 changes: 4 additions & 1 deletion mirror.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php

use phpweb\I18n\Languages;

$_SERVER['BASE_PAGE'] = 'mirror.php';
include_once __DIR__ . '/include/prepend.inc';
$SIDEBAR_DATA = '
Expand Down Expand Up @@ -84,7 +87,7 @@
<h2>Mirror Services</h2>

<ul>
<li>Default language is <?php echo $LANGUAGES[default_language()]; ?></li>
<li>Default language is <?php echo Languages::LANGUAGES[default_language()]; ?></li>
</ul>

<h2>Mirror Status</h2>
Expand Down
3 changes: 2 additions & 1 deletion my.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use phpweb\I18n\Languages;
use phpweb\UserPreferences;

$_SERVER['BASE_PAGE'] = 'my.php';
Expand All @@ -9,7 +10,7 @@
header_nocache();

// Languages array copy and options to list
$langs = $ACTIVE_ONLINE_LANGUAGES;
$langs = Languages::ACTIVE_ONLINE_LANGUAGES;
$options = [];

// We have post data, and it is an available language
Expand Down
Loading

0 comments on commit cdf5907

Please sign in to comment.