How to use gettext-parser - 10 common examples

To help you get started, we’ve selected a few gettext-parser examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mapseed / platform / scripts / static-build.js View on Github external
path.resolve(
        baseLocaleDir,
        language.code,
        "LC_MESSAGES/messages.po"
      )
    );
    mergedPOFile = fs.readFileSync(mergedPOFileOutputPath);
    log("Finished merging .po file for " + language.code);
  } catch(e) {
    logError("Error merging .po file for " + language.code);
  }

  gt.addTranslations(
    language.code,
    "messages",
    gettextParser.po.parse(mergedPOFile)
  );
  gt.setTextDomain("messages");
  gt.setLocale(language.code);

  // Localize the config for the current language.
  walk(thisConfig, (val, prop, obj) => {
    if (typeof val === "string") {
      if (configGettextRegex.test(val)) {
        val = val
          .replace(configGettextRegex, "")
          .replace(/\)$/, "");
        obj[prop] = gt.gettext(val);
      }
    }
  });
  log("Finished localizing config for " + language.code);
github WordPress / gutenberg / packages / babel-plugin-makepot / src / index.js View on Github external
memo[ msgctxt ][ msgid ] = translation;
							} );
						}

						return memo;
					}, {} );

					// Merge translations from individual files into headers
					const data = merge( {}, baseData, { translations } );

					// Ideally we could wait until Babel has finished parsing
					// all files or at least asynchronously write, but the
					// Babel loader doesn't expose these entry points and async
					// write may hit file lock (need queue).
					const compiled = po.compile( data );
					writeFileSync( state.opts.output || DEFAULT_OUTPUT, compiled );
					this.hasPendingWrite = false;
				},
			},
github laget-se / react-gettext-parser / src / json2pot.js View on Github external
// Allow the consumer to transform headers
  const transformHeaders = opts.transformHeaders
    ? opts.transformHeaders
    : x => x
  const transformedPotJson = {
    ...potJson,
    headers: transformHeaders(potJson.headers),
  }

  const compilerOpts = {}
  if (opts.noWrap === true) {
    compilerOpts.foldLength = 0
  }

  const pot = po.compile(transformedPotJson, compilerOpts)

  return pot.toString()
}
github Automattic / sensei / scripts / pot-dist-references.js View on Github external
// Loop through message objects.
		Object.keys( context ).forEach( ( messageKey ) => {
			const message = context[ messageKey ];
			const { reference } = message.comments;

			if ( reference ) {
				message.comments.reference = getReferenceWithDist(
					reference,
					chunksMapBySource
				);
			}
		} );
	} );

	return po.compile( potObject );
};
github WordPress / gutenberg / i18n / babel-plugin.js View on Github external
memo[ msgctxt ][ msgid ] = translation;
							} );
						}

						return memo;
					}, {} );

					// Merge translations from individual files into headers
					const data = merge( {}, baseData, { translations } );

					// Ideally we could wait until Babel has finished parsing
					// all files or at least asynchronously write, but the
					// Babel loader doesn't expose these entry points and async
					// write may hit file lock (need queue).
					const compiled = po.compile( data );
					writeFileSync( state.opts.output || DEFAULT_OUTPUT, compiled );
					this.hasPendingWrite = false;
				},
			},
github westonruter / spoken-word / src / i18n / babel-plugin.js View on Github external
// By spec, enumeration order of object keys cannot be
					// guaranteed, but in practice most runtimes respect order
					// in which keys are inserted. We rely on this to specify
					// ordering alphabetically by file, line. A better solution
					// is to support or reimplement translations as array.
					data.translations.messages = fromPairs( sortBy(
						toPairs( data.translations.messages ),
						( [ , translation ] ) => translation.comments.reference
					) );

					// Ideally we could wait until Babel has finished parsing
					// all files or at least asynchronously write, but Babel
					// doesn't expose these entry points and async write may
					// hit file lock (need queue).
					const compiled = po.compile( data );
					writeFileSync( state.opts.output || DEFAULT_OUTPUT, compiled );
					this.hasPendingWrite = false;
				}
			}
github Mailtrain-org / mailtrain / lib / translate.js View on Github external
[].concat(config.language || []).forEach(lang => {
    let data;
    let file = path.join(__dirname, '..', 'languages', lang + '.mo');
    try {
        data = gettextParser.mo.parse(fs.readFileSync(file));
    } catch (E) {
        // ignore
    }
    if (data) {
        gt.addTranslations(lang, lang, data);
        gt.setTextDomain(lang);
        gt.setLocale(lang);
        log.info('LANG', 'Loaded language file for %s', lang);
    }
});
github rtymchyk / babel-plugin-extract-text / test / plugin.spec.js View on Github external
it('should extract 1 singular/plural string with context', () => {
        transform(
          '',
          OPTIONS
        )
        const po = poParser.parse(fs.readFileSync(TESTPO))
        assertHasPluralContextEntry(po, '1 flag', 'Many flags', 'Object')
        assertNumberOfEntries(po, 1)
      })
github rtymchyk / babel-plugin-extract-text / test / plugin.spec.js View on Github external
it('should extract 1 singular/plural string ', () => {
        transform("_n('One', 'Many', 5)", OPTIONS)
        const po = poParser.parse(fs.readFileSync(TESTPO))
        assertHasPluralEntry(po, 'One', 'Many')
        assertNumberOfEntries(po, 1)
      })
github rtymchyk / babel-plugin-extract-text / test / plugin.spec.js View on Github external
it('should extract comments', () => {
        transform(
          '', OPTIONS)
        const po = poParser.parse(fs.readFileSync(TESTPO))
        expect(po.translations['']['Hey there!'].comments.extracted).toBe('On Homepage')
        assertNumberOfEntries(po, 1)
      })
    })

gettext-parser

Parse and compile gettext po and mo files to/from json, nothing more, nothing less

MIT
Latest version published 3 months ago

Package Health Score

79 / 100
Full package analysis