Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: i18n framework based on extracting translations from plugin's /languages/ to /language/ #8435

Open
wants to merge 23 commits into
base: master
Choose a base branch
from

Conversation

linonetwo
Copy link
Contributor

@linonetwo linonetwo commented Jul 27, 2024

Sorry for being late.

This is based on logic in #7821 (comment)

Lingo macro keep untouched. Will handle fallback between de-AT and de-DE. fixes #8263

How it works:

  1. When wiki start and language change, $tw.wiki.unpackPluginTiddlers will be called, and in this method we can modify shadowTiddlers, which will be used by $tw.wiki.getTiddler from wikitext.
  2. I add a call to my new $tw.utils.activatePluginTranslations method in unpackPluginTiddlers
  3. Plugin authors will write l10n tiddlers under xxx/languages/, and this PR make TW auto move it to xxx/language/, so lingo macro works with xxx/language/ base path.
  4. It move en-GB first, then the active language and dependeicies, so it will fallback in cascade.

图片

  1. RFC: $tw.utils.activatePluginTranslations will be undefined on first call, when utils is not mounted, so I check if it is undefined
  2. RFC: when loading wiki and switch language, unpackPluginTiddlers will be called at least 3 times, wasting lots of repeated work in activatePluginTranslations. But I can't skip it, because it reset shadowTiddlers = Object.create(null); each time it's called.

Downside of this implementation is it not able to translate user wiki any more. Which was achieved by modify lingo macro in #7821:

!! Any l10n strings

To translate any text that directly placed in user's wiki, instead of in a language plugin, you can set the lingo-base variable to teach <<.def lingo>> macro the place to look for.

commit 14f7f98
Author: lin onetwo <[email protected]>
Date:   Sat Jun 8 19:45:17 2024 +0800

    Revert "refactor: remove the "tree" tid"

    This reverts commit fb70f0b.

commit fb70f0b
Author: lin onetwo <[email protected]>
Date:   Sat Jun 8 19:36:28 2024 +0800

    refactor: remove the "tree" tid

commit 5c8b392
Merge: 6603794 33bc77f
Author: lin onetwo <[email protected]>
Date:   Sat Jun 8 19:36:13 2024 +0800

    Merge remote-tracking branch 'upstream/master' into feat/i18n

commit 6603794
Author: linonetwo <[email protected]>
Date:   Fri Feb 9 23:18:45 2024 +0800

    docs: Headings -> Heading

commit 7f2b8b2
Author: linonetwo <[email protected]>
Date:   Tue Feb 6 22:23:46 2024 +0800

    i18n: more for menubal

commit 8840a57
Author: linonetwo <[email protected]>
Date:   Tue Feb 6 22:07:58 2024 +0800

    i18n: add menubar translation for example

commit 33456a5
Author: linonetwo <[email protected]>
Date:   Tue Feb 6 21:37:07 2024 +0800

    feat: add tree to plugin to reveal l10n structure

commit 2579d7f
Author: linonetwo <[email protected]>
Date:   Tue Feb 6 21:36:51 2024 +0800

    i18n: add more l10n

commit 810b5d5
Author: linonetwo <[email protected]>
Date:   Tue Feb 6 21:08:50 2024 +0800

    feat: add comment, so when use <$text text=<<lingo>> by mistake, it shows

commit ab9572e
Author: linonetwo <[email protected]>
Date:   Tue Feb 6 21:00:30 2024 +0800

    docs: more tiddlyweb l10n

commit f3eefa6
Author: linonetwo <[email protected]>
Date:   Tue Feb 6 20:06:29 2024 +0800

    refactor: update translation of tiddlyweb

commit aaf3182
Author: linonetwo <[email protected]>
Date:   Tue Feb 6 19:43:04 2024 +0800

    Delete translateMacro.tid

commit 36a9d49
Author: linonetwo <[email protected]>
Date:   Tue Feb 6 19:36:02 2024 +0800

    refactor: update translate macro to reuse lingo macro

commit bef871d
Author: lin onetwo <[email protected]>
Date:   Sat Dec 16 01:40:55 2023 +0800

    docs: more usage about inline

commit 9fbd71e
Author: lin onetwo <[email protected]>
Date:   Sat Dec 16 01:34:45 2023 +0800

    Update LingoMacro.tid

commit a8c5bc0
Author: lin onetwo <[email protected]>
Date:   Sat Dec 16 01:33:47 2023 +0800

    docs: about  mode:"inline"

commit 2055f36
Author: lin onetwo <[email protected]>
Date:   Sat Dec 16 01:27:40 2023 +0800

    refactor: use function instead of set variable

commit 7d321fe
Author: lin onetwo <[email protected]>
Date:   Sat Dec 16 00:26:55 2023 +0800

    refactor: use lingo and procedure

commit cacd236
Author: lin onetwo <[email protected]>
Date:   Wed Dec 13 23:22:12 2023 +0800

    feat: support block mode so you can transclude whole tiddler

commit a6b6132
Author: linonetwo <[email protected]>
Date:   Sat Oct 28 20:38:26 2023 +0800

    feat: t macro and docs
Copy link

Confirmed: linonetwo has already signed the Contributor License Agreement (see contributing.md)

This was referenced Jul 27, 2024
@linonetwo linonetwo marked this pull request as ready for review July 27, 2024 19:41
Copy link
Member

@pmario pmario left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did just review the code. I'll give some more feedback. Once I did test it in detail.

@linonetwo
Copy link
Contributor Author

I also tried patch one plugin from another, with fallback.

图片

@pmario
Copy link
Member

pmario commented Jul 27, 2024

@linonetwo -- I thought you would have a closer look at the $:/core/modules/pluginswitcher.js, which is responsible to switch languages and themes.

I intended to suggest that the new functionality of extracting and removing languages should be part of the pluginswitcher.js

It is registered at startup.js but the main code is at /pluginswitcher.js

  • At startup.js line 80 the language-switcher is registered, which in turn adds an event listener from pluginswitcher.js
  • The swtichPlugins() funciton contains the logic to extract the language tiddlers from the corresponding plugins and overwrites the default en-GB shadow tiddlers, because they have the same "namespace"
  • accumulatePlugin() takes care of the dependencies
  • Then there is .unregisterPluginTiddlers()
  • . registerPluginTiddlers()
  • and finally .upackPluginTiddlers()

This mechanism makes sure that only those shadow tiddlers are in the shadow-store that are part of the activated language.
If a language is missing some translations, the default language or dependents are still there. Because of the same namespace.

IMO there is no reason to have all languages extracted from the plugins at startup. The number of language related shadow tiddlers should be constant and as low as possible.

We only have 1 core plugin in this PR, but the number of shadow tiddlers is increased by 174 tiddlers. The menubar-plugin de-DE translations.multids only contains 25 new texts + readme it should be 26 shadows more and not 174.

We have 33 possible languages in TW, which makes a total of 858 potential shadow tiddlers for menu-bar plugin alone.

The current total number of TW shadow tiddlers is: 2476 for v5.3.5 -- With the implementation in this PR the menubar has the potential to increase the number of shadows by 35%, which will directly impact the core performance for every filter that uses [all[shadows+tiddlers]] -- That's basically every UI related tiddler.

I was not happy, that every possible language is part of the plugin (because of size). -- But OK -- It was decided, that that's not a problem.

But I definitely think that only 1 set of language shadow tiddlers should be active at any given time.

@linonetwo
Copy link
Contributor Author

every possible language is part of the plugin

In current implementation, you can use another language plugin to provide l10n. See my last comment.

  • The swtichPlugins() funciton contains the logic to extract the language tiddlers

AFAIK It does this by calling unpackPluginTiddlers, which is I'm currently using. And in swtichPlugins() there is no way to access to shadowTiddlers. So I can't put my logic in it.

number of shadows will directly impact the core performance

In unpackPluginTiddlers -> activatePluginTranslations, I can delete unused shadowtiddlers.

@pmario
Copy link
Member

pmario commented Jul 28, 2024

number of shadows will directly impact the core performance

In unpackPluginTiddlers -> activatePluginTranslations, I can delete unused shadowtiddlers.

I think it should not be needed to delete unused shadow tiddlers. Only used shadows should be extracted into the /language/ namespace. As long as no unnecessary tiddlers are extracted no shadows need to be deleted.

en-GB will need to be always extracted since it is the "source of truth" and the final fallback. If the $:/language setting is de-DE then the German tiddlers are extracted into the same namespace that en-GB already uses. So no extra shadows are ever created. The same should be true for every other language, that is part of a plugin.

@linonetwo
Copy link
Contributor Author

linonetwo commented Jul 28, 2024

@pmario I've update the logic in unpackPluginTiddlers to not unpack unused language tiddlers.

The logic have to be called in unpackPluginTiddlers instead of in PluginSwitcher, because other places that call the unpackPluginTiddlers will clean up the language shadow tiddlers, so language shadow tiddlers added on language plugin switching/startup will lost.

I'm using $tw.language.activatePluginTranslations because I don't want to make boot.js file become much longer. Or we can use a library module and require it here, instead of use a util and check its existance.

@pmario
Copy link
Member

pmario commented Jul 28, 2024

@linonetwo

I do like your translation concept better than my initial one, which is much more complex. 🥇

I think the plugin menubar/language/ directory should be renamed to /languages/ to be consistent with the tiddler namespace.

The code and functionality looks good to me now.

every possible language is part of the plugin

In current implementation, you can use another language plugin to provide l10n. See my last comment.

I did miss that. That's a good thing. So we could decide to change the current behaviour if we want to, without compatibility problems.

@Jermolene

I did test:

  • The dependents setting -> OK
    • The good thing is, that dependents only need to be defined once in the main language file. Plugins will inherit this configuration.
  • Switching languages to German de-DE <-> de-AT -> OK
  • de-AT only contains 2 translations -> uses fallback de-DE -> OK
  • Change language -> Restart the wiki -> OK
  • Change back to English -> OK
  • Set language to de-DE and remove Config/HeadingMainXXX so en-GB fallback needs to be active -> OK

@pmario
Copy link
Member

pmario commented Jul 28, 2024

@linonetwo -- Please keep my de-AT and de-DE translations within this PR. So it's easier to test. -- I'll close my menubar-translation PR.

@linonetwo
Copy link
Contributor Author

linonetwo commented Jul 28, 2024

I think the plugin menubar/language/ directory should be renamed to /languages/ to be consistent with the tiddler namespace.

I was using /language/ because I wasn't delete unused shadow tiddlers. Now that they are deleted, it is OK to put then all under /languages/.

But notice that core translations are like {{$:/language/Buttons/Underline/Hint}}, and they are loaded from $:/languages/en-GB with /languages/. So it is also reasonable to keep the both /languages/ and /language/.

@pmario
Copy link
Member

pmario commented Jul 28, 2024

I was talking about the physical path name on the harddisk. The JS code is OK

grafik

@linonetwo
Copy link
Contributor Author

@Jermolene @pmario Is current implementation good enough?

@pmario
Copy link
Member

pmario commented Sep 1, 2024

See my last post: #8435 (comment)

@linonetwo
Copy link
Contributor Author

@pmario isn't that updated in last commit?

@Leilei332
Copy link
Contributor

Would it be possible to seperate plugin language packs from plugin itself? IMO this can avoid unnecessary language to be installed and avoid the size of the tiddlywiki file be increased too much when the plugin has many languages.

@linonetwo
Copy link
Contributor Author

Would it be possible to seperate plugin language packs from plugin itself?

@Leilei332 Yes. Mario already ask same question above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] Plugin Translation - Needs to respect the language plugin "dependents" field
3 participants