Skip to content

Commit cdf5907

Browse files
Replace languages.inc globals with I18n\Languages consts (#1121)
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 <[email protected]>
1 parent 817a3e7 commit cdf5907

File tree

15 files changed

+190
-58
lines changed

15 files changed

+190
-58
lines changed

docs.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
<?php
2+
3+
use phpweb\I18n\Languages;
4+
25
$_SERVER['BASE_PAGE'] = 'docs.php';
36
include_once __DIR__ . '/include/prepend.inc';
47

@@ -27,19 +30,19 @@
2730
<?php
2831

2932
// List all manual languages viewable online
30-
$lastlang = end($ACTIVE_ONLINE_LANGUAGES);
31-
foreach ($ACTIVE_ONLINE_LANGUAGES as $langcode => $langname) {
33+
$lastlang = array_key_last(Languages::ACTIVE_ONLINE_LANGUAGES);
34+
foreach (Languages::ACTIVE_ONLINE_LANGUAGES as $langcode => $langname) {
3235
if (!file_exists($_SERVER["DOCUMENT_ROOT"] . "/manual/{$langcode}/index.php")) {
3336
continue;
3437
}
3538

3639
// Make preferred language bold
37-
if ($langcode == $LANG) { echo "<strong>"; }
40+
if ($langcode === $LANG) { echo "<strong>"; }
3841

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

42-
if ($langcode == $LANG) { echo "</strong>"; }
45+
if ($langcode === $LANG) { echo "</strong>"; }
4346
}
4447

4548
?>

download-docs.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
<?php
2+
3+
use phpweb\I18n\Languages;
4+
25
$_SERVER['BASE_PAGE'] = 'download-docs.php';
36
include_once __DIR__ . '/include/prepend.inc';
47

58
if (!empty($_GET['active_langs'])) {
6-
echo serialize($ACTIVE_ONLINE_LANGUAGES);
9+
echo serialize(Languages::ACTIVE_ONLINE_LANGUAGES);
710
exit;
811
}
912

@@ -90,8 +93,8 @@
9093
$filepath = $filename = '';
9194

9295
// Go through all possible manual languages
93-
foreach ($LANGUAGES as $langcode => $language) {
94-
if (isset($INACTIVE_ONLINE_LANGUAGES[$langcode]) && $MYSITE !== 'http://docs.php.net/') {
96+
foreach (Languages::LANGUAGES as $langcode => $language) {
97+
if (isset(Languages::INACTIVE_ONLINE_LANGUAGES[$langcode]) && $MYSITE !== 'http://docs.php.net/') {
9598
continue;
9699
}
97100

@@ -180,7 +183,7 @@
180183
$cellclass = "";
181184
}
182185

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

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

error.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
1010
*/
1111

12+
use phpweb\I18n\Languages;
1213
use phpweb\UserPreferences;
1314

1415
// Ensure that our environment is set up
1516
include_once __DIR__ . '/include/prepend.inc';
16-
include_once __DIR__ . '/include/languages.inc';
1717
include_once __DIR__ . '/include/errors.inc';
1818

1919
// Get URI for this request, strip leading slash
@@ -79,7 +79,7 @@
7979
// default language manual accessibility on mirror sites through /manual/filename)
8080
// @todo do we rely on this? how about removing it...
8181
if (preg_match("!^manual/([^/]*)$!", $URI, $array)) {
82-
if (!isset($INACTIVE_ONLINE_LANGUAGES[$array[1]])) {
82+
if (!isset(Languages::INACTIVE_ONLINE_LANGUAGES[$array[1]])) {
8383
mirror_redirect("/manual/$LANG/$array[1]");
8484
}
8585
} elseif (preg_match("!^manual/html/([^/]+)$!", $URI, $array)) {
@@ -705,10 +705,10 @@
705705
// ============================================================================
706706
// For manual pages for inactive languages, point visitors to the English page
707707
if (preg_match("!^manual/([^/]+)/([^/]+).php$!", $URI, $match) &&
708-
isset($INACTIVE_ONLINE_LANGUAGES[$match[1]])) {
708+
isset(Languages::INACTIVE_ONLINE_LANGUAGES[$match[1]])) {
709709
$try = find_manual_page("en", $match[2]);
710710
if ($try) {
711-
error_inactive_manual_page($INACTIVE_ONLINE_LANGUAGES[$match[1]], $try);
711+
error_inactive_manual_page(Languages::INACTIVE_ONLINE_LANGUAGES[$match[1]], $try);
712712
}
713713
}
714714

include/errors.inc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
not available.
66
*/
77

8+
use phpweb\I18n\Languages;
9+
810
// A 'good looking' 404 error message page
911
function error_404(): void
1012
{
@@ -37,7 +39,7 @@ function error_404_manual(): void
3739
// An error message page for manual pages from inactive languages
3840
function error_inactive_manual_page($lang_name, $en_page): void
3941
{
40-
global $MYSITE, $ACTIVE_ONLINE_LANGUAGES;
42+
global $MYSITE;
4143
status_header(404);
4244
site_header('Page gone', ["noindex"]);
4345
echo "<h1>Page gone</h1>\n" .
@@ -48,7 +50,7 @@ function error_inactive_manual_page($lang_name, $en_page): void
4850
echo "<p>The English page is available at <a href=\"{$en_url}\">{$en_url}</a></p>\n";
4951
echo "<p>Several other languages are also available:</p>\n";
5052
echo "<ul>\n";
51-
foreach ($ACTIVE_ONLINE_LANGUAGES as $alt_lang => $alt_lang_name) {
53+
foreach (Languages::ACTIVE_ONLINE_LANGUAGES as $alt_lang => $alt_lang_name) {
5254
if ($alt_lang === "en") {
5355
continue;
5456
}

include/langchooser.inc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
2929
*/
3030

31+
use phpweb\I18n\Languages;
3132
use phpweb\LangChooser;
3233

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

3839
// The code is encapsulated in a function,
3940
// so the variable namespace is not polluted
40-
list($LANG, $EXPL_LANG) = (new LangChooser($LANGUAGES, $INACTIVE_ONLINE_LANGUAGES, $userPreferences->languageCode, default_language() ?: ''))->chooseCode(
41+
list($LANG, $EXPL_LANG) = (new LangChooser(Languages::LANGUAGES, Languages::INACTIVE_ONLINE_LANGUAGES, $userPreferences->languageCode, default_language() ?: ''))->chooseCode(
4142
$_REQUEST['lang'] ?? null,
4243
$_SERVER['REQUEST_URI'],
4344
$_SERVER['HTTP_ACCEPT_LANGUAGE'] ?? null,

include/languages.inc

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
2+
/**
3+
* This file is deprecated, and it exists for backward compatibility purposes only.
4+
* It must be kept in sync with {@see \phpweb\I18n\Languages}.
5+
*/
26

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

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

3728
$ACTIVE_ONLINE_LANGUAGES = array_diff($LANGUAGES, $INACTIVE_ONLINE_LANGUAGES);
38-
39-
// Convert between language codes back and forth
40-
// [We use non-standard languages codes and so conversion
41-
// is needed when communicating with the outside world]
42-
function language_convert(string $langcode): string
43-
{
44-
global $LANGUAGES;
45-
switch ($langcode) {
46-
case 'zh_cn': return 'zh';
47-
default:
48-
// Fallback on english if we got something wacky
49-
return array_key_exists($langcode, $LANGUAGES) ? $langcode : 'en';
50-
}
51-
}

include/layout.inc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use phpweb\I18n\Languages;
34
use phpweb\Navigation\NavItem;
45

56
$_SERVER['STATIC_ROOT'] = $MYSITE;
@@ -463,7 +464,7 @@ META
463464

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

466-
$lang = language_convert($config["lang"]);
467+
$lang = (new Languages())->convert($config["lang"]);
467468
$curr = $config["current"];
468469
$classes = $config['classes'];
469470

include/shared-manual.inc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ $PGI = []; $SIDEBAR_DATA = '';
2222
// User note display functions
2323
// =============================================================================
2424

25+
use phpweb\I18n\Languages;
2526
use phpweb\UserNotes\Sorter;
2627
use phpweb\UserNotes\UserNote;
2728

@@ -271,7 +272,6 @@ function manual_navigation_methodname($methodname) {
271272
// including HTTP header information
272273
function manual_setup($setup): void {
273274
global $PGI, $MYSITE, $USERNOTES;
274-
global $ACTIVE_ONLINE_LANGUAGES;
275275

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

@@ -313,7 +313,7 @@ function manual_setup($setup): void {
313313
$config = [
314314
"current" => "docs",
315315
"breadcrumbs" => $breadcrumbs,
316-
"languages" => array_keys($ACTIVE_ONLINE_LANGUAGES),
316+
"languages" => array_keys(Languages::ACTIVE_ONLINE_LANGUAGES),
317317
"meta-navigation" => [
318318
"contents" => $base . $setup["home"][0],
319319
"index" => $base . $setup["up"][0],
@@ -340,12 +340,12 @@ PAGE_TOOLS;
340340
}
341341

342342
function manual_language_chooser($currentlang, $currentpage) {
343-
global $ACTIVE_ONLINE_LANGUAGES, $LANG;
343+
global $LANG;
344344

345345
// Prepare the form with all the options
346346
$othersel = ' selected="selected"';
347347
$out = [];
348-
foreach ($ACTIVE_ONLINE_LANGUAGES as $lang => $text) {
348+
foreach (Languages::ACTIVE_ONLINE_LANGUAGES as $lang => $text) {
349349
$selected = '';
350350
if ($lang == $currentlang) {
351351
$selected = ' selected="selected"';

include/site.inc

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
use phpweb\I18n\Languages;
4+
35
// Define some constants, and $MIRRORS array
46
// Mirror type constants
57
const MIRROR_DOWNLOAD = 0;
@@ -31,9 +33,6 @@ $MIRRORS = [
3133
*/
3234
$COUNTRIES = include __DIR__ . '/countries.inc';
3335

34-
// Define $LANGUAGES array
35-
include __DIR__ . '/languages.inc';
36-
3736
// Returns true if the current (or specified)
3837
// site is the primary mirror site
3938
function is_primary_site($site = false)
@@ -261,14 +260,14 @@ if (!isset($MIRRORS[$MYSITE])) {
261260

262261
// Override mirror language with local preference
263262
if (isset($_SERVER['MIRROR_LANGUAGE'])) {
264-
if (isset($LANGUAGES[$_SERVER['MIRROR_LANGUAGE']])) {
263+
if (isset(Languages::LANGUAGES[$_SERVER['MIRROR_LANGUAGE']])) {
265264
$MIRRORS[$MYSITE][6] = $_SERVER['MIRROR_LANGUAGE'];
266265
}
267266
}
268267

269268
// Fallback to English in case the language
270269
// set is definitely not supported
271-
if (!isset($LANGUAGES[$MIRRORS[$MYSITE][6]])) {
270+
if (!isset(Languages::LANGUAGES[$MIRRORS[$MYSITE][6]])) {
272271
$MIRRORS[$MYSITE][6] = "en";
273272
}
274273

js/search-index.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
use phpweb\I18n\Languages;
4+
35
$_GET["lang"] = "en";
46
if (!isset($_GET["lang"])) {
57
header("Location: http://php.net");
@@ -9,7 +11,7 @@
911
$_SERVER["DOCUMENT_ROOT"] = __DIR__ . "/../";
1012
}
1113
include __DIR__ . '/../include/prepend.inc';
12-
if (!isset($ACTIVE_ONLINE_LANGUAGES[$_GET["lang"]])) {
14+
if (!isset(Languages::ACTIVE_ONLINE_LANGUAGES[$_GET["lang"]])) {
1315
header("Location: http://php.net");
1416
}
1517
$lang = $_GET["lang"];

manual/help-translate.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
<?php
2+
3+
use phpweb\I18n\Languages;
4+
25
$_SERVER['BASE_PAGE'] = 'manual/help-translate.php';
36
include_once __DIR__ . '/../include/prepend.inc';
47
include_once __DIR__ . '/../include/shared-manual.inc';
@@ -26,7 +29,7 @@
2629
// $archived are manuals we have old versions of
2730
$archived = ['da', 'kr', 'pl', 'tw'];
2831

29-
foreach ($INACTIVE_ONLINE_LANGUAGES as $cc => $lang) {
32+
foreach (Languages::INACTIVE_ONLINE_LANGUAGES as $cc => $lang) {
3033
$link = 'no archive';
3134
if (in_array($cc, $archived, true)) {
3235
$link = '<a href="http://docs.php.net/manual/' . $cc . '">archive</a>';

mirror.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
<?php
2+
3+
use phpweb\I18n\Languages;
4+
25
$_SERVER['BASE_PAGE'] = 'mirror.php';
36
include_once __DIR__ . '/include/prepend.inc';
47
$SIDEBAR_DATA = '
@@ -84,7 +87,7 @@
8487
<h2>Mirror Services</h2>
8588

8689
<ul>
87-
<li>Default language is <?php echo $LANGUAGES[default_language()]; ?></li>
90+
<li>Default language is <?php echo Languages::LANGUAGES[default_language()]; ?></li>
8891
</ul>
8992

9093
<h2>Mirror Status</h2>

my.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use phpweb\I18n\Languages;
34
use phpweb\UserPreferences;
45

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

1112
// Languages array copy and options to list
12-
$langs = $ACTIVE_ONLINE_LANGUAGES;
13+
$langs = Languages::ACTIVE_ONLINE_LANGUAGES;
1314
$options = [];
1415

1516
// We have post data, and it is an available language

0 commit comments

Comments
 (0)