-
-
Notifications
You must be signed in to change notification settings - Fork 6
feat: Use associative array for strings on keyboard search page 🗺️ #604
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
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
689d15b
feat: Use associate array for strings on keyboard search page
darcywong00 755a2df
wip: Start implementing review comments
darcywong00 db8aa79
refactor load strings
darcywong00 c715fb8
Cleanup fallback of locale
darcywong00 d446584
refactor callback locales
darcywong00 79dbb48
chore: more cleanup
darcywong00 faa45cf
fix: variadric args
darcywong00 76c9d0a
set lang tag and fallback to English
darcywong00 b2313a8
simplify some keys
darcywong00 ab2a5a1
Merge remote-tracking branch 'origin/master' into feat/localize/keybo…
darcywong00 82c6671
fix: add globe icon for UI selector
darcywong00 ed1c38b
fix: Update globe to hover
darcywong00 2fd6527
fix: Parse lang string in URI query
darcywong00 0bc40f5
refactor render_globe_dropdown()
darcywong00 54a0de5
chore: Add header to render_globe_dropdown
darcywong00 c1638ca
die if current locales aren't set
darcywong00 cacc840
chore: Address review comments
darcywong00 75abde9
Merge branch 'master' into feat/localize/keyboards-map
darcywong00 f1f8a62
fix: Pass div number for render_globe_dropdown()
darcywong00 4fe6b0c
test: exclude link-checking locales 'lang=*'
darcywong00 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,169 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * Keyman is copyright (C) SIL Global. MIT License. | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Keyman\Site\com\keyman; | ||
|
|
||
| use \Keyman\Site\Common\KeymanHosts; | ||
|
|
||
| class Locale { | ||
| public const DEFAULT_LOCALE = 'en'; | ||
|
|
||
| // array of the support locales | ||
| // xx-YY locale as specified in crowdin %locale% | ||
| private static $currentLocales = []; | ||
|
|
||
| // strings is an array of domains. | ||
| // Each domain is an array of locales | ||
| // Each locale is an object? with loaded flag and array of strings | ||
| private static $strings = []; | ||
|
|
||
| /** | ||
| * Return the current locales. Fallback to 'en' | ||
| * @return $currentLocales | ||
| */ | ||
| public static function currentLocales() { | ||
| return self::$currentLocales; | ||
| } | ||
|
|
||
| /** | ||
| * Set the current locales, with an array of fallbacks, ending in 'en'. | ||
| * @param $locale - the new current locale (xx-YY as specified in crowdin %locale%) | ||
| */ | ||
| public static function setLocale($locale) { | ||
| // Clear current locales | ||
| self::$currentLocales == []; | ||
|
|
||
| if (!empty($locale)) { | ||
| self::$currentLocales = self::calculateFallbackLocales($locale); | ||
| } | ||
|
|
||
| // Push default fallback locale to the end | ||
| array_push(self::$currentLocales, Locale::DEFAULT_LOCALE); | ||
| } | ||
|
|
||
| /** | ||
| * Load the strings for the given domain | ||
| * @param $domain - the domain of strings | ||
| * @return boolean - true if successfully loaded strings | ||
| */ | ||
| public static function loadDomain($domain) { | ||
| self::$strings[$domain] = []; | ||
| $path = __DIR__ . '/strings/' . $domain . '/*.php'; | ||
| $files = glob(__DIR__ . '/strings/' . $domain . '/*.php'); | ||
| if ($files == false) { | ||
| return false; | ||
| } | ||
| foreach ($files as $file) { | ||
| // files are named by locale | ||
| $file = pathinfo($file, PATHINFO_FILENAME); | ||
| self::$strings[$domain][$file] = (object)[ | ||
| 'strings' => [], | ||
| 'loaded' => false | ||
| ]; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * Reads localized strings from the specified $domain-locale.php file | ||
| * @param $domain - base filename of the .php file containing localized strings | ||
| * (name not including -xx-YY locale) | ||
| * path is relative to _includes/locale/ | ||
| * @param $locale - locale for the strings to load | ||
| */ | ||
| public static function loadStrings($domain, $locale) { | ||
| $currentLocaleFilename = sprintf("%s/%s/%s", | ||
| __DIR__ . '/strings/', | ||
| $domain, | ||
| $locale . '.php'); | ||
|
|
||
| if (file_exists($currentLocaleFilename)) { | ||
| self::$strings[$domain][$locale]-> strings = require $currentLocaleFilename; | ||
| self::$strings[$domain][$locale]-> loaded = true; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Given a locale, return an array of fallback locales | ||
| * For example: es-ES --> [es, es-ES] | ||
| * TODO: Use an existing fallback algorthim like | ||
| * https://cldr.unicode.org/development/development-process/design-proposals/language-distance-data | ||
| * @param $locale - the locale to determine fallback locales | ||
| * @return array of fallback locales | ||
| */ | ||
| private static function calculateFallbackLocales($locale) { | ||
| // Start with the given locale | ||
| $fallback = [$locale]; | ||
|
|
||
| // Support other fallbacks such as es-419 -> es | ||
| $parts = explode('-', $locale); | ||
| for ($i = count($parts)-1; $i > 0; $i--) { | ||
| $lastPosition = strrpos($locale, $parts[$i]) - 1; | ||
| // Insert language tag substring to head | ||
| array_unshift($fallback, substr($locale, 0, $lastPosition)); | ||
| } | ||
|
|
||
| return $fallback; | ||
| } | ||
|
|
||
| /** | ||
| * Wrapper to lookup string. Fallback to English | ||
| * @param $domain - the domain file | ||
| * @param $id - the key | ||
| * @return localized string, or fallback to English string, and then $id | ||
| */ | ||
| private static function getString($domain, $id) { | ||
| if (!array_key_exists($domain, self::$strings)) { | ||
| // Load the domain if it doesn't exist | ||
| if (!self::loadDomain($domain)) { | ||
| die('Domain ' . $domain . "doesn't exist"); | ||
| } | ||
| } | ||
|
|
||
| if (count(self::currentLocales()) == 0) { | ||
| die('Current locales haven\'t been set by session'); | ||
| } | ||
|
|
||
| foreach (self::currentLocales() as $locale) { | ||
| if (!array_key_exists($locale, self::$strings[$domain])) { | ||
| continue; | ||
| } | ||
|
|
||
| if (!self::$strings[$domain][$locale]->loaded) { | ||
| // Will set -> loaded = true | ||
| self::loadStrings($domain, $locale); | ||
| } | ||
|
|
||
| if (array_key_exists($id, self::$strings[$domain][$locale]->strings)) { | ||
| return self::$strings[$domain][$locale]->strings[$id]; | ||
| } | ||
| } | ||
|
|
||
| // String not found in any localization - | ||
| if(KeymanHosts::Instance()->Tier() == KeymanHosts::TIER_DEVELOPMENT) { | ||
| die('string ' . $id . ' is missing in all the l10ns'); | ||
| } | ||
| return $id; | ||
| } | ||
|
|
||
| /** | ||
| * Wrapper to lookup localized string for webpage domain. | ||
| * Formatted string using optional variable args for placeholders | ||
| * should escape like %1\$s | ||
| * @param $domain - the PHP file using the localized strings | ||
| * @param $id - the id for the string | ||
| * @param $args - optional remaining args to the format string | ||
| */ | ||
| public static function m($domain, $id, ...$args) { | ||
| $str = self::getString($domain, $id); | ||
| if (count($args) == 0) { | ||
| return $str; | ||
| } | ||
| return vsprintf($str, ...$args); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| # Guidance on strings for keyboards/index.php | ||
|
|
||
| ## Keyman is copyright (C) SIL Global. MIT License. | ||
|
|
||
| 1. **`page_title`** | ||
| - **Context**: Page Title | ||
|
|
||
| 2. **`page_description`** | ||
| - **Context**: Page Description | ||
|
|
||
| 3. **`keyboard_search"`** | ||
| - **Context**: Keyboard search bar | ||
|
|
||
| 4. **`enter_language`** | ||
| - **Context**: Search bar placeholder | ||
|
|
||
| 5. **`search`** | ||
| - **Context**: Search Button Value | ||
|
|
||
| 6. **`new_search`** | ||
| - **Context**: Link to start a new keyboard search | ||
|
|
||
| 7. **`enter_name`** | ||
| - **Context**: Search box instruction (Popular keyboards | All keyboards) | ||
|
|
||
| 8. **`popular_keyboards`** | ||
| - **Context**: Search box link for popular keyboards | ||
|
|
||
| 9. **`all_keyboards`** | ||
| - **Context**: Search box link for all Keyman keyboards | ||
|
|
||
| 10. **`hints`** | ||
| - **Context**: Search box hint: List header | ||
|
|
||
| 11. **`searchbox_description`** | ||
| - **Context**: Search box hint: Description | ||
|
|
||
| 12. **`searchbox_hint`** | ||
| - **Context**: Search box hint: available prefixes to use in the search | ||
|
|
||
| 13. **`(keyboards)`** | ||
| - **Context**: (keyboards) | ||
|
|
||
| 14. **`(languages)`** | ||
| - **Context**: (languages) | ||
|
|
||
| 15. **`(scripts, writing systems) or`** | ||
| - **Context**: (scripts, writing systems) or... | ||
|
|
||
| 16. **`(countries) to filter your search results. For example`** | ||
| - **Context**: (countries) to filter your search results... | ||
|
|
||
| 17. **`searches for keyboards for languages used in Thailand.`** | ||
| - **Context**: Search box hint: example of country search | ||
|
|
||
| 18. **`use_prefix`** | ||
| - **Context**: Search box hint: BCP 47 prefix | ||
|
|
||
| 19. **`to search for a BCP 47 language tag, for example`** | ||
| - **Context**: Search box hint: BCP 47 language example | ||
|
|
||
| 20. **`searches_tigrigna`** | ||
| - **Context**: Search box hint: BCP 47 language example |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why
m?I might overlook the obvious, but currently I have a hard time seeing a connection between
mand getting a localized string. I could make sense ofs,t, orl, but formI'm missing the association (which then makes it harder to use the function if I can't remember it).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm open to any clear name. In the previous iteration #516, we were using
Locale::_s()for formatted strings.That version was also using
_gettext, but we're not setting locales this time.