Skip to content

Commit

Permalink
Move plural conversions from loctool to ilib-tools-common (#51)
Browse files Browse the repository at this point in the history
* Copied ResourceConvert functions from loctool so that they could be shared if necessary
* Remove the ResourceConvert code from the loctool
    - left the wrapper there to convert from tools-common style of
      resources to loctool and vice-versa, but the contents of the
      code is moved to the library now
    - also left the tests to ensure that it is still working properly
    - fixed a bug in the resource conversion where it had left out
      the resource flavor before
* Added support for nested and sequential plurals
    - convert nested and sequential plurals into nested plurals at the
       top level of the tree
    - support multi-key ResourcePlural instances that allow for 2
      (or more!) keys for a resource
  • Loading branch information
ehoogerbeets authored Jan 28, 2025
1 parent 32ac524 commit 4965315
Show file tree
Hide file tree
Showing 15 changed files with 1,767 additions and 175 deletions.
7 changes: 7 additions & 0 deletions .changeset/honest-icons-brake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"ilib-tools-common": minor
---

Added functions convertPluralResToICU and convertICUToPluralRes to convert
ICU-style plural strings encoded in ResourceString instances into ResourcePlural
instances and vice-versa
6 changes: 6 additions & 0 deletions .changeset/rotten-pears-kick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"loctool": patch
---

Now uses ICU<->Plural conversion code from the ilib-tools-common library instead of its
own implementation. This way, the code can be shared between tools.
1 change: 1 addition & 0 deletions packages/ilib-tools-common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
"ilib-ctype": "workspace:^",
"ilib-locale": "workspace:^",
"ilib-xliff": "workspace:^",
"intl-messageformat": "^10.7.11",
"micromatch": "^4.0.8"
}
}
24 changes: 21 additions & 3 deletions packages/ilib-tools-common/src/Resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,24 @@ const translationImportant = [
* @abstract
*/
class Resource {
/**
* The type of this resource instance.
* @type {String | undefined}
*/
resType;

/**
* The source string or strings for the resource.
* @type {String|Array.<String>|Object}
*/
source;

/**
* The target string or strings for the resource.
* @type {String|Array.<String>|Object}
*/
target;

/**
* Construct a new Resource instance.
* The props may contain any
Expand Down Expand Up @@ -138,7 +156,7 @@ class Resource {
/**
* Return the target string or strings for this resource.
*
* @returns {String|Array.<String>|Object} the source string or
* @returns {String|Array.<String>|Object|undefined} the source string or
* strings of this resource
*/
getTarget() {
Expand Down Expand Up @@ -437,7 +455,7 @@ class Resource {
* properties, it is not added to the list of instances. This
* can be checked easily by calling the isInstance() method.
*
* @param {Resource} an instance of the current resource to
* @param {Resource} resource an instance of the current resource to
* record
* @returns {boolean} true if the instance was added, and
* and false otherwise
Expand All @@ -463,7 +481,7 @@ class Resource {
* affect the possible translation match between the given and
* the current resource.
*
* @param {Resource} a resource to check
* @param {Resource} resource a resource to check
* @returns {boolean} true if this is an instance of
* the current resource, false otherwise.
*/
Expand Down
3 changes: 1 addition & 2 deletions packages/ilib-tools-common/src/ResourceArray.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ class ResourceArray extends Resource {
/**
* Set the array of source strings for this resource.
*
* @override
* @param {Array.<String>} arr the array of strings to set
* as the source array
*/
Expand Down Expand Up @@ -379,7 +378,7 @@ class ResourceArray extends Resource {
* resource.
*
* @override
* @param {Resource} a resource to check
* @param {Resource} resource a resource to check
* @returns {boolean} true if this is an instance of
* the current resource, false otherwise.
*/
Expand Down
Loading

0 comments on commit 4965315

Please sign in to comment.