Skip to content

[Translation] Fallback not working when using a script #17786

Closed
@twifty

Description

@twifty

My default locale is 'en'. At the moment, I only have resource catalogues for the 'en' locale. For every simple locale, for example 'en_US', 'zh_CN', the translator will fallback to the 'en' locale and issue a warning/error.

The problem comes into play when using locales with a script subtag, as in 'zh_Hant_SG' (Singapore Traditional Chinese) or 'zh_Hans_SG' (Simplified Chinese). These correctly fallback. But later, when using a non script locale 'zh_TW', these don't.

The cause of the problem is during the loading of fallback catalogues. 'zh_Hans_SG' will correctly load and add to the parent as a fallback 'zh_Hans', 'zh' and 'en'. BUT, The 'zh' catalogue stored in the Translator::$catalogues is a different instance to that stored as a fallback in the 'zh_Hans' catalogue, therefore, it has NO fallbacks.

I don't have time to fork and patch, so here is the fix for anybody who has chance:

private function loadFallbackCatalogues($locale)
    {
        $current = $this->catalogues[$locale];

        foreach ($this->computeFallbackLocales($locale) as $fallback) {
            if (!isset($this->catalogues[$fallback])) {
                $this->doLoadCatalogue($fallback);
                // This method call needs adding to populate fallbacks
                // in the newly created catalogue.
                $this->loadFallbackCatalogues($fallback);
            }

            $fallbackCatalogue = new MessageCatalogue($fallback, $this->catalogues[$fallback]->all());
            foreach ($this->catalogues[$fallback]->getResources() as $resource) {
                $fallbackCatalogue->addResource($resource);
            }
            $current->addFallbackCatalogue($fallbackCatalogue);
            $current = $fallbackCatalogue;
        }
    }

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions