Skip to content

Possible to map between index and source text #285

@Stadly

Description

@Stadly

I have a special use case where I, for some customers, need to override a few text strings. I want to have the application localized using wuchale, but be able to, based on the signed in user, override some strings. My plan is to, for each locale, define a mapping from source text to override text on the customer, stored in the database. When loading the catalog, I plan to do something like this:

  1. Load the catalogs using wuchale like normally
  2. Load the text override map for the current locale from the database based on the signed-in user
  3. For each override-text, find the corresponding index in the wuchale catalogs
  4. Update the index in the catalog with the override-text

I understand that this will result in the loader having to do async work, and hence placeholders will be shown. I don't think that will be an issue.

The problem I have is how to solve step 3. What do you think about one of these ideas:

  1. Implement an option for emitting a map from extracted text to catalog index that can be enabled per adapter in the wuchale config.
  2. Expose adapters in the Vite plugin:
export class Wuchale {
    // ...
    get adapters(): ReadonlyMap<string, AdapterHandler> {
        return this.#adapters
    }
}

Then I think I would be able to use a plugin like this:

import { wuchale } from '@wuchale/vite-plugin'

const wuchalePlugin = wuchale()

function wuchaleSourceMapPlugin() {
    return {
        name: 'wuchale-source-map',
        generateBundle() {
            // With the proposed getter, this would work:
            for (const [key, handler] of wuchalePlugin.adapters) {
                const sourceMap = {}
                for (const [sourceStr, id] of handler.sharedState.indexTracker.indices) {
                    sourceMap[id] = sourceStr
                }
                this.emitFile({
                    type: 'asset',
                    fileName: `wuchale-source-map-${key}.json`,
                    source: JSON.stringify(sourceMap),
                })
            }
        },
    }
}

export default {
    plugins: [wuchalePlugin, wuchaleSourceMapPlugin()],
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions