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

Fix localisation variant groups #70

Merged
merged 4 commits into from
Oct 16, 2017
Merged

Conversation

ryohey
Copy link
Collaborator

@ryohey ryohey commented Sep 25, 2017

Add localized resources are added to one variant group.
described in #69

  1. Create variant groups of the base localization
  2. Add references to localized resources into base localization variant groups

@yonaskolb
Copy link
Owner

Thanks @ryohey, this is great!
Before I have a proper look at this, are you able to run swift test to generate the fixture project again and commit the changes (if there are any).

I actually just added a commit to master that fails the travis test if the generated fixture changes and wasn't committed, but it's after you created your branch

@ryohey
Copy link
Collaborator Author

ryohey commented Sep 25, 2017

OK thanks!
By the way I want to write some tests for this changes.
How do you think I can do that properly? (Is FixtureTests.swift good for that?)

@yonaskolb
Copy link
Owner

Yeah, FixtureTests would be a good place. You can make a new Fixture project, but I think it would just be easier to edit the current TestProject and localise the storyboard

let fileReference = getFileReference(path: filePath, inPath: path)
groupChildren.append(fileReference)
// create variant groups of the base localisation first
var baseLocalisationVariantGroups = [PBXVariantGroup]()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor: the rest of the codebase uses var baseLocalisationVariantGroups: [String] = [] syntax

var baseLocalisationVariantGroups = [PBXVariantGroup]()
if let baseLocalisedDirectory = localisedDirectories.first(where: { $0.lastComponent == "Base.lproj" }) {
for filePath in try baseLocalisedDirectory.children() {
let variantGroup = PBXVariantGroup(reference: generateUUID(PBXVariantGroup.self, filePath.lastComponent),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can pass the whole path.string into generateUUID so it's more deterministic

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK!

if let baseLocalisedDirectory = localisedDirectories.first(where: { $0.lastComponent == "Base.lproj" }) {
for filePath in try baseLocalisedDirectory.children() {
let variantGroup = PBXVariantGroup(reference: generateUUID(PBXVariantGroup.self, filePath.lastComponent),
children: Set(),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

children can use []. This will also be changing to an array in the next xcodeproj version anyway tuist/XcodeProj#87

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK!

var baseLocalisationVariantGroups = [PBXVariantGroup]()
if let baseLocalisedDirectory = localisedDirectories.first(where: { $0.lastComponent == "Base.lproj" }) {
for filePath in try baseLocalisedDirectory.children() {
let variantGroup = PBXVariantGroup(reference: generateUUID(PBXVariantGroup.self, filePath.lastComponent),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if more than 1 target has these files in it's sources? Do the variant groups need to be cached like the groupsByPath are?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's right. I will add the cache for variant groups.

allFilePaths.append(path)
// find base localisation variant group
let name = filePath.lastComponentWithoutExtension
guard let variantGroup = baseLocalisationVariantGroups.first(where: { Path($0.name).lastComponentWithoutExtension == name }) else {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if a localised file isn't inside a Base.lproj? Does this then not add the file reference anymore?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes It will do not add the reference. It can be a problem in the case that resources placed in en.lproj but not in Base.lproj. For example a case an user removed resource in Base.lproj but not one in en.lproj.
I will fix it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the project was created in the usual way, it looks like Base.lproj is included.

The project folder should contain one folder named Base.lproj and other language-specific folders with the .lproj extension. The prefix for the language folders is the language ID, as described in Language and Locale IDs.

https://developer.apple.com/library/content/documentation/MacOSX/Conceptual/BPInternational/MaintaingYourOwnStringsFiles/MaintaingYourOwnStringsFiles.html

@ryohey ryohey force-pushed the fix-localize branch 3 times, most recently from c720d9e to d8aa1e2 Compare September 28, 2017 02:58
@ryohey
Copy link
Collaborator Author

ryohey commented Sep 28, 2017

@yonaskolb I fixed issues you reviewed and I ran swift test then commit the updated project, but CI was failure. What should I do?

@yonaskolb
Copy link
Owner

Thats interesting. If you've really committed the current version of the generated project I'm not sure why it's generating a different on on travis.
One thing to make sure of is to not have the GeneratedProject open in Xcode, as Xcode will resave the project with some small formatting differences. If it's open close it and then run swift test again

@ryohey ryohey force-pushed the fix-localize branch 3 times, most recently from 7ddc1b2 to a5256f1 Compare September 29, 2017 06:33
@ryohey
Copy link
Collaborator Author

ryohey commented Sep 29, 2017

@yonaskolb Thanks I resolved. I used full path to generate UUID. I changed it to remove basePath. Could you review it?

@ryohey ryohey force-pushed the fix-localize branch 2 times, most recently from a0c15a9 to 9cc24bb Compare October 1, 2017 15:28
@ryohey
Copy link
Collaborator Author

ryohey commented Oct 1, 2017

rebased onto master and resolve conflicts

@ryohey
Copy link
Collaborator Author

ryohey commented Oct 3, 2017

@yonaskolb are there any problems?

@yonaskolb
Copy link
Owner

Hi @ryohey, sorry I haven't had time to look at this in depth. I'll review it as soon as I have some time

Copy link
Collaborator

@toshi0383 toshi0383 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍👍

for path in try localisedDirectory.children().sorted { $0.lastComponent < $1.lastComponent } {
guard fileReferencesByPath[path] == nil else {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens when multiple targets share localised sources that aren't in a Base.lproj directory? It looks like the second target won't get a build file, as this path is skipped

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You’re right. I will fix it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addObject(variantGroup)
// find base localisation variant group
let name = path.lastComponentWithoutExtension
let variantGroup = baseLocalisationVariantGroups.first(where: { Path($0.name).lastComponentWithoutExtension == name })
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: could use trailing closure syntax here


guard let variableGroup = getVariableGroups(resourceName).first else { throw failure("Couldn't find the variable group") }

do {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this just for visual grouping? Otherwise the it function is throwing so you don't need to wrap in a do

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it's just for visual grouping.

@ryohey
Copy link
Collaborator Author

ryohey commented Oct 16, 2017

I rebased this branch, but the code change is only 8596091, which fixed the problem pointed out in the review.

@yonaskolb yonaskolb merged commit 98706a3 into yonaskolb:master Oct 16, 2017
@yonaskolb
Copy link
Owner

Nice one @ryohey! 🎉

@keith
Copy link
Collaborator

keith commented Oct 17, 2017

I noticed this only seems to work if the default localization is in Base.lproj, not if you just have one in en.lproj. I'm not really sure if that's expected, or if it's worth solving for *.lproj, but it might be.

@keith
Copy link
Collaborator

keith commented Oct 17, 2017

Actually looks like there might be another issue with this, I get this error on CI:

2017-10-17 14:19:02.197 xcodebuild[1357:7330] warning:  The file reference for "Base.lproj/Localizable.strings" is a member of multiple groups ("Localizable.strings" and "Localizable.strings"); this indicates a malformed project.  Only the membership in one of the groups will be preserved (but membership in targets will be unaffected).  If you want a reference to the same file in more than one group, please add another reference to the same path.

@yonaskolb
Copy link
Owner

@ryohey, are you able to take a look at this?

@ryohey
Copy link
Collaborator Author

ryohey commented Oct 21, 2017

@keith If only one * .lproj other than Base.lproj is included in the project, It does not create a Variant Group but add a reference to the file directly, so I think it's not a problem.

However, if there are only en.lproj and ja.lproj, for example, they are added directly, so there is a problem. It's necessary to disable Base Localization to fix the issue but the project currently output by XcodeGen is unconditionally enabled Base Internationalization by specifing Base in knownRegions.

let knownRegions: [String] = ["en", "Base"]

(I just noticed that it might necessary to add the name of * .proj to knownRegions now)

If there is no base.lproj, it seems correct to use PBXProject's developmentRegion as Base Language, but developmentRegion is fixed to English, It may be necessary to adding syntax so that it can be specified developmentRegion from spec.

developmentRegion: "English",

As for the CI warnings please let me know your project's spec because I could not reproduced.

@keith
Copy link
Collaborator

keith commented Oct 22, 2017

Here's an example project with the warning. You need to build with the newest commit of XcodeGen for it to work at all. Run make to see the warning output in the console.

XcodeGenLocalizationWarnings.zip

@ryohey
Copy link
Collaborator Author

ryohey commented Oct 23, 2017

@keith Thanks I got the warning.
fixed in #102

@keith
Copy link
Collaborator

keith commented Oct 23, 2017

Awesome thanks so much!

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

Successfully merging this pull request may close these issues.

4 participants