Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
funkwhale
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Creak
funkwhale
Commits
593a1fe5
Verified
Commit
593a1fe5
authored
6 years ago
by
Eliot Berriot
Browse files
Options
Downloads
Patches
Plain Diff
Added script to populate translated and contextualized strings if possible
parent
54f7cc6e
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
front/scripts/contextualize.py
+82
-0
82 additions, 0 deletions
front/scripts/contextualize.py
front/scripts/i18n-populate-contextualized-strings.sh
+21
-0
21 additions, 0 deletions
front/scripts/i18n-populate-contextualized-strings.sh
with
103 additions
and
0 deletions
front/scripts/contextualize.py
0 → 100644
+
82
−
0
View file @
593a1fe5
import
argparse
import
polib
def
get_missing
(
entries
):
"""
Return a list of entries with:
- a msgcontext
- an empty msgstr
"""
for
e
in
entries
:
if
e
.
translated
():
continue
yield
e
return
[]
def
match
(
entries
,
other_entries
):
"""
Given two list of po entries, will return a list of 2-tuples with
match from the second list
"""
by_id
=
{}
for
e
in
other_entries
:
is_translated
=
bool
(
e
.
msgstr
)
if
not
is_translated
:
continue
by_id
[
e
.
msgid
]
=
e
matches
=
[]
for
e
in
entries
:
matches
.
append
((
e
,
by_id
.
get
(
e
.
msgid
)))
return
matches
def
update
(
new
,
old
):
"""
Update a new po entry with translation from the first one (removing fuzzy if needed)
"""
new
.
msgstr
=
old
.
msgstr
new
.
flags
=
[
f
for
f
in
new
.
flags
if
f
!=
"
fuzzy
"
]
def
contextualize
(
old_po
,
new_po
,
edit
=
False
):
old
=
polib
.
pofile
(
old_po
)
new
=
polib
.
pofile
(
new_po
)
missing
=
list
(
get_missing
(
new
))
print
(
"
Found {} entries with contexts and missing translations ({} total)
"
.
format
(
len
(
missing
),
len
(
new
)
)
)
matches
=
match
(
missing
,
old
)
found
=
[
m
for
m
in
matches
if
m
[
1
]
is
not
None
]
print
(
"
Found {} matching entries
"
.
format
(
len
(
found
)))
if
edit
:
print
(
"
Applying changes
"
)
for
matched
,
matching
in
found
:
update
(
matched
,
matching
)
new
.
save
()
else
:
print
(
"
--no-dry-run not provided, not applying change
"
)
if
__name__
==
"
__main__
"
:
parser
=
argparse
.
ArgumentParser
(
description
=
"""
Given two .po file paths, it will populate empty contextualized messages
in the second one with matching message IDs from the first one, if any.
This is especially helpful when you add some contexts on existing translated strings
but don
'
t want to have those being retranslated.
"""
)
parser
.
add_argument
(
"
old_po
"
,
help
=
"
Path of the po file to use as a source
"
)
parser
.
add_argument
(
"
new_po
"
,
help
=
"
Path of the po file to populate
"
)
parser
.
add_argument
(
"
--no-dry-run
"
,
action
=
"
store_true
"
)
args
=
parser
.
parse_args
()
contextualize
(
old_po
=
args
.
old_po
,
new_po
=
args
.
new_po
,
edit
=
args
.
no_dry_run
)
This diff is collapsed.
Click to expand it.
front/scripts/i18n-populate-contextualized-strings.sh
0 → 100755
+
21
−
0
View file @
593a1fe5
#!/bin/bash -eu
# Typical use:
# cp -r locales old_locales
# ./scripts/i18n-extract.sh
# ./scripts/i18n-populate-contextualized-strings.sh old_locales locales
# Then review/commit the changes
old_locales_dir
=
$1
new_locales_dir
=
$2
locales
=
$(
tail
-n
+2 src/locales.js |
sed
-e
's/export default //'
| jq
'.locales[].code'
| xargs
echo
)
# Generate .po files for each available language.
echo
$locales
for
lang
in
$locales
;
do
echo
"Fixing contexts for
$lang
…"
old_po_file
=
$old_locales_dir
/
$lang
/LC_MESSAGES/app.po
new_po_file
=
$new_locales_dir
/
$lang
/LC_MESSAGES/app.po
python3 ./scripts/contextualize.py
$old_po_file
$new_po_file
--no-dry-run
done
;
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment