Replacing specific text from document A to B
-
Hi there, I was wondering if anyone can help me do this:
I have two documents opened side by side (as shown below):
I am trying to get the already-translated text from document A (on the left) to the respective lines in document B. I cannot just copy-paste this, because I know there might be some lines that can have different “keys” and are on different lines.
The thing is (as you can see on the screenshot) I cannot just select lines from document A and paste them into document B, because they are on the different lines.My question is: Is there a way to replace text in the document B if it matches the “key” in the document A? Such as (as for the screenshot):
- I have a key called “searchInputHintOnTable” in the document A. If there is the same key in the document B, use the text ‘Search by alert name’ (from document A) to replace the ‘Hledat podle názvu’ (in document B).
I hope I explained it well enough, but I will be more than happy to specify this as much as possible.
Thank you.
-
You really should follow the instructions HERE for posting such a question.
-
Hello, @Václav-Trunec and All,
I’ve got a solution to your problem which is a bit long, but without any major difficulty !
It will use :
-
5
regex search/replacements -
2
sort operations -
The use of the
column editor
to number the lines
I assume that your initial
Document A
contains only lines like :Key: 'English strings',
and that your initial
Document B
contains only lines like :Key: 'Czech strings',
A last advice : Better to read carefully all this post first, before beginning any operation !!
Regarding the
5
regex S/R, you’ll follow these steps :-
Open the Replace dialog (
Ctrl + H
) -
Type in the SEARCH regex in the
Find what:
field -
Type in the REPLACE regex in the
replace with:
field -
Untick all options
-
Tick the
Wrap around
option -
Select the
Regular expression
search mode -
Click preferably on the
Replace All
button ( Do not use theReplace
button in the fourth regex S/R )
Let’s go :
-
Copy/paste the
Document A
contents in a newDocument C
-
Use this first regex S/R, in
Document C
:-
SEARCH
^(\w+):
-
REPLACE
\1 CCC:
-
=> Contents of
Document C
should look as below :... ... searchInputHintOnTable CCC: 'Search by alert name', searchInputHint CCC: 'Find the alert belonging to a specific alert by its name.', ... ...
-
Copy/paste the
Document B
contents in a newDocument D
-
Use this second regex S/R, in
Document D
:-
SEARCH
^(\w+):
-
REPLACE
\1 DDD:
-
=> Contents of
Document D
should look as below :... ... searchInputHint DDD: 'Najděte upozornění patřící konkrétnímu alertu podle jeho názvu', searchInputHintOnTable DDD: 'Hledat podle nazvu alertu', ... ...
-
Now, try to find out the longest line of
Document D
. Its length will be set to theLm
variable -
Move to the first line of
Document D
. Its length will be set to theL1
variable -
Determine the value
X
=Lm - L1 + 5
-
Move at the end of the first line of
Document D
-
Add
X space
chars -
Open the Column Editor (
Alt + C
)-
Choose the
Number to Insert
option -
Type in the value
1
in theInitial number :
field -
Type in the value
1
in theIncrease by :
field -
Type in the value
1
in theRepeat :
field -
Tick the
Leading zeros
option ( IMPORTANT ) -
Verify that the
Dec
option is selected -
Click on the
OK
button
-
=> A numbering should appear at the end of each line of
Document D
... ... searchInputHint DDD: 'Najděte upozornění patřící konkrétnímu alertu podle jeho názvu', xxxx searchInputHintOnTable DDD: 'Hledat podle nazvu alertu', yyyy ... ...
- Merge
document C
andDocument D
in a newDocument E
=> Contents of
Document E
should look as below :... ... searchInputHintOnTable CCC: 'Search by alert name', searchInputHint CCC: 'Find the alert belonging to a specific alert by its name.', ... ... searchInputHint DDD: 'Najděte upozornění patřící konkrétnímu alertu podle jeho názvu', xxxx searchInputHintOnTable DDD: 'Hledat podle nazvu alertu', yyyy ... ...
-
Now, in
Document E
:-
Suppress any selection
-
Use the
Edit > Line Operations > Sort Lines Lexicographically Ascending
option
-
=> The contents of
Document E
become :... ... searchInputHint CCC: 'Find the alert belonging to a specific alert by its name.', searchInputHint DDD: 'Najděte upozornění patřící konkrétnímu alertu podle jeho názvu', xxxx .... .... searchInputHintOnTable CCC: 'Search by alert name', searchInputHintOnTable DDD: 'Hledat podle nazvu alertu', yyyy .... ....
-
Use this third regex S/R, in
Document E
:-
SEARCH
(?-is)^(.+) CCC: (.+)\R\1 DDD: \K.+,
-
REPLACE
\2
-
=> The contents of
Document E
are now changed into :... ... searchInputHint CCC: 'Find the alert belonging to a specific alert by its name.', searchInputHint DDD: 'Find the alert belonging to a specific alert by its name.', xxxx ... ... searchInputHintOnTable CCC: 'Search by alert name', searchInputHintOnTable DDD: 'Search by alert name', yyyy ... ...
-
Use this fourth regex S/R, in
Document E
:-
SEARCH
(?-is)^.+ CCC: .+\R|^(.+ DDD: .+?)\x20+(\d+)$
-
REPLACE
(?1\2 \1:)
-
=>
-
All the lines containing the
CCC:
string, surrounded byspace
chars, should be deleted : -
Numbering, in lines containing the
DDD:
string, surrounded byspace
chars, should have moved from the end to the beginning of each line -
Remember that
xxxx
andyyyy
are numbers !
... ... xxxx searchInputHint DDD: 'Find the alert belonging to a specific alert by its name.', ... ... yyyy searchInputHintOnTable DDD: 'Search by alert name', ... ...
-
Again, in
Document E
:-
Suppress any selection
-
Use the
Edit > Line Operations > Sort Lines Lexicographically Ascending
option
-
=> This time, all the lines should be sorted, according to their initial location in
Document B
andDocument D
... ... xxxx searchInputHint DDD: 'Find the alert belonging to a specific alert by its name.', yyyy searchInputHintOnTable DDD: 'Search by alert name', ... ...
-
Use this fifth and final regex S/R, in
Document E
:-
SEARCH
(?-i)^\d+\x20{4}|\x20DDD(?=:\x20)
-
REPLACE
Leave EMPTY
-
Your final
Document E
should look like below :... ... searchInputHint: 'Find the alert belonging to a specific alert by its name.', searchInputHintOnTable: 'Search by alert name', ... ...
-
The order of lines, in
Document E
, should be identical to theDocument B
order -
The
Czech
version of all lines, inDocument B
andDocument E
, should have been replaced with theirEnglish
equivalents !
Best Regards
guy038
-
-
First of all, thank you very much for the thorough explanation, it is absolutely wonderful.
However, I am stuck at this thing in Document E:
- Use the Edit > Line Operations > Sort Lines Lexicographically Ascending option
If I do this, the document will not be usable for our developers, since it also moves the { and } characters, determining the end of a translation key. Is there a way to skip this?
Thank you again.
-
@václav-trunec said in Replacing specific text from document A to B:
If I do this, the document will not be usable for our developers, since it also moves the { and } characters, determining the end of a translation key. Is there a way to skip this?
If you follow Guy’s full instructions, you will see that he added a “key” (the extra text) which will allow his two different sorts to undo each other: he sorts on that “key”, and then in the end, it re-sorts back to the original order. As Guy said, “The order of lines, in
Document E
, should be identical to theDocument B
order” after his last step.-----
Alternatively, I recently published a script in a similar discussion, which will allow you set up pairs of English/OtherLanguage translation strings (or, in your case, OtherLanguage-to-English). If you follow the instructions in that post, but use the original/Czech as the left side of the translation dictionary and the translated/English as the right side, it should work.
So, instead of using the example from that discussion:
translation = { r'"JJIS number:"': r'"د JJIS شمیره:"', r'"Choose one"': r'"یو يې غوره کړئ"', r'"Social Security number:"': r'"د ټولنیز مصؤنیت لمبر:"', r'"State ID \(SID\):"': r'"د ایالت ID \(SID\):"' }
… you would use something like …
translation = { r'Najděte upozornění patřící konkrétnímu alertu podle jeho názvu': r'Find the alert belonging to a specific alert by its name.', r'Hledat podle nazvu alertu': r'Search by alert name' }
(remembering to end every pair except the last one with a comma