1/*  Part of Refactoring Tools for SWI-Prolog
    2
    3    Author:        Edison Mera Menendez
    4    E-mail:        efmera@gmail.com
    5    WWW:           https://github.com/edisonm/refactor
    6    Copyright (C): 2017, Process Design Center, Breda, The Netherlands.
    7    All rights reserved.
    8
    9    Redistribution and use in source and binary forms, with or without
   10    modification, are permitted provided that the following conditions
   11    are met:
   12
   13    1. Redistributions of source code must retain the above copyright
   14       notice, this list of conditions and the following disclaimer.
   15
   16    2. Redistributions in binary form must reproduce the above copyright
   17       notice, this list of conditions and the following disclaimer in
   18       the documentation and/or other materials provided with the
   19       distribution.
   20
   21    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   22    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   23    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   24    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   25    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   26    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   27    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   28    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   29    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   30    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   31    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   32    POSSIBILITY OF SUCH DAMAGE.
   33*/
   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(ref_changes)).   43:- use_module(library(ref_replacers)).   44:- use_module(library(i18n/i18n_file_utils)).   45:- use_module(library(i18n/i18n_parser)).   46:- use_module(library(i18n/i18n_support)).   47
   48% :- pred translate(+,+,?,?,+).
   49translate(M, Lang, TermEngl, TermLang, OptionL) :-
   50    translate_po(M, Lang, TermEngl, TermLang),
   51    replace_term(TermLang, TermEngl, [module(M)|OptionL]).
   52
   53translate(M, Lang, EnglLangL, OptionL) :-
   54    forall(member(Engl-Lang, EnglLangL),
   55           translate_po(M, Lang, Engl, Lang)),
   56    replace_term(TermLang, TermEngl,
   57                 ( member(TermEngl-Lang, EnglLangL),
   58                   TermLang=@=Lang,
   59                   TermLang=Lang
   60                 ), [module(M)|OptionL]).
   61
   62translate_po(M, Lang, TermEngl, TermLang) :-
   63    i18n_refactor(update_po_entry(Lang), M, TermLang, TermEngl).
 rename_id(?Module, +OldTerm, +NewTerm, +OptionL) is det
Change an English Term by another and update its key in the po files.
   68rename_id(M, OldTerm, NewTerm, OptionL) :-
   69    i18n_refactor(rename_id_lang, M, OldTerm, NewTerm),
   70    replace_term(OldTerm, NewTerm, [module(M)|OptionL]).
   71
   72% TODO: optimize
   73:- meta_predicate replace_entry_po(+,+,+,2).   74replace_entry_po(M, MsgId0, MsgId, StrReplacer) :-
   75    freeze(MsgStr0, call(StrReplacer, MsgStr0, MsgStr)),
   76    findall(PoFile-Codes,
   77            ( current_po_file(M, '??', PoFile),
   78              read_po_file(PoFile, Entries0),
   79              replace_entry(M, MsgId0, MsgStr0, MsgId, MsgStr, Entries0, Entries),
   80              parse_po_entries(Entries, Codes, [])
   81            ), FileCodes),
   82    save_changes(FileCodes).
   83
   84:- meta_predicate i18n_refactor(3,+,+,+).   85
   86i18n_refactor(PoUpdater, M, Term0, Term) :-
   87    i18n_to_translate(Term0, _, KeysValues, []),
   88    i18n_to_translate(Term,  _, ValuesKeys, []),
   89    pairs_keys_values(KeysValues, MsgId0, MsgId),
   90    pairs_keys_values(ValuesKeys, MsgId,  MsgId0),
   91    call(PoUpdater, M, MsgId0, MsgId).
   92
   93update_po_entry(Lang, M, MsgStr, MsgId) :-
   94    update_po_entry(Lang, M, MsgStr, MsgId, MsgStr).
   95
   96update_po_entry(Lang, M, MsgId0, MsgId, MsgStr) :-
   97    current_po_file(M, Lang, PoFile),
   98    read_po_file(PoFile, Entries0),
   99    replace_entry(M, MsgId0, _, MsgId, MsgStr, Entries0, Entries),
  100    save_to_po_file(Entries, PoFile).
  101
  102rename_id_lang(MsgId0, MsgId, M) :-
  103    findall(PoFile-Codes,
  104            ( current_po_file(M, '??', PoFile),
  105              rename_id_po_file(PoFile, MsgId0, MsgId, M, Codes)
  106            ), FileCodes),
  107    save_changes(FileCodes).
  108
  109rename_id_po_file(PoFile, MsgId0, MsgId, M, Codes) :-
  110    read_po_file(PoFile, Entries0),
  111    replace_entry(M, MsgId0, MsgStr, MsgId, MsgStr, Entries0, Entries),
  112    parse_po_entries(Entries, Codes, []).
  113
  114replace_entry(M, MsgId0, MsgStr0, MsgId, MsgStr, Entries0, Entries) :-
  115    reference(M, Ref),
  116    ( member(Entry0, Entries0),
  117      Entry0 = entry(_, _, Ref, _, MsgId0, MsgStr0) ->
  118      setarg(5, Entry0, MsgId),
  119      setarg(6, Entry0, MsgStr),
  120      Entries = Entries0
  121    ; MsgId0 \== MsgId,
  122      member(Entry, Entries0),
  123      Entry =  entry(_, _, Ref, _, MsgId, MsgStr1) ->
  124      ( MsgStr = MsgStr1 -> true
  125      ; setarg(6, Entry, MsgStr)
  126      ),
  127      Entries = Entries0
  128    ; nonvar(MsgStr) ->
  129      Entry =  entry([], [], Ref, [], MsgId, MsgStr),
  130      append(Entries0, [Entry], Entries)
  131    ; Entries = Entries0
  132    )