TriggerAu

Visual Studio Tools for KSP : Localization Helper

6 posts in this topic

In working on Localization for my mods I was finding it sorta tedious to correlate all the numbers/files etc so I indulged myself in spending some time on something I've always wanted to do: creating a Visual Studio Extension.

Currently this has a single focus - which is speeding up the replacement of strings with localizer calls, but with enough time will add in any useful additions.

So What does it do?

In a nutshell it does this (cause pictures give you a better score right?):

RefactorPreviewOnly.png

It makes it a bit easier to replace strings in your code with Localizer.Format calls. When its actioned it:

  • Replaced the string under the cursor with the Localizer.Format call that has the next tag injected
  • Made sure that the usings statement is in the top of the file
  • Added comment to the end of the line to say what the tag equals do you can still search your code for the text
  • Written the key/values to the cfg files you configure
  • Updated the ID in the settings so we can move straight to the next one
  • Recorded the steps errors to a custom output pane

With this I can now run through and refactor strings, and write the cfg files, using the native VS refactoring toolset.

I've published it in a git hub repo with an MIT license as per usual modding rules and will add any other features/functions I can come up with that help too.

Lots more info on the wiki.

Links and Further Info

DT-OSILogo16.png This work is licensed under an MIT license as outlined at the OSI site

9 people like this

Share this post


Link to post
Share on other sites

Posted (edited)

I worked with this tool this morning.  I like it.  The only issue I found with it was that it did not recognize interpolated strings.  The work around is to revert the string interpolation to a string.Format.  It then recognizes and localizes the string.  This is definitely a huge time saver.  I'd done 2 other mods before this and it took a lot longer :P

A feature I would like to see, although with translations, I'm not sure how viable it is (given the contextual nature of some languages), would be to avoid duplicate strings in the localization file.  If 2 strings are identical, use the previously created id and avoid an additional string in the localization file.

Edited by Papa_Joe

Share this post


Link to post
Share on other sites
5 hours ago, Papa_Joe said:

I worked with this tool this morning.  I like it.  The only issue I found with it was that it did not recognize interpolated strings.  The work around is to revert the string interpolation to a string.Format.  It then recognizes and localizes the string.  This is definitely a huge time saver.  I'd done 2 other mods before this and it took a lot longer :P

A feature I would like to see, although with translations, I'm not sure how viable it is (given the contextual nature of some languages), would be to avoid duplicate strings in the localization file.  If 2 strings are identical, use the previously created id and avoid an additional string in the localization file.

Can you whack me an example line of an interpolated string you had and I can try and hook it up?

Imma try and get it so it can replace a parameterized string.Format in the next week too, but that's another story :P

Reusing keys is something that I'm not 100% sure on myself. I have most of the code for it, but I left it out intentionally because of translations. One of the things we saw in the stock translation is even if the same word is in used in English its not always the same word in all the other languages. We had to go back and redo a number of keys to make extra ones due to this context, Ill see about putting it in as an option, but you do need to beware of the context

EDIT: Just realized you said about the context stuff too - sorry :P

2 people like this

Share this post


Link to post
Share on other sites

Posted (edited)

3 hours ago, TriggerAu said:

Can you whack me an example line of an interpolated string you had and I can try and hook it up?

Imma try and get it so it can replace a parameterized string.Format in the next week too, but that's another story :P

Reusing keys is something that I'm not 100% sure on myself. I have most of the code for it, but I left it out intentionally because of translations. One of the things we saw in the stock translation is even if the same word is in used in English its not always the same word in all the other languages. We had to go back and redo a number of keys to make extra ones due to this context, Ill see about putting it in as an option, but you do need to beware of the context

EDIT: Just realized you said about the context stuff too - sorry :P

Ya, I was also concerned about the translation issues when thinking about the duplicate strings.  Good to know that you had considered it.  String example below:

 - GUILayout.Label($"Part {part.partinfo.title} is missing Kerbal {kerbal.name}", labelStyle, GUILayout.Width(150));

I recently switched over all my mods to using string interpolation.  So, while not painful, it was an extra step to revert back to a string.Format.  Likely the best solution is to convert it to a string.Format and replace the objects in the bracket with indexes, then localize the string portion.

All in all a very useful extension.  Thanks for taking the time to build this.

 

Update:  Another thing I noted is that if you undo a localization in the code editor, the localization file does not update.  This results in orphaned strings in the localization file.  It does not break anything, but does cause confusion and potentially additional work for needless translations.

With that said, if you alter your numbering in the localization file and you wish for the counts to be consistent, be sure to update the NextProjectID in the .ksplocalizer.settings file, found in your project root. and restart your project.

Edited by Papa_Joe
Grammar, clarifications

Share this post


Link to post
Share on other sites
On 6/12/2017 at 10:34 AM, Papa_Joe said:

...

Update:  Another thing I noted is that if you undo a localization in the code editor, the localization file does not update.  This results in orphaned strings in the localization file.  It does not break anything, but does cause confusion and potentially additional work for needless translations.

With that said, if you alter your numbering in the localization file and you wish for the counts to be consistent, be sure to update the NextProjectID in the .ksplocalizer.settings file, found in your project root. and restart your project.

Ill see if I can hook the undo path, but not sure. You can however simply edit the NextID in the settings window (which updates the file) and the next use of a tag in the file will replace the contents for you

eg Settings window says nextID = 1... Refactor string for id 1... you press Ctrl-Z , Simply change the nextID value in the settings window to 1 again and on you go - it will update the #autoloc...1 tag when you do the next refactor

1 person likes this

Share this post


Link to post
Share on other sites

Posted (edited)

3 hours ago, TriggerAu said:

Ill see if I can hook the undo path, but not sure. You can however simply edit the NextID in the settings window (which updates the file) and the next use of a tag in the file will replace the contents for you

eg Settings window says nextID = 1... Refactor string for id 1... you press Ctrl-Z , Simply change the nextID value in the settings window to 1 again and on you go - it will update the #autoloc...1 tag when you do the next refactor

Thanks.   And here I went under the hood without thinking about the settings window.... :P 

Update:  I just wanted to say, with or without any action on the suggestions and observations, this is a good tool.  saved me loads of time.

Edited by Papa_Joe
additional comments

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now