Deleting unused Translatable Strings

Forums for specific tips, techniques and example code
Stokes
Posts: 66
Joined: Wed Oct 13, 2010 2:06 pm
Location: QLD, Australia

Deleting unused Translatable Strings

Postby Stokes » Fri Jan 14, 2011 9:48 am

Just thought I would post a method for deleting translatable strings that are not used. Will save a lot of time if you are trying to translate into another language. Hopefully it will help someone at some stage :)

Code: Select all

constants Locale_NZ : Integer = 5129; Percent_Interval : Integer = 2; vars crLocale : Locale; crLocaleNZ : Locale; crObject : Object; crSchema : Schema; crString : TranslatableString; crStringNZ : TranslatableString; crStringsNZ : ConstantNDict; pbAllOk : Boolean; pbSchemaOk : Boolean; piCntUnusedSchema : Integer; piCntUnusedTotal : Integer; piDone : Integer; piLastPercentDone : Integer; piParamCount : Integer; piParamCountNZ : Integer; piPercentDone : Integer; piToDo : Integer; trLocales : ObjectArray; trConstants : ConstantSet; trMethods : MethodSet; piStart : Integer; trJcfFile : File; begin create trConstants transient; create trLocales transient; create trMethods transient; pbAllOk := true; crSchema := currentSchema; piStart := app.clock(); create trJcfFile transient; trJcfFile.fileName := "c:\Translatable_Strings_To_Delete.jcf"; trJcfFile.usePresentationFileSystem := true; trJcfFile.mode := File.Mode_Output; trJcfFile.kind := File.Kind_ANSI; trJcfFile.shareMode := File.Share_Exclusive; trJcfFile.open(); trJcfFile.writeLine("JadeCommandFile"); trJcfFile.writeLine("JadeVersionNumber " & app.getSystemVersion()); trJcfFile.writeLine("Commands"); trJcfFile.writeLine("AbortOnError False"); while crSchema <> null do write Tab & "Checking strings in schema " & crSchema.name; pbSchemaOk := true; piCntUnusedSchema := 0; trLocales.clear(); crSchema.getAllLocalLocales(trLocales); crLocaleNZ := null; foreach crObject in trLocales do crLocale := crObject.Locale; if crLocale.languageId = Locale_NZ then crLocaleNZ := crLocale; break; endif; endforeach; if crLocaleNZ <> null then crStringsNZ := crLocaleNZ.getTranslatableStrings(); piToDo := crStringsNZ.size(); piDone := 0; piPercentDone := 0; piLastPercentDone := 0; write Tab & Tab & "0%"; foreach crObject in crStringsNZ do crStringNZ := crObject.TranslatableString; piParamCountNZ := crStringNZ.getParameterCount(); trMethods.clear(); trConstants.clear(); crStringNZ.getMethodRefs(trMethods); crStringNZ.getConstantRefs(trConstants); if trMethods.size() = 0 and trConstants.size() = 0 then piCntUnusedSchema := piCntUnusedSchema + 1; //write Tab & Tab & crStringNZ.name & " is NOT used"; trJcfFile.writeLine("Delete TranslatableString " & crSchema.name & "::" & crObject.Constant.name); endif; endforeach; if piPercentDone <> 100 then write Tab & Tab & "100%"; endif; else write Tab & Tab & "Unable to locate locale " & Locale_NZ.String & " for schema " & crSchema.name; pbSchemaOk := false; endif; pbAllOk := pbAllOk and pbSchemaOk; if piCntUnusedSchema > 0 then write Tab & Tab & "Schema " & crSchema.name & " contains " & piCntUnusedSchema.String & " unused strings"; endif; piCntUnusedTotal := piCntUnusedTotal + piCntUnusedSchema; crSchema := crSchema.superschema; endwhile; trJcfFile.close(); write "Time = " & (app.clock() - piStart).String; write Tab & ">>> A total of " & piCntUnusedTotal.String & " unused strings were detected"; epilog delete trLocales; end;

User avatar
BeeJay
Posts: 312
Joined: Tue Jun 30, 2009 2:42 pm
Location: Christchurch, NZ

Re: Deleting unused Translatable Strings

Postby BeeJay » Fri Jan 14, 2011 1:32 pm

Your code is assuming that English NZ is the default base locale where the method references to the translatable strings would be located. This may not be the case for Jade developers in different countries where their default base locale may not be English NZ, and therefore none of the English NZ strings would necessarily have method references. For our deploys, we perform our unused strings check while there is only a single locale present in the source environment. For a development time performance improvement we only have a single locale in our environment during the development phase, and load in the additional locales prior to creating our releases.

There's also a few other places where there could be references to the translatable strings that need checking before you can safely delete the string:

constantUsages - A reference to a collection of embedded usages of the translatable string
xamlRefs - for any systems that have XAML based forms that use translatable strings
formBuildDataRefs - for any systems that have put the translatable string directly into the various control/form/menu captions

Cheers,
BeeJay.


Return to “Tips and Techniques”

Who is online

Users browsing this forum: No registered users and 13 guests