Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide support for localised resources #85

Open
epologee opened this issue Aug 29, 2013 · 14 comments
Open

Provide support for localised resources #85

epologee opened this issue Aug 29, 2013 · 14 comments

Comments

@epologee
Copy link

An example project, created with regular Xcode, contains two localized Localizable.strings files:

xcode

When inspecting the project with xcodeproj show ..., it will list localized files like this:

  - Resources:
    - Localizable.strings:
      - en
      - nl

The underlying Localizable.strings-files are actually in two folders called en.lproj and nl.lproj:

finder

When trying to mimic this setup, with the same underlying paths I add them like this:

project.new_file('Resources/en.lproj/Localizable.strings', 'Resources/Localizable.strings/en')

But the result is different from the way Xcode treats these special cases:

xcodeproj

Would you know how to add these files, so Xcode treats them the same way, as when you add them the old-fashioned way?

@Michenux
Copy link

Any solution for this bug ?

@lolgear
Copy link

lolgear commented Mar 12, 2014

@Michenux
I convert real path to xcodeproject path and add localizable strings. If you want I could share code with you

@Michenux
Copy link

@lolgear thanks a lot, i would prefer the fix to be included in the next release of xcodeproj. Maybe you can create a pull request.

@fabiopelosin fabiopelosin changed the title How to add localized resources correctly? Provide support for localised resources Mar 27, 2014
@martnst
Copy link

martnst commented May 11, 2014

As far as I know, for now pods need to provide localized files a bundle from which Localizable.strings can be access e.g. via NSLocalizedStringFromTableInBundle(key, tbl, bundle, comment).

see this pods for example:

@o15a3d4l11s2
Copy link

@lolgear would you please share the code for adding localizable resource? I have to add localizable images. Will your code help for this?

@lolgear
Copy link

lolgear commented Nov 10, 2014

@o15a3d4l11s2, ok, you can try it

@haxpor
Copy link

haxpor commented Jul 29, 2015

@epologee @Michenux I have successfully add Localizable.strings for additional languages added into the project. I created the script to manage my own project, and you can see it in following this line.

You can see the result from my tweet here (with photos).

Basically, if you take a look inside project.pbxproj file, those Localizable.strings are added into PBXVariantGroup. From my observation, there's only one of its instance. Thus the steps are as follows.

  1. Find PBXVariantGroup from #objects not from #groups. I wasted a couple of hours trying to get it from #groups without success.
  2. Now we're done adding localizable string to the project.
  3. Add Localizable.strings as part of #resources-build-phase for all project's targets (as needed). This means you need to iterate through all project's #native_targets, and add it properly and accordingly to its type (header => header-build-phase, source => sources-build-phase, and resources => resources-build-phase). You can look at Build Phases in Xcode to be familiar with those.
    Again, normally adding its folder i.e. fr.lproj which contains Localizable.strings inside instead of adding .strings file will do it very well. Check my addfile command here.
  4. You're done.

@epologee
Copy link
Author

epologee commented Aug 3, 2015

Sounds like @haxpor's solution could be part of Xcodeproj?

@haxpor
Copy link

haxpor commented Aug 4, 2015

Ohh sorry, I just found out that my code is not complete yet. I successfully did that because I added PBXVariantGroup via Xcode's way to add localization stuff first before executing my code, then the instance is found in project.pbxproj. That file didn't have PBXVariantGroup initially.

As observed in Ruby doc, I don't see how can I create PBXVariantGroup (if I miss anything please tell me). I think it might be a simple way but I just didn't see it yet. Just add a new group with isa is PBXVariantGroup in project.pbxproj file.

The following is what PBXVariantGroup looks like.
screen shot 2015-08-04 at 10 22 22 pm

@toshanmugaraj
Copy link

I added new method for creating variant

class Xcodeproj::Project::Object::PBXGroup
  def new_vargroup(name, path = nil, source_tree = :group)
          group = project.new(Xcodeproj::Project::Object::PBXVariantGroup)
          children << group
          group.name = name
          group.set_source_tree(source_tree)
          group.set_path(path)
          group
  end
end

And loop through the files inside *.lproj folders to add the localisation

def addvariance (direc, current_group)
    Dir.glob(direc) do |item|
        new_item = File.basename(item)
        if !current_group[new_item]
           var_group = current_group.new_vargroup(new_item, nil, '<group>')  
           var_group.new_reference(item, '<group>') 
        else 
          current_group[new_item].new_reference(item, '<group>')
        end
    end
end

@beribas
Copy link

beribas commented Oct 25, 2016

Was there an update on the issue? Would be great if this feature could find its way to Xcodeproj.
Thanks @toshanmugaraj for advice, I used your code snippet, but had to add the files to target resources, too. Otherwise they are not copied.

@simonseyer
Copy link
Contributor

I guess the ticket is solved since there is a new_variant_group method available in the PBXGroup class!?

@o15a3d4l11s2
Copy link

@Eldorado234 , I am not sure how to generate localised resources records using new_variant_group that you mentioned. Could you please give more information?

@simonseyer
Copy link
Contributor

simonseyer commented Feb 22, 2017

Sure @o15a3d4l11s2. This is the respective PR in the project I'm working on: jensmeder/Phoenx/pull/31. It boils down to the following:

Given the file structure:

Resources
    en.lproj
        Localizable.strings
    de.lproj
        Localizable.strings

You have to create the variant group named Localizable.strings in the Resources group:

parent_group = project.main_group.find_file_by_path('Resources')
variant_group = parent_group.new_variant_group('Localizable.strings') 

Then you can add the actual translation files:

variant_group.new_file('en.lproj/Localizable.strings')
variant_group.new_file('de.lproj/Localizable.strings')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests