Skip to content

Commit

Permalink
Integrate internationalization component
Browse files Browse the repository at this point in the history
  • Loading branch information
ocram committed Jun 15, 2019
1 parent c961416 commit fb8067b
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 0 deletions.
6 changes: 6 additions & 0 deletions config/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ MAIL_USERNAME=user@example.com
MAIL_PASSWORD=monkey
MAIL_TLS=0

# The (comma-separated) list of supported locales (see 'https://github.com/delight-im/PHP-I18N/blob/master/src/Codes.php' for possible values)
I18N_SUPPORTED_LOCALES=
I18N_SESSION_FIELD=
I18N_COOKIE_NAME=
I18N_COOKIE_LIFETIME=31556952

# The settings of the encoder/decoder that can be used to obfuscate IDs
SECURITY_IDS_ALPHABET=GZwBHpfWybgQ5d_2mM-jh84K69tqYknx7LN3zvDrcSJVRPXsCFT
SECURITY_IDS_PRIME=1125812041
Expand Down
69 changes: 69 additions & 0 deletions i18n.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#!/bin/bash

### PHP-I18N (https://github.com/delight-im/PHP-I18N)
### Copyright (c) delight.im (https://www.delight.im/)
### Licensed under the MIT License (https://opensource.org/licenses/MIT)

set -eu

# Switch to the directory where the current script is located
cd "${BASH_SOURCE%/*}" || exit 1

echo "Extracting and updating translations"

LOCALE_CODE="${1:-}"
LOCALE_PARENT_DIR="${2:-locale}"
LOCALE_DOMAIN="${3:-messages}"

if [ ! -d "${LOCALE_PARENT_DIR}" ]; then
echo " * Error: Target directory “${LOCALE_PARENT_DIR}” not found"
exit 2
fi

if [ ! -w "${LOCALE_PARENT_DIR}" ]; then
echo " * Error: Target directory “${LOCALE_PARENT_DIR}” not writable"
exit 3
fi

if [ -z "${LOCALE_CODE}" ]; then
echo " * Creating generic POT (Portable Object Template) file"
fi

find . -iname "*.php" -not -path "./vendor/*" | xargs xgettext --output="${LOCALE_DOMAIN}.pot" --output-dir="${LOCALE_PARENT_DIR}" --language=PHP --from-code=UTF-8 --force-po --no-location --no-wrap --sort-output --copyright-holder="" --keyword --keyword="_:1,1t" --keyword="_f:1" --keyword="_fe:1" --keyword="_p:1,2,3t" --keyword="_pf:1,2" --keyword="_pfe:1,2" --keyword="_c:1,2c,2t" --keyword="_m:1,1t" --flag="_f:1:php-format" --flag="_fe:1:no-php-format" --flag="_pf:1:php-format" --flag="_pfe:1:no-php-format"
sed -i '/# SOME DESCRIPTIVE TITLE./d' "${LOCALE_PARENT_DIR}/${LOCALE_DOMAIN}.pot"
sed -i '/# This file is put in the public domain./d' "${LOCALE_PARENT_DIR}/${LOCALE_DOMAIN}.pot"
sed -i '/# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR./d' "${LOCALE_PARENT_DIR}/${LOCALE_DOMAIN}.pot"
sed -i '0,/#, fuzzy/{s/#, fuzzy//}' "${LOCALE_PARENT_DIR}/${LOCALE_DOMAIN}.pot"
sed -i '/\"Project-Id-Version: PACKAGE VERSION\\n\"/d' "${LOCALE_PARENT_DIR}/${LOCALE_DOMAIN}.pot"
sed -i '/\"Report-Msgid-Bugs-To: \\n\"/d' "${LOCALE_PARENT_DIR}/${LOCALE_DOMAIN}.pot"
sed -i '/\"POT-Creation-Date: /d' "${LOCALE_PARENT_DIR}/${LOCALE_DOMAIN}.pot"
sed -i '/\"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\\n\"/d' "${LOCALE_PARENT_DIR}/${LOCALE_DOMAIN}.pot"
sed -i '/\"Last-Translator: FULL NAME <EMAIL@ADDRESS>\\n\"/d' "${LOCALE_PARENT_DIR}/${LOCALE_DOMAIN}.pot"
sed -i '/\"Language-Team: LANGUAGE <LL@li.org>\\n\"/d' "${LOCALE_PARENT_DIR}/${LOCALE_DOMAIN}.pot"
sed -i '0,/\"Language: \\n\"/{s/\"Language: \\n\"/\"Language: xx\\n\"/}' "${LOCALE_PARENT_DIR}/${LOCALE_DOMAIN}.pot"
sed -i '0,/\"Content-Type: text\/plain; charset=CHARSET\\n\"/{s/\"Content-Type: text\/plain; charset=CHARSET\\n\"/\"Content-Type: text\/plain; charset=UTF-8\\n\"/}' "${LOCALE_PARENT_DIR}/${LOCALE_DOMAIN}.pot"

if [ ! -z "${LOCALE_CODE}" ]; then
LOCALE_CONTENTS_DIR="${LOCALE_PARENT_DIR}/${LOCALE_CODE}/LC_MESSAGES"

mkdir --parents "${LOCALE_CONTENTS_DIR}"

echo " * Creating PO (Portable Object) file for “${LOCALE_CODE}"

if [ -f "${LOCALE_CONTENTS_DIR}/${LOCALE_DOMAIN}.po" ]; then
msgmerge --update --backup=none --suffix=".bak" --previous --force-po --no-location --no-wrap --sort-output "${LOCALE_CONTENTS_DIR}/${LOCALE_DOMAIN}.po" "${LOCALE_PARENT_DIR}/${LOCALE_DOMAIN}.pot"
else
msginit --input="${LOCALE_PARENT_DIR}/${LOCALE_DOMAIN}.pot" --output-file="${LOCALE_CONTENTS_DIR}/${LOCALE_DOMAIN}.po" --locale="${LOCALE_CODE}" --no-translator --no-wrap
sed -i '/\"Project-Id-Version: /d' "${LOCALE_CONTENTS_DIR}/${LOCALE_DOMAIN}.po"
sed -i '/\"Last-Translator: Automatically generated\\n\"/d' "${LOCALE_CONTENTS_DIR}/${LOCALE_DOMAIN}.po"
sed -i '/\"Language-Team: none\\n\"/d' "${LOCALE_CONTENTS_DIR}/${LOCALE_DOMAIN}.po"
fi

echo " * Creating MO (Machine Object) file for “${LOCALE_CODE}"

msgfmt --output-file="${LOCALE_CONTENTS_DIR}/${LOCALE_DOMAIN}.mo" --check-format --check-domain "${LOCALE_CONTENTS_DIR}/${LOCALE_DOMAIN}.po"

rm "${LOCALE_PARENT_DIR}/${LOCALE_DOMAIN}.pot"
fi

echo "Done"
22 changes: 22 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,5 +96,27 @@ function e($str) {
\define('TYPE_TEXT', \Delight\Foundation\Input::DATA_TYPE_TEXT);
\define('TYPE_RAW', \Delight\Foundation\Input::DATA_TYPE_RAW);

// reluctantly put some functions into the global namespace for maximum convenience
function _f($text, ...$replacements) { global $app; return $app->i18n()->translateFormatted($text, ...$replacements); }
function _fe($text, ...$replacements) { global $app; return $app->i18n()->translateFormattedExtended($text, ...$replacements); }
function _p($text, $alternative, $count) { global $app; return $app->i18n()->translatePlural($text, $alternative, $count); }
function _pf($text, $alternative, $count, ...$replacements) { global $app; return $app->i18n()->translatePluralFormatted($text, $alternative, $count, ...$replacements); }
function _pfe($text, $alternative, $count, ...$replacements) { global $app; return $app->i18n()->translatePluralFormattedExtended($text, $alternative, $count, ...$replacements); }
function _c($text, $context) { global $app; return $app->i18n()->translateWithContext($text, $context); }
function _m($text) { global $app; return $app->i18n()->markForTranslation($text); }

if ($app->isClientCli() || $app->isClientLoopback()) {
if ($app->hasCliArgument()) {
if ($app->getCliArgument() === 'clear-template-cache') {
$app->clearTemplateCache();
exit;
}
elseif ($app->getCliArgument() === 'precompile-templates') {
$app->precompileTemplates();
exit;
}
}
}

// include the actual application code
require __DIR__ . '/app/index.php';
4 changes: 4 additions & 0 deletions locale/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Ignore MO (Machine Object) files
*.mo
# Ignore POT (Portable Object Template) files
*.pot

0 comments on commit fb8067b

Please sign in to comment.