Skip to content
This repository has been archived by the owner on Mar 13, 2024. It is now read-only.

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
nd1012 committed Mar 9, 2024
1 parent 7438963 commit d25c58f
Show file tree
Hide file tree
Showing 15 changed files with 974 additions and 2 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# This workflow will build a .NET project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-net

name: .NET

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: 8.0.x
- name: Restore dependencies
run: dotnet restore ./src/wan24-PoeditParser.sln --ignore-failed-sources
- name: Build lib
run: dotnet build ./src/wan24-PoeditParser CLI/wan24-PoeditParser CLI.csproj --no-restore
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -396,3 +396,10 @@ FodyWeavers.xsd

# JetBrains Rider
*.sln.iml

# NuGetconfig
nuget.config
nuget-publish.bat

# Others
**/log.txt
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2024 nd
Copyright (c) 2024 Andreas Zimmermann, wan24.de

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
113 changes: 112 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,113 @@
# wan24-PoeditParser
Poedit source code parser

This is a small dotnet tool for parsing source code for gettext strings and
writing the result in the PO format to a file or STDOUT.

It's pre-configured for use with the
[`wan24-Core`](https://github.com/WAN-Solutions/wan24-Core) translation
helpers for C#, but it can be customized easily for any environment and any
programming language by customizing the used regular expressions in your own
configuration file.

## Usage

### Where to get it

The Poedit parser is available as a dotnet tool and can be installed from the
command line:

```bash
dotnet tool install -g wan24PoeditParser
```

### Default file extensions

Per default `.cs` file extensions will be looked up when walking through a
folder tree.

### Default keyword search

Per default keywords will be found by these regular expressions:

- `^.*((Description|DisplayText)\(\s*(\"".*[^\\]\"")\s*\)).*$` (`$3`)
- `^.*((_|gettextn?|Translate(Plural)?)\(\s*(\"".*[^\\]\"")).*$` (`$4`)
- `^.*(CliApi[^\s]*\([^\)]*Example\s*\=\s*(\"".*[^\\]\"")).*$` (`$2`)

**NOTE**: (Multiline) concatenated string value definitions (like
`"Part a" + "Part b"`) or interpolations can't be parsed. The matched keyword
must be C style escaped.

### Poedit extractor configuration

Command to extract translations:

```bash
dotnet tool run wan24PoeditParser (-singleThread) (--config "/path/to/wan24PoeditParserConfig.json") (--ext ".ext" ...) --output %o %C %F
```

**NOTE**: The `--config` parameter is optional and used in case you want to
define a custom configuration file. Using the optional `-singleThread` you can
disable multithreading (it'll override the configuration). The optional
`--ext` parameter defines the file extensions to use when walking through a
folder tree and overrides the configuration.

One input file list entry:

```bash
--input %f
```

Configured source code encoding (default is UTF-8):

```bash
--encoding %c
```

### Custom parser configuration

In the `wan24PoeditParserConfig.json` file in the root folder of this
repository you find the default configuration. You can download and modify it
for your needs, and use it with the `--config` parameter in the Poedit
extractor configuration.

The configuration allows to define regular expressions, where

- an array with two elements is a regular expression (and its `RegexOptions`
enumeration value) which needs to match the string to use, and export the
whole match as `$1`
- an array with three elements is used to replace a pattern (the 3rd element
is the replacement), if the regular expression does match

Example parser JSON configuration:

```json
{
"SingleThread": false,// (optional) Set to true to disable multithreading (may be overridden by -singleThread)
"Encoding": "UTF-8",// (optional) Source encoding to use (default is UTF-8; may be overridden by --encoding)
"Patterns": [// (optional)
["Any regular expression", "None"],// Search expression example
["Any regular search expression", "None", "Replacement"],// Replacement expression example
...
],
"FileExtensions": [// (optional) File extensions to include when walking through a folder tree (may be overridden by --ext)
".ext",
...
],
"Merge": false// (optional) Set to true to merge your custom configuration with the default configuration
}
```

The parser looks for any matching search expression, then applies all matching
replacement expressions to refer to the keyword to use, finally. If no
replacement matched the search expression string, the full search match will
be the used keyword.

During merging, lists will be combined, and single options will be overwritten.

### Parsing from the command line

If you want to call the parser manually, you can display help like this:

```bash
dotnet tool run wan24PoeditParser help (--api API (--method METHOD)) (-details)
```
46 changes: 46 additions & 0 deletions src/wan24-PoeditParser CLI/Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System.Collections.Frozen;

namespace wan24.PoeditParser
{
/// <summary>
/// Extensions
/// </summary>
internal static class Extensions
{
/// <summary>
/// Literal string replacements
/// </summary>
private static readonly FrozenDictionary<string, string> LiteralReplacements;

/// <summary>
/// Constructor
/// </summary>
static Extensions()
{
LiteralReplacements = new Dictionary<string, string>()
{
{"\"", "\\\"" },
{"\\", "\\" },
{"\0", "\\0" },
{"\a", "\\a" },
{"\b", "\\b" },
{"\f", "\\f" },
{"\n", "\\n" },
{"\r", "\\r" },
{"\t", "\\t" },
{"\v", "\\v" }
}.ToFrozenDictionary();
}

/// <summary>
/// Convert to a literal Poedit message string
/// </summary>
/// <param name="str">String</param>
/// <returns>Literal string</returns>
public static string ToPoeditMessageLiteral(this string str)
{
foreach (var kvp in LiteralReplacements) str = str.Replace(kvp.Key, kvp.Value);
return str;
}
}
}
Loading

0 comments on commit d25c58f

Please sign in to comment.