Skip to content

Commit

Permalink
add options to disable keepCase of lang replacements
Browse files Browse the repository at this point in the history
  • Loading branch information
PssbleTrngle committed Mar 6, 2024
1 parent e2d33d6 commit 38cd60f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/emit/assets/lang.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type LangRule = Readonly<{

type ReplaceOptions = Readonly<{
matchCase?: boolean
keepCase?: boolean
lang?: string | string[]
mod?: string | string[]
}>
Expand Down Expand Up @@ -93,7 +94,7 @@ export default class LangEmitter implements LangRules, ClearableEmitter {
replaceValue(match: string | RegExp, value: string, options: ReplaceOptions = {}) {
const languages = arrayOrSelf(options.lang)
const mods = arrayOrSelf(options.mod)
const matcher = matchCase(value)
const matcher: (it: string) => string = options.keepCase === false ? () => value : keepCaseMatcher(value)

if (typeof match === 'string') {
if (options.matchCase) {
Expand Down Expand Up @@ -125,7 +126,7 @@ export default class LangEmitter implements LangRules, ClearableEmitter {
}

// Taken from https://stackoverflow.com/questions/17264639/replace-text-but-keep-case
function matchCase(replaceValue: string) {
function keepCaseMatcher(replaceValue: string) {
return (input: string) => {
let result = ''

Expand Down
24 changes: 24 additions & 0 deletions test/__snapshots__/lang.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,27 @@ exports[`replacing translation entries replaces using string value: replaced dio
"block.minecraft.polished_diorite_stairs": "Polished Bird poop Stairs",
}
`;

exports[`replacing translation entries respects keepCase option: kept case values 1`] = `
{
"block.minecraft.dark_oak_button": "Mahagony Button",
"block.minecraft.dark_oak_door": "Mahagony Door",
"block.minecraft.dark_oak_fence": "Mahagony Fence",
"block.minecraft.dark_oak_fence_gate": "Mahagony Fence Gate",
"block.minecraft.dark_oak_leaves": "Mahagony Leaves",
"block.minecraft.dark_oak_log": "Mahagony Log",
"block.minecraft.dark_oak_planks": "Mahagony Planks",
"block.minecraft.dark_oak_pressure_plate": "Mahagony Pressure Plate",
"block.minecraft.dark_oak_sapling": "Mahagony Sapling",
"block.minecraft.dark_oak_sign": "Mahagony Sign",
"block.minecraft.dark_oak_slab": "Mahagony Slab",
"block.minecraft.dark_oak_stairs": "Mahagony Stairs",
"block.minecraft.dark_oak_trapdoor": "Mahagony Trapdoor",
"block.minecraft.dark_oak_wall_sign": "Mahagony Wall Sign",
"block.minecraft.dark_oak_wood": "Mahagony Wood",
"block.minecraft.potted_dark_oak_sapling": "Potted Mahagony Sapling",
"block.minecraft.stripped_dark_oak_log": "Stripped Mahagony Log",
"block.minecraft.stripped_dark_oak_wood": "Stripped Mahagony Wood",
"item.minecraft.dark_oak_boat": "Mahagony Boat",
}
`;
10 changes: 10 additions & 0 deletions test/lang.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,14 @@ describe('replacing translation entries', () => {

expect(acceptor.paths()).toHaveLength(2)
})

it('respects keepCase option', async () => {
const acceptor = createTestAcceptor()

loader.lang.replaceValue('Dark Oak', 'Mahagony', { lang: 'en_us', mod: ['minecraft'], keepCase: false })

await loader.emit(acceptor)

expect(acceptor.jsonAt('assets/minecraft/lang/en_us.json')).toMatchSnapshot('kept case values')
})
})

0 comments on commit 38cd60f

Please sign in to comment.