11#   File src/library/tools/R/translations.R
22#   Part of the R package, https://www.R-project.org
33# 
4- #   Copyright (C) 1995-2023  The R Core Team
4+ #   Copyright (C) 1995-2025  The R Core Team
55# 
66#   This program is free software; you can redistribute it and/or modify
77#   it under the terms of the GNU General Public License as published by
1818
1919# ### R based engine for managing translations
2020
21+ processPoFile  <-  function  (f , potfile ,
22+                            localedir  =  file.path(" inst"  , " po"  ),
23+                            mergeOpts  =  " "  , #  in addition to --update
24+                            verbose  =  getOption(" verbose"  ))
25+ {
26+     poname  <-  sub(" [.]po$"  , " "  , basename(f ))
27+     lang  <-  sub(" ^(R|RGui)-"  , " "  , poname )
28+     dom  <-  sub(" [.]pot$"  , " "  , basename(potfile ))
29+     mo_make  <-  ! is.null(localedir )
30+ 
31+     # # Will not update PO file if already in sync; keeps PO-Revision-Date.
32+     cmd  <-  paste(" msgmerge"  ,
33+                  if  (is.character(mergeOpts )) paste(" --update"  , mergeOpts ),
34+                  shQuote(f ), shQuote(potfile ))
35+     if  (verbose ) cat(" Running cmd"  , cmd , " :\n "  )
36+     else  message("   "  , poname , " :"  , appendLF  =  FALSE , domain  =  NA )
37+     if  (system(cmd ) !=  0L )
38+         return (warning(sprintf(" running msgmerge on %s failed"  , sQuote(f )),
39+                        call.  =  FALSE , domain  =  NA ))
40+ 
41+     res  <-  checkPoFile(f , TRUE )
42+     if  (nrow(res )) {
43+         print(res )
44+         if  (mo_make ) message(" not installing"  , domain  =  NA )
45+         # # the msgfmt -c step below would also fail (includes --check-format)
46+         return (warning(sprintf(" inconsistent format strings in %s"  , sQuote(f )),
47+                        call.  =  FALSE , domain  =  NA ))
48+     }
49+ 
50+     if  (! mo_make ) return (invisible ())
51+     dest  <-  file.path(localedir , lang , " LC_MESSAGES"  )
52+     dir.create(dest , FALSE , TRUE )
53+     dest  <-  file.path(dest , sprintf(" %s.mo"  , dom ))
54+     # # Skip compilation if PO is unchanged since last run / checkout?
55+     # if (file.exists(dest) && file_test("-ot", f, dest)) return(invisible())
56+     cmd  <-  paste(" msgfmt -c --statistics -o"  , shQuote(dest ), shQuote(f ))
57+     if  (verbose ) cat(" Running cmd"  , cmd , " :\n "  )
58+     if  (system(cmd ) !=  0L )
59+         return (warning(sprintf(" running msgfmt on %s failed"  , sQuote(f )),
60+                        call.  =  FALSE , domain  =  NA ))
61+ }
62+ 
2163# # This only works in a UTF-8 locale: specifically substr needs to count
2264# # UTF-8 chars
2365en_quote  <-  function (potfile , outfile )
@@ -116,7 +158,6 @@ update_pkg_po <- function(pkgdir, pkg = NULL, version = NULL,
116158    # # The interpreter is 'src' for the base package.
117159    is_base  <-  (pkg  ==  " base"  )
118160    have_src  <-  paste0(pkg , " .pot"  ) %in%  files 
119-     mergeCmd  <-  paste(" msgmerge"  , if (is.character(mergeOpts )) paste(" --update"  , mergeOpts ))
120161
121162    # # do R-pkg domain first
122163  if (pot_make ) {
@@ -136,33 +177,9 @@ update_pkg_po <- function(pkgdir, pkg = NULL, version = NULL,
136177    }
137178    pofiles  <-  dir(" po"  , pattern  =  " R-.*[.]po$"  , full.names  =  TRUE )
138179    pofiles  <-  pofiles [
pofiles  !=  " po/[email protected] " ]
 139-     # # .po file might be newer than .mo
140180    for  (f  in  pofiles ) {
141-         lang  <-  sub(" ^R-(.*)[.]po$"  , " \\ 1"  , basename(f ))
142-         # # Interestingly does *not* update the file dates
143-         cmd  <-  paste(mergeCmd , f , shQuote(potfile ))
144-         if (verbose ) cat(" Running cmd"  , cmd , " :\n "  ) else 
145-         message("   R-"  , lang , " :"  , appendLF  =  FALSE , domain  =  NA )
146-         if (system(cmd ) !=  0L ) {
147-             warning(" running msgmerge on "  , sQuote(f ), "  failed"  , domain  =  NA )
148-             next 
149-         }
150-         res  <-  checkPoFile(f , TRUE )
151-         if (nrow(res )) {
152-             print(res )
153-             message(" not installing"  , domain  =  NA )
154-             next 
155-         }
156-         if (! mo_make ) next 
157-         dest  <-  file.path(stem , lang , " LC_MESSAGES"  )
158-         dir.create(dest , FALSE , TRUE )
159-         dest  <-  file.path(dest , sprintf(" R-%s.mo"  , pkg ))
160-  #        if(file_test("-ot", f, dest)) next
161-         cmd  <-  paste(" msgfmt -c --statistics -o"  , shQuote(dest ), shQuote(f ))
162-         if (verbose ) cat(" Running cmd"  , cmd , " :\n "  )
163-         if (system(cmd ) !=  0L )
164-             warning(sprintf(" running msgfmt on %s failed"  , basename(f )),
165-                     domain  =  NA , immediate.  =  TRUE )
181+         processPoFile(f , potfile , localedir  =  if (mo_make ) stem ,
182+                       mergeOpts  =  mergeOpts , verbose  =  verbose )
166183    }
167184
168185    # # do en@quot
@@ -179,7 +196,7 @@ update_pkg_po <- function(pkgdir, pkg = NULL, version = NULL,
179196        if (verbose ) cat(" Running cmd"  , cmd , " :\n "  )
180197        if (system(cmd ) !=  0L )
181198            warning(sprintf(" running msgfmt on %s failed"  , basename(f )),
182-                     domain  =  NA ,  immediate.   =   TRUE )
199+                     domain  =  NA )
183200    }
184201
185202    if (! (is_base  ||  have_src )) return (invisible ())
@@ -227,30 +244,8 @@ update_pkg_po <- function(pkgdir, pkg = NULL, version = NULL,
227244    pofiles  <-  dir(" po"  , pattern  =  " ^[^R].*[.]po$"  , full.names  =  TRUE )
228245    pofiles  <-  pofiles [
pofiles  !=  " po/[email protected] " ]
 229246    for  (f  in  pofiles ) {
230-         lang  <-  sub(" [.]po"  , " "  , basename(f ))
231-         cmd  <-  paste(mergeCmd , shQuote(f ), shQuote(potfile ))
232-         if (verbose ) cat(" Running cmd"  , cmd , " :\n "  ) else 
233-         message("   "  , lang , " :"  , appendLF  =  FALSE , domain  =  NA )
234-         if (system(cmd ) !=  0L ) {
235-             warning(" running msgmerge on "  ,  f , "  failed"  , domain  =  NA )
236-             next 
237-         }
238-         res  <-  checkPoFile(f , TRUE )
239-         if (nrow(res )) {
240-             print(res )
241-             message(" not installing"  , domain  =  NA )
242-             next 
243-         }
244-         if (! mo_make ) next 
245-         dest  <-  file.path(stem , lang , " LC_MESSAGES"  )
246-         dir.create(dest , FALSE , TRUE )
247-         dest  <-  file.path(dest , sprintf(" %s.mo"  , dom ))
248- #         if(file_test("-ot", f, dest)) next
249-         cmd  <-  paste(" msgfmt -c --statistics -o"  , shQuote(dest ), shQuote(f ))
250-         if (verbose ) cat(" Running cmd"  , cmd , " :\n "  )
251-         if (system(cmd ) !=  0L )
252-             warning(sprintf(" running msgfmt on %s failed"  , basename(f )),
253-                     domain  =  NA )
247+         processPoFile(f , potfile , localedir  =  if (mo_make ) stem ,
248+                       mergeOpts  =  mergeOpts , verbose  =  verbose )
254249    }
255250    # # do en@quot
256251    if  (l10n_info()[[" UTF-8"  ]] &&  mo_make ) {
@@ -314,28 +309,9 @@ update_RGui_po <- function(srcdir,
314309  }
315310    pofiles  <-  dir(" src/library/base/po"  , pattern  =  " ^RGui-.*[.]po$"  , full.names  =  TRUE )
316311    for  (f  in  pofiles ) {
317-         lang  <-  sub(" ^RGui-(.*)[.]po$"  , " \\ 1"  , basename(f ))
318-         lang2  <-  sub(" [.]po"  , " "  , basename(f ))
319-         message("   "  , lang2 , " :"  , appendLF  =  FALSE , domain  =  NA )
320-         cmd  <-  paste(" msgmerge --update"  , mergeOpts , f , potfile )
321-         if (system(cmd ) !=  0L ) {
322-             warning(" running msgmerge failed"  , domain  =  NA )
323-             next 
324-         }
325-         res  <-  checkPoFile(f , FALSE )
326-         if (nrow(res )) {
327-             print(res )
328-             next 
329-         }
330-         if (! mo_make ) next 
331-         dest  <-  file.path(" src/library/translations/inst"  , lang , " LC_MESSAGES"  )
332-         dir.create(dest , FALSE , TRUE )
333-         dest  <-  file.path(dest , " RGui.mo"  )
334-         if  (file_test(" -ot"  , f , dest )) next 
335-         cmd  <-  paste(" msgfmt -c --statistics -o"  , dest , f )
336-         if (system(cmd ) !=  0L )
337-             warning(sprintf(" running msgfmt on %s failed"  , basename(f )),
338-                     domain  =  NA )
312+         processPoFile(f , potfile ,
313+                       localedir  =  if (mo_make ) " src/library/translations/inst"  ,
314+                       mergeOpts  =  mergeOpts )
339315    }
340316
341317    invisible ()
0 commit comments