Skip to content

Commit

Permalink
Updated readme
Browse files Browse the repository at this point in the history
  • Loading branch information
marcin-adamczewski committed Dec 6, 2024
1 parent ead3b90 commit e2e6322
Showing 1 changed file with 37 additions and 22 deletions.
59 changes: 37 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
# android-placeholders-validator
Gradle plugin which validates placeholders from translated strings.xml files by comparing them with main strings.xml file.
A Gradle plugin that validates placeholders in translated strings.xml files by comparing them with the main `strings.xml` file.

# Why may I need it?
If other people make translations for your app there is a risk that they may modify and malform string placeholders.\
I struggled with such situations which eventually led to app crashing only for Korean people (sorry Korean people).\
Then I applied the plugin in my other apps and it turned out that some placeholders are also malformed! Luckily it didn't
make the app crashing, but not luckily, displayed strings were incorrectly formatted.
Of course you could also malform or accidentaly modify such placeholder by yourself, but as we programmers don't make mistakes,
it is less probable, isn't it? :sweat_smile:
If other people translate your app, there is a risk they might modify or misformat string placeholders.\
I’ve encountered situations where this led to the app crashing only for Korean users (sorry, Korean friends!).\
Later, I applied this plugin to my other apps and discovered additional malformed placeholders.\
While these didn’t cause crashes, they did result in incorrectly formatted strings being displayed.\
Of course, it’s also possible to accidentally misformat or modify a placeholder yourself. \
But as programmers, we never make mistakes, right? 😅

# Validation
The plugin compares placeholders from translated strings.xml files with placeholders from your main strings.xml file.
When there is no match, you get clear message with exact place of issue, like this one:
The plugin compares placeholders in translated strings.xml files with those in your main `strings.xml` file.\
If there is a mismatch, it provides a clear message indicating the exact location of the issue, like this:

```
Affected file: .../app/src/main/res/values-fr/strings.xml Affected key: settings_version. Should be: [%1$s] but is: []
Expand All @@ -20,33 +20,48 @@ Affected file: .../app/src/main/res/values-de/strings.xml Affected key: settings
```

# Usage
In main build.grale file:
In the main `build.gradle` file:

```
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "gradle.plugin.com.appunite.placeholdersvalidator:placeholders-validator:1.0.2"
plugins {
// When using .toml
alias(libs.plugins.placeholder.validator) apply false
// Without .toml
id("com.appunite.placeholdersvalidator") version("1.1.0") apply(false)
}
}
```

At the end of your app module build.gradle file:
At the top of your app module `build.gradle` file:
```
// When using .toml
alias(libs.plugins.placeholder.validator)
// Without .toml
id("com.appunite.placeholdersvalidator")
```

At the end of your app module `build.gradle` file:

```
apply plugin: "com.appunite.placeholdersvalidator"
placeholdersValidator {
resourcesDir = android.sourceSets.main.res.sourceFiles
// Path to your main source set
resourcesDir = android.sourceSets.getByName("main").res.getSourceFiles()
// It's possible to ignore order for specific languages. It's useful for languages like Japanese
ignoredOrderLanguages.set(setOf("values-ja/strings.xml"))
// Disable plurals validation. By default it's true and only the first plural option is verified due to
// the fact that each language can have different number of options [one, few, many, others]
ignorePlurals = false
}
preBuild.dependsOn placeholdersValidatorTask
tasks.preBuild {
dependsOn("placeholdersValidatorTask")
}
```

Code below will run the validation before building. You can adjust it to your needs though.
```
preBuild.dependsOn placeholdersValidatorTask
tasks.preBuild {
dependsOn("placeholdersValidatorTask")
}
```

0 comments on commit e2e6322

Please sign in to comment.