forked from metabase/metabase
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial i18n implementation for frontend and backend
- Loading branch information
1 parent
4ef778d
commit bad9a2a
Showing
14 changed files
with
281 additions
and
13 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,36 @@ | ||
#!/usr/bin/env node | ||
|
||
// This program compiles a ".po" translations file to a JSON version suitable for use on the frontend | ||
// It removes strings that aren't used on the frontend, and other extraneous information like comments | ||
|
||
const fs = require("fs"); | ||
const _ = require("underscore"); | ||
const gParser = require("gettext-parser"); | ||
|
||
if (process.argv.length !== 4) { | ||
console.log("USAGE: build-translation-frontend-resource input.po output.json"); | ||
process.exit(1); | ||
} | ||
|
||
const inputFile = process.argv[2]; | ||
const outputFile = process.argv[3]; | ||
|
||
const translationObject = gParser.po.parse(fs.readFileSync(inputFile, "utf-8")); | ||
|
||
// NOTE: unsure why the headers are duplicated in a translation for "", but we don't need it | ||
delete translationObject.translations[""][""] | ||
|
||
for (const id in translationObject.translations[""]) { | ||
const translation = translationObject.translations[""][id]; | ||
if (!translation.comments.reference || _.any(translation.comments.reference.split("\n"), reference => reference.startsWith("frontend/"))) { | ||
// remove comments: | ||
delete translation.comments; | ||
// NOTE: would be nice if we could remove the message id since it's redundant: | ||
// delete translation.msgid; | ||
} else { | ||
// remove strings that aren't in the frontend | ||
delete translationObject.translations[""][id]; | ||
} | ||
} | ||
|
||
fs.writeFileSync(outputFile, JSON.stringify(translationObject, null, 2), "utf-8"); |
This file contains 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,41 @@ | ||
#!/bin/sh | ||
|
||
set -eu | ||
|
||
# gettext installed via homebrew is "keg-only", add it to the PATH | ||
if [ -d "/usr/local/opt/gettext/bin" ]; then | ||
export PATH="/usr/local/opt/gettext/bin:$PATH" | ||
fi | ||
|
||
POT_NAME="locales/metabase.pot" | ||
LOCALES=$(find locales -type f -name "*.po" -exec basename {} .po \;) | ||
|
||
FRONTEND_LANG_DIR="resources/frontend_client/app/locales" | ||
|
||
# backend | ||
cat << EOF > "resources/locales.clj" | ||
{ | ||
:locales #{"de_DE" "en"} | ||
:packages ["metabase"] | ||
:bundle "metabase.Messages" | ||
} | ||
EOF | ||
|
||
mkdir -p "$FRONTEND_LANG_DIR" | ||
|
||
for LOCALE in $LOCALES; do | ||
LOCALE_FILE="locales/$LOCALE.po" | ||
# frontend | ||
# NOTE: just copy these for now, but eventially precompile from .po to .json | ||
./bin/i18n/build-translation-frontend-resource \ | ||
"$LOCALE_FILE" \ | ||
"$FRONTEND_LANG_DIR/$LOCALE.json" | ||
|
||
# backend | ||
msgfmt \ | ||
--java2 \ | ||
-d "resources" \ | ||
-r "metabase.Messages" \ | ||
-l "$LOCALE" \ | ||
"$LOCALE_FILE" | ||
done |
This file contains 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,22 @@ | ||
#!/bin/sh | ||
|
||
set -eu | ||
|
||
# gettext installed via homebrew is "keg-only", add it to the PATH | ||
if [ -d "/usr/local/opt/gettext/bin" ]; then | ||
export PATH="/usr/local/opt/gettext/bin:$PATH" | ||
fi | ||
|
||
POT_NAME="locales/metabase.pot" | ||
PO_NAME="locales/$1.po" | ||
|
||
if [ $# -lt 1 ]; then | ||
echo "USAGE: update-translation en_US" | ||
exit 1 | ||
fi | ||
|
||
if [ -f "$PO_NAME" ]; then | ||
exec msgmerge -U "$PO_NAME" "$POT_NAME" | ||
else | ||
exec msginit -i "$POT_NAME" -o "$PO_NAME" -l "$1" | ||
fi |
This file contains 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,48 @@ | ||
#!/bin/sh | ||
|
||
set -eu | ||
|
||
# gettext installed via homebrew is "keg-only", add it to the PATH | ||
if [ -d "/usr/local/opt/gettext/bin" ]; then | ||
export PATH="/usr/local/opt/gettext/bin:$PATH" | ||
fi | ||
|
||
POT_NAME="locales/metabase.pot" | ||
POT_BACKEND_NAME="locales/metabase-backend.pot" | ||
POT_FRONTEND_NAME="locales/metabase-frontend.pot" | ||
|
||
mkdir -p "locales" | ||
|
||
# update frontend pot | ||
|
||
# NOTE: about twice as fast to call babel directly rather than a full webpack build | ||
BABEL_ENV=extract ./node_modules/.bin/babel -q -x .js,.jsx -o /dev/null frontend/src | ||
# BABEL_ENV=extract BABEL_DISABLE_CACHE=1 yarn run build | ||
|
||
# update backend pot | ||
|
||
# xgettext before 0.19 does not understand --add-location=file. Even CentOS | ||
# 7 ships with an older gettext. We will therefore generate full location | ||
# info on those systems, and only file names where xgettext supports it | ||
LOC_OPT=$(xgettext --add-location=file -f - </dev/null >/dev/null 2>&1 && echo --add-location=file || echo --add-location) | ||
|
||
find src -name "*.clj" | xgettext \ | ||
--from-code=UTF-8 \ | ||
--language=lisp \ | ||
--copyright-holder='Metabase <[email protected]>' \ | ||
--package-name="metabase" \ | ||
--msgid-bugs-address="[email protected]" \ | ||
-k \ | ||
-kmark:1 -ki18n/mark:1 \ | ||
-ktrs:1 -ki18n/trs:1 \ | ||
-ktru:1 -ki18n/tru:1 \ | ||
-ktrun:1,2 -ki18n/trun:1,2 \ | ||
-ktrsn:1,2 -ki18n/trsn:1,2 \ | ||
$LOC_OPT \ | ||
--add-comments --sort-by-file \ | ||
-o $POT_BACKEND_NAME -f - | ||
|
||
sed -i "" -e 's/charset=CHARSET/charset=UTF-8/' "$POT_BACKEND_NAME" | ||
|
||
# merge frontend and backend pots | ||
msgcat "$POT_FRONTEND_NAME" "$POT_BACKEND_NAME" > "$POT_NAME" |
This file contains 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 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,13 @@ | ||
import MetabaseSettings from "metabase/lib/settings"; | ||
|
||
import { addLocale, useLocale } from "c-3po"; | ||
import { I18NApi } from "metabase/services"; | ||
|
||
export async function loadLocale(locale) { | ||
// load and parse the locale | ||
const translationsObject = await I18NApi.locale({ locale }); | ||
|
||
// add and set locale with C-3PO | ||
addLocale(locale, translationsObject); | ||
useLocale(locale); | ||
} |
This file contains 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 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 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 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
Oops, something went wrong.