34
35:- module(i18n_trans, [translate/4,
36 translate/5,
37 translate_po/4,
38 rename_id/4,
39 replace_entry_po/4,
40 i18n_refactor/4]). 41
42:- use_module(library(lists)). 43:- use_module(library(pairs)). 44:- use_module(library(ref_changes)). 45:- use_module(library(ref_replacers)). 46:- use_module(library(i18n/i18n_file_utils)). 47:- use_module(library(i18n/i18n_parser)). 48:- use_module(library(i18n/i18n_support)). 49
51translate(M, Lang, TermEngl, TermLang, Options) :-
52 translate_po(M, Lang, TermEngl, TermLang),
53 replace_term(TermLang, TermEngl, [module(M)|Options]).
54
55translate(M, Lang, EnglLangL, Options) :-
56 forall(member(Engl-Lang, EnglLangL),
57 translate_po(M, Lang, Engl, Lang)),
58 replace_term(TermLang, TermEngl,
59 ( member(TermEngl-Lang, EnglLangL),
60 TermLang=@=Lang,
61 TermLang=Lang
62 ), [module(M)|Options]).
63
64translate_po(M, Lang, TermEngl, TermLang) :-
65 i18n_refactor(update_po_entry(Lang), M, TermLang, TermEngl).
70rename_id(M, OldTerm, NewTerm, Options) :-
71 i18n_refactor(rename_id_lang, M, OldTerm, NewTerm),
72 replace_term(OldTerm, NewTerm, [module(M)|Options]).
73
75:- meta_predicate replace_entry_po(+,+,+,2). 76replace_entry_po(M, MsgId1, MsgId, StrReplacer) :-
77 freeze(MsgStr1, call(StrReplacer, MsgStr1, MsgStr)),
78 findall(PoFile-Codes,
79 ( current_po_file(M, '??', PoFile),
80 read_po_file(PoFile, Entries1),
81 replace_entry(M, MsgId1, MsgStr1, MsgId, MsgStr, Entries1, Entries),
82 parse_po_entries(Entries, Codes, [])
83 ), FileCodes),
84 save_changes(FileCodes).
85
86:- meta_predicate i18n_refactor(3,+,+,+). 87
88i18n_refactor(PoUpdater, M, Term1, Term) :-
89 i18n_to_translate(Term1, _, KeysValues, []),
90 i18n_to_translate(Term, _, ValuesKeys, []),
91 pairs_keys_values(KeysValues, MsgId1, MsgId),
92 pairs_keys_values(ValuesKeys, MsgId, MsgId1),
93 call(PoUpdater, M, MsgId1, MsgId).
94
95update_po_entry(Lang, M, MsgStr, MsgId) :-
96 update_po_entry(Lang, M, MsgStr, MsgId, MsgStr).
97
98update_po_entry(Lang, M, MsgId1, MsgId, MsgStr) :-
99 current_po_file(M, Lang, PoFile),
100 read_po_file(PoFile, Entries1),
101 replace_entry(M, MsgId1, _, MsgId, MsgStr, Entries1, Entries),
102 save_to_po_file(Entries, PoFile).
103
104rename_id_lang(MsgId1, MsgId, M) :-
105 findall(PoFile-Codes,
106 ( current_po_file(M, '??', PoFile),
107 rename_id_po_file(PoFile, MsgId1, MsgId, M, Codes)
108 ), FileCodes),
109 save_changes(FileCodes).
110
111rename_id_po_file(PoFile, MsgId1, MsgId, M, Codes) :-
112 read_po_file(PoFile, Entries1),
113 replace_entry(M, MsgId1, MsgStr, MsgId, MsgStr, Entries1, Entries),
114 parse_po_entries(Entries, Codes, []).
115
116replace_entry(M, MsgId1, MsgStr1, MsgId, MsgStr, Entries1, Entries) :-
117 reference(M, Ref),
118 ( member(Entry1, Entries1),
119 Entry1 = entry(_, _, Ref, _, MsgId1, MsgStr1) ->
120 setarg(5, Entry1, MsgId),
121 setarg(6, Entry1, MsgStr),
122 Entries = Entries1
123 ; MsgId1 \== MsgId,
124 member(Entry, Entries1),
125 Entry = entry(_, _, Ref, _, MsgId, MsgStr2) ->
126 ( MsgStr = MsgStr2 -> true
127 ; setarg(6, Entry, MsgStr)
128 ),
129 Entries = Entries1
130 ; nonvar(MsgStr) ->
131 Entry = entry([], [], Ref, [], MsgId, MsgStr),
132 append(Entries1, [Entry], Entries)
133 ; Entries = Entries1
134 )