Skip to content

Commit

Permalink
add wrappers to setup locale files
Browse files Browse the repository at this point in the history
  • Loading branch information
darcywong00 committed Nov 30, 2024
1 parent f84e5a1 commit ac28241
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
39 changes: 36 additions & 3 deletions _includes/locale/locale.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,23 @@
'fr-FR' => 'Français'
];

// Wrapper to format string with gettext '_(' alias and variable args
function _s($s, ...$args) {
return vsprintf(_($s), $args);
/**
* Wrapper to setup locale files
* basePoFile - base filename of the .po file (not including the locales or .po extension)
* pathToLocale - absoluate path to _includes/locale
*/
function setupLocale($basePoFile, $pathToLocale) {
// Container uses English locale, but use setTextDomain to change localization as needed
setLocale(LC_ALL, 'en_US.UTF-8');

// Iterate through locales for bindtextdomain
foreach(array_keys($locales) as $code) {
if ($code == 'en') {
// Skip appending English .po name
continue;
}
bindtextdomain("keyboards-${code}", $pathToLocale);
}
}

/**
Expand All @@ -28,3 +42,22 @@ function setTextDomain($locale, $basename) {
return;
}
}

/**
* Wrapper to format string with gettext '_(' alias and variable args
* s - the format string
* args - optional remaining args to the format string
*/
function _s($s, ...$args) {
return vsprintf(_($s), $args);
}

/**
* Wrapper of echo and _s
*/
function echo_s($s, ...$args) {
$tempString = _s($s, $args);
echo '$tempString';
}


5 changes: 1 addition & 4 deletions keyboards/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@
use Keyman\Site\com\keyman\templates\Body;
use Keyman\Site\com\keyman\templates\Foot;

// Container uses English locale, but use setTextDomain to change localization as needed
setLocale(LC_ALL, 'en_US.UTF-8');
bindtextdomain("keyboards-fr-FR", __DIR__ . "/../_includes/locale");
bindtextdomain("keyboards-es-ES", __DIR__ . "/../_includes/locale");
setupLocale("keyboards", __DIR__ . "/../_includes/locale");

// $embed_locale set by session.php
setTextDomain($embed_locale, "keyboards");
Expand Down

0 comments on commit ac28241

Please sign in to comment.