Generate Localizable.strings file based on occurrences of NSLocalizedString in codebase Swift - swift

I have a codebase were I've used NSLocalizedString everywhere. I now want some kind of script or tool that finds all these NSLocalizedString and formats a Localizable.strings file. Does this exist? I'd hate to have to do this manually.

1
You can use genstrings as #Extra Savoir-Faire noted.
Create a file names en.lproj in the root directory of your project.
cd into the project from the terminal.
Run this command find ./ -name "*.swift" -print0 | xargs -0 genstrings -o en.lproj
This should export all files ending with swift that have a NSLocalizedString.
Also you can have the en.lproj anywhere as long as you run this command in the it's parent directory (you can see it when run the ls command
2
You can also export the file directly from Xcode.. the .strings file.
Open your project in Xcode
Select Product
Select Export Localizations
Select the desired package
Select location and export.
When you go to the folder you exported. You will find the Localizable.strings in
en.xcloc -> Source Contents -> YourProjectName-> en.lproj
You can use the Localizable.strings from there.

I've used this app before to do what you’re asking about: http://linguanapp.com.

Per Apple's documentation:
Although you can create strings files manually, it is rarely necessary
to do so. If you write your code using the appropriate string-loading
macros, you can use the genstrings command-line tool to extract those
strings and create strings files for you.

Related

Opening all files of a certain filetype in vscode

For my website, I have one "root" folder with a bunch of subfolders containing many different types of files. Example:
root folder
subfolder
subfolder
HTML file
other files...
subfolder
HTML file
other files...
Many of these subfolders have HTML files in them. I am wondering if there are any commands I can run to open all of the HTML files in vscode, or of any filetype for that matter.
I am aware that cmd+control+p allows the selection of a specific file by search, but is there any modification of that for all files of a file type? It is probably possible to write a bash script to do this, but I am not well versed in bash and I am wondering if there are any built-in ways to do this on vscode or plugins I can install to do this.
It also should be noted I am on a mac.
Here's on option to find all files that match a given filename specification (in*.html in my example) and send that list of files to VS Code via xargs:
find . -iname "in*\.html" -print0 | xargs -0 code

How to rename files in VS Code in bulk?

Is there a way to search for ALL .txt files in a project and automatically replace/rename them to .js?
E.g user.txt to user.js
It seems I can't search for a file format in VS Code.
If you know how, please share!
You could just do it in the terminal.
mv *.txt *.js
You can use a combination of:
Filtering the EXPLORER panel to just show .txt files
See "Filtering the document tree" section of the VS Code docs
See related Stack Overflow post: "Filter files shown in Visual Studio Code"
Installing and using the Batch Rename extension
For example, given these files/folders:
Steps:
Start by clicking on the EXPLORER panel and filtering to just display .txt files
Select/highlight all the files, then right-click on any selected file, then select "Batch Rename"
That would open a text file ".Batch Rename.txt" with all the selected files
Do a regular find-and-replace to change .txt to .js
Note that the ".Batch Rename.txt" is unsaved. It's like a preview of what the changes would look like
Save ".Batch Rename.txt" and it will automatically close
The EXPLORER panel should now be empty since it's filtered on .txt
Remove the filter and the files should now be renamed
You can use find to do this in a terminal recursively.
find . -iname "*.txt" -exec rename .txt .js '{}' \;
refer Find multiple files and rename them in Linux
mv works if you want to just do it in a folder itself.
Old question, I know, but for you or anyone else coming in, if you're okay with using a non-VS Code solution, there's a GUI Windows tool called RegexRenamer that is so well-named, you already know what it does.
It gives you a preview of what the renamed files will look like, and has options to rename everything in subfolders or only the current folder, ignore/include file extensions in the search, and apply the rename only to folders/files or both.
What do you think?

How do I find all files in Xcode that are NOT a member of a target

One of the most common issues I have with multiple targets is forgetting to add a file to all targets. Its easy to import a file and not tick all the boxes...
In build phases there is a 'compile sources' view which is a list of files compiled for each target. However there seams to be no easy way to compare targets to see what files are in one but not the other.
Is there a way to find the files that are NOT a member of a particular target? A script perhaps?
Assuming your MyProject.xcodeproj is in your working directory, and all your sources are in or below your working directory, you could use a hack* like this to look for .m files that are not part of any target:
find . -name "*.m" -exec basename '{}' \; | xargs -I '{}' sh -c 'grep "{} in Sources" MyProject.xcodeproj/project.pbxproj > /dev/null; echo "$?\c"; echo " {}"' | grep "^1"
Once you remove the files from the project, you can remove them -- and other objc source files that are no longer referenced from your project -- from your git repo using this similar script:
find . -name "*.[hm]" -exec basename '{}' \; | xargs -I '{}' sh -c 'grep {} MyProject.xcodeproj/project.pbxproj > /dev/null; echo "$?\c"; echo " {}"' | grep "^1" | cut -c3- | xargs -I '%' find . -name '%' -exec git rm '{}' \;
* hack in the sense that it depends on implementation details of the Xcode file format.
I don't know of a way to script it, but for a file you can view a list of all targets with the associated targets checked, and you can check or uncheck targets from that list.
Open the Utilities pane and select the File Inspector. Then select a file and the related targets will be in the Target Membership area of the File Inspector.
You can even select multiple files and the targets that are associated to some but not all the selected files will be marked with '-'. If you check a target then all the selected files will be added to that target.
I built a tool which lets you extract what files are in targets
https://github.com/ConfusedVorlon/ProjectFileTool
You could compare this with a file search for .m files within your project directory.
In Xcode 8, listing the files from the Target -> Build Phases -> Compile Sources gave a list of files for each target but no easy way to find the missing ones. Selecting all files and copying them allows copies of the physical files to be pasted into a temporary directory. After doing this with both targets, into separate folders, duplicates can then be deleted until only files missing from the other target are left in each folder.
I've just had a "this class is not key value coding-compliant for the key" crash caused by a file not being included in a target so I had to find the missing files. Not an efficient or elegant solution but it got the job done.
If you don't know which files you added to a target, you can go to Build Phases->Compile Sources (or whatever section you have differences in). Then select everything and hit copy. Paste this into a diff program, select the other target and repeat.
I don't know a (completely) quick and easy way, but if you were after a script, I have a library that could do this here: https://github.com/jasperblues/XcodeEditor
Xcode assembles some env variables and one of them has the dir you want to look for:
LINK_FILE_LIST_normal_x86_64, for x86_64 target, it point to the LinkFileList, but from basedir you can get the actual dir with FileLists you may want.

How to move a file in to zip uncompressed, with zip cmd tool

I'm try to determine how to use the zip cmd line tool to move a file (uncompressed) in to a zip of compressed files (ie I want a zip in the end with all files but one compressed, b/c the one file is another compressed file).
Anyone know how to do this?
It looks like you could use -n option to just store the files with defined extensions together with -g option to append the file to archive.
I didn't test it, but something like this should do the trick:
zip -gn .foo archive.zip myAddedFile.foo
Although documentation states that, by default, zip does not compress files with extensions in the list .Z:.zip:.zoo:.arc:.lzh:.arj, so if you are adding a file with one of those extensions you should be fine.
Documentation to the command is here
-m is what I wanted, moves the file(s) into a zip.

Localization problems in iPhone

I have been trying to localize my iPhone app in two languages - english and german
I have created two folders en.lproj and de.lproj each containing Localizable.strings file for respective language. I have included these files in my resources folder, changed the encoding of these files to UTF-16.
I have tried cleaning the project and building it again, deleting folder at path: Library/Application Support/iPhone Simulator/User
But the problem is still there. Anybody having an idea please help.
We have added 2 languages (to test the process, 1 European & 1 Asian double byte)
It's a two stage process, you will need to localize both your strings and your xibs. For the strings,
Create the Localizable.strings (Only available under the MacOSX resources)
Right click the new file and 'Get Info'
Click the button 'Make File Localizable' (This will automatically add one language - English for me)
In the 'Groups & Files' you should now be able to expand the resource file and see the new language
From the command line run the command
genstrings -o English.lproj *.m. This will parse your .m files and extract all the NSLocalizedStrings and put them in the file.
If you do Get Info again, you should be able to add more languages
For the xibs, right click on them and 'Make File Localizable'. These can then be translated separately
I tried with Spanish successfully.See the basic steps I followed.
(Note:This project is not using interface builder.)
1.Add a folder named es.lproj in the root directory of the project.
2.Open terminal and go to the root directory.
3.Enter genstrings -o es.lproj *.m.This will create Localizable.strings file in the es.lproj folder.
(Make sure that all the strings that you want to localize,you are using NSLocalizedString instead of NSString.)
4.Go to Xcode and add es.lproj in to the Target.Make sure to confirm when the message pops up.
5.open Localizable.strings file, edit and save.
ex:
/* Phone Label */
"Phone" = "Teléfono";
5.Go to phone 'settings->General->International' and change 'Language' and 'Region format' accordingly.
6.Start you application.
This should work fine.