1/*  Part of Refactoring Tools for SWI-Prolog
    2
    3    Author:        Edison Mera
    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(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
   50% :- pred translate(+,+,?,?,+).
   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).
 rename_id(?Module, +OldTerm, +NewTerm, +Options) is det
Change an English Term by another and update its key in the po files.
   70rename_id(M, OldTerm, NewTerm, Options) :-
   71    i18n_refactor(rename_id_lang, M, OldTerm, NewTerm),
   72    replace_term(OldTerm, NewTerm, [module(M)|Options]).
   73
   74% TODO: optimize
   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    )