Integrate generated Swift constants for your localized strings in an Xcode Project Target.
The XCStrings Tool plugin integrates directly into Xcode providing a seamless integration into any of your project targets that contain Strings Catalog files.
If you haven't already, open up an Xcode project which contains the Strings Catalog files that you wish generate source code constants for.
Before integrating with any targets, you need to add the XCStrings Tool package dependency to your project.
In Xcode, click File → Add Package Dependencies… and in the search box, type github.com/liamnichols/xcstrings-tool-plugin
When using the Package Plugin, it's recommended to use the xcstrings-tool-plugin dependency instead of xcstrings-tool to benefit from the precompiled binary executable.
Select the xcstrings-tool-plugin package from the list of results, ensure that Add to Project is correctly set to your project and click Add Package.
Click Add Package again and the dependency will be imported into your project.
The Build Tool plugin is the main component of XCStrings Tool. Once integrated within a target, it'll tell the build system to invoke the generator whenever the Strings Catalogs in the target are modified. The generator then writes the Swift code to your derived sources which are then available for you to use within your other source files.
To integrate the plugin, navigate to your project settings, click on your desired target and navigate to the Build Phases tab.
Expand the Run Build Tool Plug-ins group and click the + button. In the list under xcstrings-tool-plugin, select XCStringsToolPlugin and then click the Add button:
Before building the product, let's just review our Strings Catalog quickly:
When working XCStrings Tool, it's recommended that each string has it's Key set to a lowerCamelCase identifier so that it matches the generated Swift code.
Additionally, you need to make sure that your strings have their Managed setting set to Manually. This will be the case already if you added the string with the + button, but if the compiler pre-populated the contents of the catalog, you might need to change this value.
The first time that you build your project, you'll be presented with the following alert:
Review the plugin and once you are ready, press Trust & Enable All. Your project should now build.
After building, in the left sidebar, open Report navigator, select the last build and review the build log for your target. You should spot a message similar to Run custom shell script 'XCStringsTool: Generate Swift code for ‘Localizable.xcstrings‘':
If you wish, you can open the file printed in the log output to review the generated code:
This code is compiled as part of your target, so is accessible just like code you would write yourself:
- doc:Changing-the-Access-Level