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

Plugin Translations #146

Merged
merged 9 commits into from
Dec 11, 2023
Merged

Plugin Translations #146

merged 9 commits into from
Dec 11, 2023

Conversation

roflmuffin
Copy link
Owner

@roflmuffin roflmuffin commented Nov 28, 2023

  • Adds an instance of IStringLocalizer to the base plugin, as well as adding this to the DI Container
    • This string localizer is an opinionated implementation that reads from a subdirectory in the plugin folder called lang for any valid locale json files, such as en.json, en-AU.json, fr.json etc.
    • These json files are simple key values of translations in a C# Format string, i.e. "Hello {0}, your ID is {1}"
    • The json files are parent locale compatible, so a user with locale en-GB will try to read from an en-GB locale file first, and then en if missing. They always fall back to en if a key cannot be found.
  • Adds a new "ServerLanguage" property to the core config, defaulted to en. This will change all localized strings into that language, unless there is another language context present (like a user command will return information in a users language).
  • Adds a new css_lang or !lang command which will print your current language, as well as accept an argument to change your language, i.e. !lang de will change to German, !lang en-AU will change to English Australia etc.
    • This is done through a singleton IPlayerLanguageManager which simply maps a SteamID to a culture info, or defaults to server language if not found. There is no cleanup process for this list at the moment so it would slowly leak memory if every single player sets their language and you have thousands of unique users (not really a concern yet).
  • Adds a new extension method on CCSPlayerController called GetLanguage which returns the users configured language or a default language if they have not set one.
  • Adds a new TemporaryCulture disposable which is used to set the current thread language until disposed, this is currently used in command handlers from the plugin side.
  • This also means that CounterStrikeSharp can no longer be run in invariant mode, and libicu is required.
  • This also handles color coded translations, e.g. {orange}This{default} text has {green}green{default} text

@KillStr3aK
Copy link
Contributor

KillStr3aK commented Nov 28, 2023

Good stuff in general, here are my thoughts:

  • Wouldn't it be possible to have other structure for the files? instead of splitting everything for each language/file it might be better to have a file that contains the phrase key as an object index, then it will have the language codes inside that, for e.g.:

    "testPlugin.maxPlayersAnnouncement": {
       "en": "...",
       "en-GB": "..."
    }
  • ServerLang is missing from the unmanaged code currently. (and docs)

@KillStr3aK
Copy link
Contributor

Good stuff in general, here are my thoughts:

  • Wouldn't it be possible to have other structure for the files? instead of splitting everything for each language/file it might be better to have a file that contains the phrase key as an object index, then it will have the language codes inside that, for e.g.:
    "testPlugin.maxPlayersAnnouncement": {
       "en": "...",
       "en-GB": "..."
    }
  • ServerLang is missing from the unmanaged code currently.

so we could have a lets say, common.json translation file and there would be others aswell, just if a plugin can be translated into many languages ppl would have to edit every language file one by one and add/update the new keys for each file which is pretty time consuming

@mificot
Copy link

mificot commented Nov 28, 2023

Good stuff in general, here are my thoughts:

  • Wouldn't it be possible to have other structure for the files? instead of splitting everything for each language/file it might be better to have a file that contains the phrase key as an object index, then it will have the language codes inside that, for e.g.:
    "testPlugin.maxPlayersAnnouncement": {
       "en": "...",
       "en-GB": "..."
    }
  • ServerLang is missing from the unmanaged code currently.

so we could have a lets say, common.json translation file and there would be others aswell, just if a plugin can be translated into many languages ppl would have to edit every language file one by one and add/update the new keys for each file which is pretty time consuming

That a good point! I don't think that it's necessary to split locales into different files as it is done in web apps, for example.

@roflmuffin
Copy link
Owner Author

I would like to use the same localizer for core, and the idea of a giant file with all locales does not seem fun to edit for me personally. Especially if someone wants to add an entire language they can just add a new file and the diff is easier to read. We could probably allow a nested json file option but I'd want to support both

@KillStr3aK
Copy link
Contributor

I would like to use the same localizer for core, and the idea of a giant file with all locales does not seem fun to edit for me personally. Especially if someone wants to add an entire language they can just add a new file and the diff is easier to read. We could probably allow a nested json file option but I'd want to support both

I'm not talking about one giant file, different files but not for language codes.
Look at SourceMod Translations for reference. (ignore the folders)

I don't mean the whole implementation, just how the files are separated and we could have a loader so the core could load its own translations, and plugins could do the same

@roflmuffin
Copy link
Owner Author

I'm just not sure I see the value in having multiple translation files (except for locale) per plugin; I can't see the scope of a plugin being so large that they need more than 1 language file for english for instance

@roflmuffin roflmuffin marked this pull request as ready for review December 11, 2023 03:26
@roflmuffin roflmuffin changed the title [Draft] Plugin Translations Plugin Translations Dec 11, 2023
@roflmuffin roflmuffin merged commit 5673935 into main Dec 11, 2023
4 checks passed
@roflmuffin roflmuffin deleted the feature/translations branch January 30, 2024 03:31
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.

3 participants