@@ -21,16 +21,25 @@ import {
2121 CatalogType ,
2222} from "./types"
2323import { CliExtractOptions } from "../lingui-extract"
24+ import { CliExtractTemplateOptions } from "../lingui-extract-template"
2425
2526const NAME = "{name}"
2627const LOCALE = "{locale}"
28+ const LOCALE_SUFFIX_RE = / \{ l o c a l e \} .* $ /
2729const PATHSEP = "/" // force posix everywhere
2830
2931export type MakeOptions = CliExtractOptions & {
3032 projectType ?: string
3133 orderBy ?: OrderBy
3234}
3335
36+ export type MakeTemplateOptions = CliExtractTemplateOptions & {
37+ projectType ?: string
38+ orderBy ?: OrderBy
39+ }
40+
41+ type CollectOptions = MakeOptions | MakeTemplateOptions
42+
3443export type MergeOptions = {
3544 overwrite : boolean
3645}
@@ -87,10 +96,16 @@ export class Catalog {
8796 this . writeAll ( cleanAndSort ( catalogs ) )
8897 }
8998
99+ makeTemplate ( options : MakeTemplateOptions ) {
100+ const catalog = this . collect ( options )
101+ const sort = order ( options . orderBy ) as ( catalog : CatalogType ) => CatalogType
102+ this . writeTemplate ( sort ( catalog as CatalogType ) )
103+ }
104+
90105 /**
91106 * Collect messages from source paths. Return a raw message catalog as JSON.
92107 */
93- collect ( options : MakeOptions ) {
108+ collect ( options : CollectOptions ) {
94109 const tmpDir = path . join ( os . tmpdir ( ) , `lingui-${ process . pid } ` )
95110
96111 if ( fs . existsSync ( tmpDir ) ) {
@@ -251,6 +266,17 @@ export class Catalog {
251266 this . locales . forEach ( ( locale ) => this . write ( locale , catalogs [ locale ] ) )
252267 }
253268
269+ writeTemplate ( messages : CatalogType ) {
270+ const filename = this . templateFile
271+ const basedir = path . dirname ( filename )
272+ if ( ! fs . existsSync ( basedir ) ) {
273+ fs . mkdirpSync ( basedir )
274+ }
275+ const options = { ...this . config . formatOptions , locale : undefined }
276+ const poFormat = getFormat ( "po" )
277+ poFormat . write ( filename , messages , options )
278+ }
279+
254280 writeCompiled ( locale : string , compiledCatalog : string , namespace ?: string ) {
255281 const ext = `.${ namespace === "es" ? "mjs" : "js" } `
256282 const filename = this . path . replace ( LOCALE , locale ) + ext
@@ -279,6 +305,12 @@ export class Catalog {
279305 ) as AllCatalogsType
280306 }
281307
308+ readTemplate ( ) {
309+ const filename = this . templateFile
310+ if ( ! fs . existsSync ( filename ) ) return null
311+ return this . format . read ( filename )
312+ }
313+
282314 get sourcePaths ( ) {
283315 const includeGlobs = this . include . map (
284316 ( includePath ) =>
@@ -291,6 +323,10 @@ export class Catalog {
291323 return glob . sync ( patterns , { ignore : this . exclude , mark : true } )
292324 }
293325
326+ get templateFile ( ) {
327+ return this . path . replace ( LOCALE_SUFFIX_RE , "messages.pot" )
328+ }
329+
294330 get localeDir ( ) {
295331 const localePatternIndex = this . path . indexOf ( LOCALE )
296332 if ( localePatternIndex === - 1 ) {
0 commit comments