Skip to content

Commit

Permalink
Simplify strings
Browse files Browse the repository at this point in the history
* Replace array of indexes with `_()` alias
  • Loading branch information
darcywong00 committed Jan 20, 2025
1 parent 18b5900 commit 19510be
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 69 deletions.
19 changes: 4 additions & 15 deletions _includes/locale/Locale.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,11 @@ public static function setTextDomain($basename) {
}

/**
* Returns an array of localized strings from the specified $domain-locale.po file
* Reads localized strings from the specified $domain-locale.po file
* for the current locale.
* @param $domain - base filename of the .po files (not including -xx-YY locale)
* @param $strings - Array of msgid's in the .po files
* @return Array of localized strings for the current locale
*/
public static function localize($domain, $strings) {
public static function localize($domain) {
foreach(Locale::CROWDIN_LOCALES as $l) {
if ($l == Locale::DEFAULT_LOCALE) {
// Skip English
Expand All @@ -86,16 +84,7 @@ public static function localize($domain, $strings) {
}

$previousTextDomain = textdomain(NULL);
Locale::setTextDomain($domain);

$result = [];
foreach($strings as $s) {
$result[$s] = _($s);
}

// Restore textdomain
textdomain($previousTextDomain);
return $result;
Locale::setTextDomain($domain);
}

/**
Expand All @@ -104,6 +93,6 @@ public static function localize($domain, $strings) {
* @param $args - optional remaining args to the format string
*/
public static function _s($s, ...$args) {
return vsprintf($s, $args);
return vsprintf(_($s), $args);
}
}
6 changes: 3 additions & 3 deletions _includes/locale/en/LC_MESSAGES/keyboards-en.po
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ msgstr "Search"
msgid "New search"
msgstr "New search"

# Search box prompt
msgid "Enter the name of a keyboard or language to search for%1$s"
msgstr "Enter the name of a keyboard or language to search for%1$s"
# Search box instruction (Popular keyboards | All keyboards)
msgid "Enter the name of a keyboard or language to search for"
msgstr "Enter the name of a keyboard or language to search for"

# Search box link for popular keyboards
msgid "Popular keyboards"
Expand Down
6 changes: 3 additions & 3 deletions _includes/locale/en/LC_MESSAGES/keyboards-es-ES.po
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ msgstr "Buscar"
msgid "New search"
msgstr "Nueva buscar"

# Search box prompt
msgid "Enter the name of a keyboard or language to search for%1$s"
msgstr "Introduzca el nombre de un teclado o idioma para buscar%1$s"
# Search box instruction (Popular keyboards | All keyboards)
msgid "Enter the name of a keyboard or language to search for"
msgstr "Introduzca el nombre de un teclado o idioma para buscar"

# Search box link for popular keyboards
msgid "Popular keyboards"
Expand Down
6 changes: 3 additions & 3 deletions _includes/locale/en/LC_MESSAGES/keyboards-fr-FR.po
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ msgstr "Recherche"
msgid "New search"
msgstr "Nouvelle recherche"

# Search box prompt
msgid "Enter the name of a keyboard or language to search for%1$s"
msgstr "Saisissez le nom d'un clavier ou d'une langue à rechercher%1$s"
# Search box instruction (Popular keyboards | All keyboards)
msgid "Enter the name of a keyboard or language to search for%"
msgstr "Saisissez le nom d'un clavier ou d'une langue à rechercher"

# Search box link for popular keyboards
msgid "Popular keyboards"
Expand Down
67 changes: 22 additions & 45 deletions keyboards/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,11 @@
use Keyman\Site\com\keyman\templates\Foot;
use Keyman\Site\com\keyman\Locale;

// Of array of strings at top of file
// by msgid
$keyboardIndexStrings = Locale::localize('keyboards', [
'Keyboard Search',
'Keyman Keyboard Search',
'Keyboard search:',
'Enter language or keyboard',
'Search',
'New search',
'Enter the name of a keyboard or language to search for%1$s',
'Popular keyboards',
'All keyboards',
'Hints',
'The search always returns a list of keyboards. It searches for keyboard names and details, language names, country names and script names.',
'You can apply prefixes',
'(keyboards)',
'(languages)',
'(scripts, writing systems) or',
'(countries) to filter your search results. For example',
'searches for keyboards for languages used in Thailand.',
'Use prefix',
'to search for a BCP 47 language tag, for example',
'searches for Tigrigna (Ethiopia)',
]);
Locale::localize('keyboards');

$head_options = [
'title' => $keyboardIndexStrings['Keyboard Search'],
'description' => $keyboardIndexStrings['Keyman Keyboard Search'],
'title' => _('Keyboard Search'),
'description' => _('Keyman Keyboard Search'),
'language' => Locale::currentLocale(),
'css' => [Util::cdn('css/template.css'), Util::cdn('keyboard-search/search.css')],
'js' => [Util::cdn('keyboard-search/jquery.mark.js'), Util::cdn('keyboard-search/dedicated-landing-pages.js'),
Expand Down Expand Up @@ -73,14 +50,14 @@

<div class='<?= $embed == 'none' ? '' : 'embed embed-'.$embed ?>'>

<h2 class="red underline"><a href='/keyboards'><?= $keyboardIndexStrings['Keyboard Search'] ?></a></h2>
<h2 class="red underline"><a href='/keyboards'><?= _('Keyboard Search') ?></a></h2>

<div id='search-box'>
<form method='get' action='/keyboards' name='f'>
<label for="search-q"><?= $keyboardIndexStrings['Keyboard search:'] ?></label><input id="search-q" type="text" placeholder="<?= $keyboardIndexStrings['Enter language or keyboard'] ?>" name="q"
<label for="search-q"><?= _('Keyboard search:') ?></label><input id="search-q" type="text" placeholder="<?= _('Enter language or keyboard') ?>" name="q"
<?php if($embed == 'none') echo 'autofocus'; ?>>
<input id="search-f" type="image" src="<?= cdn('img/search-button.png"') ?>" value="<?= $keyboardIndexStrings['Search'] ?>" onclick="return do_search()">
<label id="search-new"><a href='/keyboards<?=$session_query_q?>'><?= $keyboardIndexStrings['New search'] ?></a></label>
<input id="search-f" type="image" src="<?= cdn('img/search-button.png"') ?>" value="<?= _('Search') ?>" onclick="return do_search()">
<label id="search-new"><a href='/keyboards<?=$session_query_q?>'><?= _('New search') ?></a></label>
<input id="search-obsolete" type="hidden" name="obsolete" value="0">
<input id="search-page" type="hidden" name="page" value="1">
</form>
Expand All @@ -90,29 +67,29 @@
<div id='search-results'></div>
<div id='search-results-empty'>
<p>
<?= Locale::_s($keyboardIndexStrings['Enter the name of a keyboard or language to search for%1$s'], ". (") ?>
<a href="?q=p:popular"><?= $keyboardIndexStrings['Popular keyboards'] ?></a> |
<a href="?q=p:alphabetical"><?= $keyboardIndexStrings['All keyboards'] ?></a>)
<?= _('Enter the name of a keyboard or language to search for') ?> (
<a href="?q=p:popular"><?= _('Popular keyboards') ?></a> |
<a href="?q=p:alphabetical"><?= _('All keyboards') ?></a>)
</p>
<br />
<p><?= $keyboardIndexStrings['Hints'] ?></p>
<p><?= _('Hints') ?></p>
<ul>
<li>
<?= $keyboardIndexStrings['The search always returns a list of keyboards. ' .
'It searches for keyboard names and details, language names, country names and script names.'] ?>
<?= _('The search always returns a list of keyboards. ' .
'It searches for keyboard names and details, language names, country names and script names.') ?>
</li>
<li>
<?= $keyboardIndexStrings['You can apply prefixes'] ?>
<code>k:</code> <?= $keyboardIndexStrings['(keyboards)'] ?>
<code>l:</code> <?= $keyboardIndexStrings['(languages)'] ?>
<code>s:</code> <?= $keyboardIndexStrings['(scripts, writing systems) or'] ?>
<code>c:</code> <?= $keyboardIndexStrings['(countries) to filter your search results. For example'] ?>
<code>c:thailand</code> <?= $keyboardIndexStrings['searches for keyboards for languages used in Thailand.'] ?>
<?= _('You can apply prefixes') ?>
<code>k:</code> <?= _('(keyboards)') ?>
<code>l:</code> <?= _('(languages)') ?>
<code>s:</code> <?= _('(scripts, writing systems) or') ?>
<code>c:</code> <?= _('(countries) to filter your search results. For example') ?>
<code>c:thailand</code> <?= _('searches for keyboards for languages used in Thailand.') ?>
</li>
<li>
<?= $keyboardIndexStrings['Use prefix'] ?>
<code>l:id:</code> <?= $keyboardIndexStrings['to search for a BCP 47 language tag, for example'] ?>
<code>l:id:ti-et</code> <?= $keyboardIndexStrings['searches for Tigrigna (Ethiopia)'] ?>
<?= _('Use prefix') ?>
<code>l:id:</code> <?= _('to search for a BCP 47 language tag, for example') ?>
<code>l:id:ti-et</code> <?= _('searches for Tigrigna (Ethiopia)') ?>
</li>
</ul>
</div>
Expand Down

0 comments on commit 19510be

Please sign in to comment.