refactor: deduplicate OS-specific hotkey detection into a shared helper

This commit is contained in:
Ribas160 2026-05-29 15:43:41 +03:00
parent 5d946c52a7
commit ec2d230d1b
No known key found for this signature in database
GPG key ID: ED4AE99DCA25A6BB
5 changed files with 31 additions and 6 deletions

View file

@ -297,6 +297,19 @@ class I18n
return in_array(self::$_language, array('ar', 'he'));
}
/**
* get OS-specific copy hotkey modifier key name based on user agent
*
* @access public
* @static
* @return string 'Cmd' on macOS, 'Ctrl' otherwise
*/
public static function getCopyHotkey()
{
return isset($_SERVER['HTTP_USER_AGENT']) &&
strpos($_SERVER['HTTP_USER_AGENT'], 'Mac') !== false ? 'Cmd' : 'Ctrl';
}
/**
* set the default language
*

View file

@ -626,8 +626,7 @@ endif;
<div class="media-body media-middle">
<small id="copyShortcutHintText" class="hidden-xs">
<?php
$hotkey = isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], 'Mac') !== false ? 'Cmd' : 'Ctrl';
echo I18n::_("To copy document press on the copy button or use the clipboard shortcut <kbd>%s</kbd>+<kbd>c</kbd>", $hotkey)
echo I18n::_("To copy document press on the copy button or use the clipboard shortcut <kbd>%s</kbd>+<kbd>c</kbd>", I18n::getCopyHotkey())
?>
</small>
</div>

View file

@ -483,8 +483,7 @@ endif;
<h6 id="copyShortcutHint" class="col-md-12 nav justify-content-between align-items-center mb-2 hidden">
<small id="copyShortcutHintText" class="d-none d-md-inline">
<?php
$hotkey = isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], 'Mac') !== false ? 'Cmd' : 'Ctrl';
echo I18n::_("To copy document press on the copy button or use the clipboard shortcut <kbd>%s</kbd>+<kbd>c</kbd>", $hotkey)
echo I18n::_("To copy document press on the copy button or use the clipboard shortcut <kbd>%s</kbd>+<kbd>c</kbd>", I18n::getCopyHotkey())
?>
</small>
<button type="button" id="copyShortcutHintBtn" class="btn btn-secondary ms-auto"><?php echo I18n::_('Copy'); ?></button>

View file

@ -14,8 +14,7 @@ if (empty($ERROR)) :
?>
<p>
<?php
$hotkey = isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], 'Mac') !== false ? 'Cmd' : 'Ctrl';
echo I18n::_('Your document is <a id="pasteurl" href="%s">%s</a> <span id="copyhint">(Hit <kbd>%s</kbd>+<kbd>c</kbd> to copy)</span>', $SHORTURL, $SHORTURL, $hotkey);
echo I18n::_('Your document is <a id="pasteurl" href="%s">%s</a> <span id="copyhint">(Hit <kbd>%s</kbd>+<kbd>c</kbd> to copy)</span>', $SHORTURL, $SHORTURL, I18n::getCopyHotkey());
?>
</p>
<?php

View file

@ -231,6 +231,21 @@ class I18nTest extends TestCase
Helper::rmDir($path);
}
public function testGetCopyHotkey()
{
$_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)';
$this->assertEquals('Cmd', I18n::getCopyHotkey(), 'returns Cmd on macOS');
$_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)';
$this->assertEquals('Ctrl', I18n::getCopyHotkey(), 'returns Ctrl on Windows');
$_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (X11; Linux x86_64)';
$this->assertEquals('Ctrl', I18n::getCopyHotkey(), 'returns Ctrl on Linux');
unset($_SERVER['HTTP_USER_AGENT']);
$this->assertEquals('Ctrl', I18n::getCopyHotkey(), 'returns Ctrl when user agent absent');
}
public function testMessageIdsExistInAllLanguages()
{
$messageIds = array();