Skip to content

Commit 126db0c

Browse files
[I18n] Fixes and improves plural handling (#77)
* We compute plural rules only once. * Added a bunch of known plural rules Reducing the probability of the JS engine usage. If we never load this, it's better. All POEdit-generated rules were added. * Updated CHANGELOG * try-with-ressources to load locales * Reorder langs in comments and add script variants * Updated copyright
1 parent fb75c6b commit 126db0c

File tree

4 files changed

+225
-60
lines changed

4 files changed

+225
-60
lines changed

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@ We follow semantic versioning: you can tell if a version contains breaking chang
66

77
Changes marked with :warning: are **breaking changes**.
88

9+
## QuartzLib 0.0.4
10+
11+
_Published on April 12th, 2021_
12+
13+
### Changed
14+
15+
#### Internationalization (i18n)
16+
17+
- Fixed a bug where plural scripts were loaded once per translation instead of once per file.
18+
- Optimised the plural script manager by adding many known scripts. This reduces the likelihood of
19+
having to start a JavaScript engine, improving performance by several orders of magnitude.
20+
921
## QuartzLib 0.0.3
1022

1123
_Published on April 11th, 2021_

src/main/java/fr/zcraft/quartzlib/components/i18n/translators/gettext/GettextPOTranslator.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
/*
2-
* Copyright or © or Copr. QuartzLib contributors (2015 - 2020)
2+
* This file is part of QuartzLib.
3+
*
4+
* Copyright or © or Copr. ProkopyL <[email protected]> (2015 - 2021)
5+
* Copyright or © or Copr. Amaury Carrade <[email protected]> (2015 – 2021)
6+
* Copyright or © or Copr. Vlammar <[email protected]> (2019 – 2021)
7+
*
8+
* This software is a computer program whose purpose is to create Minecraft mods
9+
* with the Bukkit API easily.
310
*
411
* This software is governed by the CeCILL-B license under French law and
512
* abiding by the rules of distribution of free software. You can use,

src/main/java/fr/zcraft/quartzlib/components/i18n/translators/gettext/POFile.java

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
/*
2-
* Copyright or © or Copr. QuartzLib contributors (2015 - 2020)
2+
* This file is part of QuartzLib.
3+
*
4+
* Copyright or © or Copr. ProkopyL <[email protected]> (2015 - 2021)
5+
* Copyright or © or Copr. Amaury Carrade <[email protected]> (2015 – 2021)
6+
* Copyright or © or Copr. Vlammar <[email protected]> (2019 – 2021)
7+
*
8+
* This software is a computer program whose purpose is to create Minecraft mods
9+
* with the Bukkit API easily.
310
*
411
* This software is governed by the CeCILL-B license under French law and
512
* abiding by the rules of distribution of free software. You can use,
@@ -118,7 +125,7 @@ public void parse() throws CannotParsePOException {
118125
return;
119126
}
120127

121-
try {
128+
try (final BufferedReader reader = rawReader) {
122129
String line;
123130
Integer lineNumber = 0;
124131

@@ -129,7 +136,7 @@ public void parse() throws CannotParsePOException {
129136
Map<String, String> tokens = new HashMap<>();
130137
String lastToken = null;
131138

132-
while ((line = rawReader.readLine()) != null) {
139+
while ((line = reader.readLine()) != null) {
133140
lineNumber++;
134141

135142
// We don't care about trailing whitespaces
@@ -174,17 +181,14 @@ public void parse() throws CannotParsePOException {
174181
if (!tokens.isEmpty()) {
175182
analyseEntry(tokens);
176183
}
184+
185+
// At the end we compute plural rules
186+
pluralForms = new PluralForms(pluralCount, pluralFormScript);
177187
} catch (IOException e) {
178188
throw new CannotParsePOException("An IO exception occurred while parsing the file", e);
179-
} finally {
180-
try {
181-
if (rawReader != null) {
182-
rawReader.close();
183-
rawReader = null;
184-
}
185-
} catch (IOException ignored) {
186-
}
187189
}
190+
191+
rawReader = null;
188192
}
189193

190194
/**
@@ -314,8 +318,6 @@ private void analyseEntry(Map<String, String> tokens) {
314318
}
315319
}
316320
}
317-
318-
pluralForms = new PluralForms(pluralCount, pluralFormScript);
319321
}
320322

321323
/**
@@ -374,7 +376,8 @@ public String getPluralFormScript() {
374376
* If you can use them, it's always better.
375377
*
376378
* <p>This method can only work correctly with Plural-Forms listed at:
377-
* http://www.gnu.org/software/gettext/manual/html_node/Plural-forms.html#Plural-forms
379+
* http://www.gnu.org/software/gettext/manual/html_node/Plural-forms.html#Plural-forms,
380+
* as well as POEdit-generated plural forms.
378381
*
379382
* @param count The count to compute plural for.
380383
* @return The plural index.

0 commit comments

Comments
 (0)