Skip to content

Commit

Permalink
Fix for spaces in Japanese unit patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
phensley committed Sep 3, 2024
1 parent c4f6599 commit 743e67d
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
14 changes: 14 additions & 0 deletions packages/cldr-compiler/src/cli/compiler/fixes/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Locale } from '@phensley/cldr-core';
import { fixJapaneseUnits } from './japanese';

/**
* Occasionally there are issues with the upstream data that
* we need to resolve. We could do this via the patching feature
* but since we're doing this internally that extra overhead is
* unnecessary.
*/
export const applyFixes = (locale: Locale, data: any) => {
if (locale.tag.language() === 'ja') {
fixJapaneseUnits(data);
}
};
27 changes: 27 additions & 0 deletions packages/cldr-compiler/src/cli/compiler/fixes/japanese.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { UnitLength } from '@phensley/cldr-core';

/**
* Remove spaces in Japanese unit patterns.
*
* Our Japanese translators indicate there should be no spaces in these
* unit patterns, for either standalone or mid-sentence contexts.
*
* See:
* - https://unicode-org.atlassian.net/browse/CLDR-10556?focusedCommentId=116703
* - https://unicode-org.atlassian.net/browse/CLDR-10715
*/
export const fixJapaneseUnits = (data: any) => {
fix(data, 'long');
fix(data, 'short');
fix(data, 'narrow');
};

const fix = (data: any, length: UnitLength) => {
const units = data.Units[length].unitPattern.other;
for (const key of Object.keys(units)) {
const value = units[key];
const i = value.indexOf('{0}');
// Ensure no space separates the unit from the quantity "{0}" in the pattern
units[key] = value.slice(0, i).trim() + '{0}' + value.slice(i + 3).trim();
}
};
6 changes: 6 additions & 0 deletions packages/cldr-compiler/src/cli/compiler/pack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import DEFAULT_CONFIG from './config.json';
import { Downloader } from '../downloader/downloader';
import { validateConfig } from './validate';
import { RBNFCollector } from '../../rbnf';
import { applyFixes } from './fixes';

/**
* Encodes fields into a resource pack and returns the offset
Expand Down Expand Up @@ -187,6 +188,11 @@ const runPackImpl = (argv: yargs.Arguments<PackArgs>, pkg: ProjectInfo) => {
}
pack.push(locale);
const main = getMain(locale.id);

// Apply internal fixes first
applyFixes(locale, main);

// Apply user patches
if (patchfiles.length) {
for (const patch of patchfiles) {
if (!applyPatch(locale.id, main, patch)) {
Expand Down
10 changes: 10 additions & 0 deletions packages/cldr/__tests__/japanese.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { getCLDR } from './_helpers';

test('units', () => {
const framework = getCLDR();
const api = framework.get('ja');
let s: string;

s = api.Units.formatQuantity({ value: 5, unit: 'hour' }, { length: 'long' });
expect(s).toEqual('5時間');
});

0 comments on commit 743e67d

Please sign in to comment.