Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Localize keyboards index page 📡 #521

Merged

Conversation

darcywong00
Copy link
Contributor

@darcywong00 darcywong00 commented Dec 11, 2024

Re-implements #500 for #384

This updates Locale.php for localizing the keyboards/ index page

Changes

Locale.php

  • Validate the new locale before overriding the current locale
  • setTextDomain() to manage the localization files
  • Add _s() wrapper

keyboards/index.php

Screenshots

Note: the "Search" button image asset contains English text.

French

http://keyman.com.localhost/keyboards/?lang=fr-FR

image

Spanish

http://keyman.com.localhost/keyboards/?lang=es-ES

image

@darcywong00 darcywong00 added this to the A18S17 milestone Dec 11, 2024
Comment on lines 78 to 99
public static function localize($domain, $strings) {
foreach(Locale::CROWDIN_LOCALES as $l) {
if ($l == Locale::DEFAULT_LOCALE) {
// Skip English
continue;
}

bindtextdomain("$domain-$l", __DIR__);
}

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

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

// Restore textdomain
textdomain($previousTextDomain);
return $result;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactored from #500 to Locale.php.
This way, pages don't need to make all the calls to bindtextdomain() and setTextDomain()

Comment on lines 64 to 65
msgid "%skeyboards%s"
msgstr "%stescados%s"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto for this file

Comment on lines 24 to 26
msgid "Keyboard search%s"
msgstr "Recherche au clavier%s"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

@@ -9,9 +9,35 @@
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', [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This all feels very fragile -- arrays across multiple files which have to match up. A lot of duplication of these strings -- 6 times:

  • twice in keyboards-en.po
  • once each in keyboards-es-ES.po, keyboards-fr-FR.po
  • twice in keyboards/index.php -- here and where referenced.

This will need automatic validation to keep in sync or it's going to get very hard to maintain. (Why do we need the array defined here as well as in the keyboards-en.po file? Can't we just use the list in that file as our starting point?)

It may be better to use an identifier which is not the whole string? e.g. I see we have also 'keyboard search' 'keyman keyboard search' 'keyboard search%s', 'search', 'new search' ... lots of repetition here and risk of confusion!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be sensible to establish a convention for a shorthand reference e.g. $_ rather than a long name like $keyboardIndexStrings

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on 🦆 -

  • Replaced $keyboardIndexStrings array with _() alias
  • Updated Locale::localize() so it doesn't restore textdomain


<div id='search-box'>
<form method='get' action='/keyboards' name='f'>
<label for="search-q">Keyboard search:</label><input id="search-q" type="text" placeholder="Enter language or keyboard" name="q"
<label for="search-q"><?= Locale::_s($keyboardIndexStrings['Keyboard search%s'], ":") ?></label><input id="search-q" type="text" placeholder="<?= $keyboardIndexStrings['Enter language or keyboard'] ?>" name="q"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why have you split the : out from the string? Here and other places? Seems to make the strings much more complicated than they need to be

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, I've consolidated punctuation back into the strings in edd7430

@darcywong00 darcywong00 modified the milestones: A18S19, A18S20 Jan 18, 2025
Copy link
Member

@mcdurdin mcdurdin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks a lot easier to maintain. Good work!

@darcywong00 darcywong00 merged commit 8c156b7 into feat/localize/keyboard-search Jan 20, 2025
2 checks passed
@darcywong00 darcywong00 deleted the feat/localize/keyboard/index branch January 20, 2025 04:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

2 participants