How to make git consider .strings files as text file instead of binary file

Viewed 5350

Every time we make changes to the .strings files (for iOS localization) from different branches, we will have conflicts.

The reason seems to be that git consider the .strings as binary files instead of text files. Sometimes it is a little annoying because git won't help merging the changes automatically, again, because they are considered as in binary format.

Is there any git settings that will force it to consider a file as text file.

I notice that both SourceTree and GitLab won't be able to tell that it is text file. But XCode itself will be able to show us the diff. Not sure this is related or not.

enter image description here

9 Answers

None of the solutions worked for me, so I did some more research and at this link I found out this:

  • in your project main directory, create or edit a .gitattributes file adding this line:

    *.strings diff=localizablestrings
    
  • in the .gitconfig file in your home directory, add these lines:

    [diff "localizablestrings"]
    textconv = "iconv -f utf-16 -t utf-8"
    

This did the trick.

Here is my variant of the script I used on my mac. I need to convert all .strings files to utf-8 encoding, including the fact that some of them were already in utf-8. Run it from folder where your *.lproj directories are placed.

STRING_FILES=$(find . -type f -name '*.strings')
for f in $STRING_FILES; do 
  ENCODING=$(file -I $f)
  if [[ $ENCODING == *"utf-16le"* ]]; then
    iconv -f UTF-16 -t UTF8 $f > $f.1; 
    mv -f $f.1 $f
  fi
done

The idea of it was taken from Yuchen Zhong's answer

I've noticed that SourceTree is not able to diff english localization file only, the others were fine. So it looked like something was wrong in that file only.

I've replaced the english one with some other random Localized.strings file which were SourceTree diffing ok, copied english translations over, commit and voila - any new change was properly showed by SourceTree.

Remember you have to commit and see if it's working since you have to compare fixed one against fixed one.

I found the best way to do this was to use iconv to convert to UTF8, the other stuff does not work for viewing diffs in github.

iconv -f utf-16 -t utf-8 path/to/localization/en.lproj/Localizable.strings > temp.strings
mv temp.strings path/to/localization/en.lproj/Localizable.strings > temp.strings

Then commit and merge your changes. Subsequent diffs will be shown properly.

No need for .gitattributes and .gitconfig files in my case!

Simple steps you need to do:

  1. Open currently available .strings file and copy all of it content
  2. Open terminal type nano <path/to/your/file/Localizable.strings> - you can drag and drop file to terminal after typing nano - don't press return yet
  3. Remove file to trash (to save it in case of failure)
  4. Go back to terminal and press return
  5. Paste clipboard content to terminal
  6. Press control+x and y to save the file
  7. Repeat for .strings files in your project
  8. Enjoy diff on your .strings files!
Related